77

I know how to configure APT to use a web proxy. But what about snap?

FrodeTennebo
  • 103
  • 5
Carlos Estrada
  • 1,105
  • 3
  • 10
  • 12
  • could you please remove the "16.04" from the title? This topic is relevant for more or less every Ubuntu version. – Simon Sudler Jan 31 '23 at 08:39
  • There is a reported bug: https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/1579652 Please subscribe to check changes on it. – julian-alarcon May 26 '16 at 13:31

6 Answers6

138

A system option was added in snap 2.28 to specify the proxy server.

$ sudo snap set system proxy.http="http://<proxy_addr>:<proxy_port>"
$ sudo snap set system proxy.https="http://<proxy_addr>:<proxy_port>"

Documentation

It works on Ubuntu LTS 14.04 and newer.

SebMa
  • 2,955
Beta Kuang
  • 1,496
29

snapd reads /etc/environment, so setting the usual proxy environment variables there works. On Ubuntu, that's done automatically for you by Settings → Network → Network proxy, so as long as you restart snapd after changing that file you should be set.

Chipaca
  • 10,160
  • 2
    Being more specific, the snapd.service file is located here: /lib/systemd/system/snapd.service – julian-alarcon May 26 '16 at 13:28
  • 1
    @darkhole one shouldn't edit /lib/systemd/system/snapd.service, but use systemctl edit snapd.service. See my answer: http://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services – muru Jun 07 '16 at 13:57
  • 1
    Well what if one wants to automate this configuration? – pmatulis Dec 19 '16 at 16:18
  • 2
    Remember, you will need to restart the snapd service before these changes take effect. – Seth Jan 11 '17 at 22:07
  • 3
    The method you posted here also doesn't work with authentication. Snap should just use the normal $http_proxy and $https_proxy env vars like everyone else. Here is the ubuntu bug tracker link for this problem. – Teque5 Jan 31 '19 at 17:31
  • @Teque5 did you get it working somehow? – Paulo Pedroso Jan 31 '19 at 20:32
  • 1
    @PauloPedroso Haha yes but mostly no. I manually downloaded the snap, extracted the contents since snap wouldn't let me install, mounted the squashfs that was inside, then ran once OK. After that I deleted the whole thing and swore off snaps forever; Flatpaks or docker images are a jillion times better. – Teque5 Feb 01 '19 at 01:51
  • @Teque5 I am out of Skype because of this. I don't believe I have the skills to do that in Skype. I have cursed snap last year because my linux install had problems and snapd wouldn't run. – Paulo Pedroso Feb 01 '19 at 11:45
  • 1
    Teque, Paulo, snapd does use the default environment variables. I don't know what you're saying doesn't work. – Chipaca Mar 19 '19 at 14:49
  • This method does not work in 20.04/snap2.45.3.1 – petertc Aug 31 '20 at 08:05
17

There is another way to add environment variables to systemd services:

Create a folder for the snap daemon and create configuration files for the environment variables:

$ sudo mkdir -p /etc/systemd/system/snapd.service.d/
$ echo -e '[Service]\nEnvironment="http_proxy=http://1.2.3.4:3128/"' \
      | sudo tee /etc/systemd/system/snapd.service.d/http-proxy.conf
$ echo -e '[Service]\nEnvironment="https_proxy=http://1.2.3.4:3128/"' \
      | sudo tee /etc/systemd/system/snapd.service.d/https-proxy.conf
$ sudo systemctl daemon-reload
$ sudo systemctl restart snapd

After that you can check if the environment variables are set for snapd:

$ systemctl show snapd | grep proxy
  Environment=http_proxy=http://1.2.3.4:3128/ https_proxy=http://1.2.3.4:3128/
  DropInPaths=/etc/systemd/system/snapd.service.d/http-proxy.conf /etc/systemd/system/snapd.service.d/https-proxy.conf
Simon Sudler
  • 4,131
9

Snap uses snapd daemon. You only need to define http_proxy and https_proxy in /etc/environment and restart the service: systemctl restart snapd.

user.dz
  • 49,295
mmartin
  • 91
  • But remember if you add the proxy variables to the /etc/environment file, every other application will have this as their default environment as well – placid chat Aug 06 '21 at 07:26
2

Snap service is configured to use special environment file, so you can just add http_proxy variable to it if your current environment variables are not picked up by the snap.

Open file:

sudo vim /etc/sysconfig/snapd

Add:

http_proxy=http://127.0.0.1:3128
https_proxy=http://127.0.0.1:3128
Alex
  • 135
0

Be careful, because the snapd reads the /etc/environment file instead of get the ENV variable. This example below doesn't work:

export https_proxy=http://<your.ip.here>:3128

you have to use:

http://<your.ip.here>:3128