111

I need to configure proxy. There wasn't apt.conf file in /etc/apt/ so I've created it using nano.

But I don't understand what I need to write there. I've written http_proxy = http://lgn:pwd@192.168.1.254:8080/ there and then restarted networking. But when I try to apt-get update - it doesn't work.

Actually, I don't understand what port and IP I should write in apt.conf file (there was example with 192.168.1.254 IP and 8080 port, so I decided to use them).

1

7 Answers 7

146

To use a proxy, you need a proxy server. The IP and port have to be from this proxy server. Login and pwd must be your user and password on the proxy server (if the proxy requires login).

From help.ubuntu.com site:

APT configuration file method

This method uses the apt.conf file which is found in your /etc/apt/ directory. This method is useful if you only want apt-get (and not other applications) to use a http-proxy permanently.

On some installations there will be no apt-conf file set up. Edit apt-conf file (or create a new one if you have no one yet) using the editor of your choice.

sudo nano /etc/apt/apt.conf

Add this line to your /etc/apt/apt.conf file (substitute your details for yourproxyaddress and proxyport).

Acquire::http::Proxy "http://yourproxyaddress:proxyport";

Save the apt.conf file.

If your proxy needs a login/password, substitute:

"http://yourproxyaddress:proxyport";

with:

"http://username:password@yourproxyaddress:proxyport";

using username and password from the proxy server.

3
  • 15
    Adding a separate local file in /etc/apt/apt.conf.d/ is preferrable so you don't have to edit the system's basic configuration files. That way, if they are updated in a subsequent package version, you don't have to merge in your local changes by hand.
    – tripleee
    Jan 27, 2017 at 8:06
  • Is there a proxy conf file on the user directory?
    – a06e
    Feb 15, 2017 at 14:32
  • This answer needs an update: gksudo is obsolete. Consider suggesting other non-GUI editor or sudo -H instead of gksudo if using gedit or similar.
    – user692175
    Nov 16, 2017 at 4:45
53

This is a more general answer along with apt config.

As there a lot of places to configure proxy settings, it might get confusing at beginning. Let me summarize some things and suggest some good practices.

For console programs

Ex: wget, git and almost every console application which connects to internet.

If you want to configure proxy every time you run your commands then set the environment variables using following commands.

export http_proxy=http://DOMAIN\USERNAME:PASSWORD@SERVER:PORT/
export ftp_proxy=http://DOMAIN\USERNAME:PASSWORD@SERVER:PORT/

Else if you want to use the same settings every time for all users, then:

Configure in bashrc

$ nano /etc/bash.bashrc
export http_proxy=http://DOMAIN\USERNAME:PASSWORD@SERVER:PORT/
export ftp_proxy=http://DOMAIN\USERNAME:PASSWORD@SERVER:PORT/

Configure in /etc/environment

$ nano /etc/environment
https_proxy="http://myproxy.server.com:8080/" 
ftp_proxy="http://myproxy.server.com:8080/" ...

Configure using GUI

Open the network settings and set the your system wide network proxy.

Network -> Network proxy -> Configure -> Apply system wide.

But this might not be useful if you have authentication for the proxy.

For apt.

You need to do some extra work for apt, as apt doesn't care about the proxy environment variables.

$ nano /etc/apt/apt.conf
Acquire::http::Proxy "http://USERNAME:PASSWORD@SERVER:PORT";
Acquire::https::Proxy "https://USERNAME:PASSWORD@SERVER:PORT";

For everything out of this scope, there will likely be an option to configure proxy settings in the application itself.

4
  • 4
    the config in /etc/environment is very important because if you just setup the http_proxy var for your user, when you go into sudo mode (for stuff like 'sudo apt-get update'), the terminal usually does not preserve the current environment vars ; very important to test your settings : sudo echo $http_proxy
    – belun
    Jun 28, 2017 at 17:51
  • 1
    HTTP_PROXY (in upper case) appears to be the standard name for that variable, though many programs will fall back to the lower case version. https_proxy (note lower case) is the correct capitalisation for that, but you'd never use it alone, as you do in your /etc/environment above. (I think the s is a typo.)
    – cjs
    Sep 6, 2018 at 2:08
  • 3
    What is the reason for APT to require additional configuration of proxy? Why doesn't it just obey with /etc/environment?
    – mnj
    Aug 23, 2019 at 14:56
  • 1
    @cjs agreed, the 's' in https:// shouldn't be there.
    – Tom Hundt
    Jul 23, 2020 at 1:07
24

Something like the following should work:

Acquire::http::proxy "http://lgn:pwd@192.168.1.254:8080/";
Acquire::https::proxy "http://lgn:pwd@192.168.1.254:8080/";
7
  • Is there is difference what port and ip to use?
    – lapots
    Feb 17, 2013 at 12:36
  • 1
    Well, yes, you need to put in the correct details for the proxy you want to use of course. Feb 17, 2013 at 12:37
  • er...ip - it is router's ip? I mean this ip I used from example but all router's setting I configure in browser using 192.168.1.1 ip. I should use .1? and login and password should be ubuntu's root or router's?
    – lapots
    Feb 17, 2013 at 12:40
  • 5
    Do you even know what you're doing? How do you know you need to use a proxy if you don't know the details for it? Feb 17, 2013 at 12:47
  • 7
    Then ask your teacher. This is not a "do my homework for me" site. Feb 17, 2013 at 12:51
14

What you can do is also try to run command with sudo -E, which will take the environment settings. It seems to work for me when I try to update, add-apt-repository, and install. Example:

sudo -E add-apt-repository ppa:xxxxx

According to @David Foerster this only works for manual interaction using shell, but it won't work on automated tasks and services invoking Apt without user interaction.

3
  • That won't help with automated tasks and services invoking Apt without user interaction though. Jun 20, 2017 at 17:05
  • Thank you for the notice, I will add your comment to the response.
    – Frank Qiu
    Jun 20, 2017 at 17:53
  • This solved it for me!
    – Elad Avron
    Nov 28, 2022 at 7:39
6

If you need to use apt behind a proxy, probably everything else also needs the proxy config. I like to manage my proxy settings in one place, which would be the environment variable.

For that I create a /etc/profiles.d/99-proxy.sh, that contains the necessary proxy settings (like larent described):

export http_proxy=http://DOMAIN\USERNAME:PASSWORD@SERVER:PORT/
export https_proxy=$http_proxy
export ftps_proxy=$http_proxy

that way, all users on that machine have access to the proxy. (don't forget chmod a+x)

When you use apt or apt-get as a user, that user is able to use sudo. Like Frank Qiu described, sudo does not pass environment variables. For that you can add

Defaults env_keep += "http_proxy https_proxy ftp_proxy"

to (e.g.)

/etc/sudoers.d/00-environment 

or directly into the

/etc/sudoers

file. That way you only have to manage the proxy settings in one place.

1
  • /etc/environment or whatever other file PAM (pam_env module) use to set up the environment is more reliable - it does not need a shell to start... Jan 21, 2020 at 8:23
1

If you want to set a proxy just in one case, you can use [-o=config_string]:

apt -o acquire::http::proxy="http://yourproxyaddress:proxyport"

Note the =. The option is case-insensitive.

Also, if you have repos using https on your sources.list you may want to add -o acquire::https::proxy=false.

Already mentioned here.

0

The original question might be in wrong direction. I believe you want to setup a proxy server in Linux ?

Well that is completely different than wanting to use a existing proxy from Linux environment.

If your question is the first one then use any third party services to configure a proxy server in Linux. An example could be squid.

Simple commands to install squid:

$ sudo apt-get install squid3

Then edit the configuration to allow the Local Net (in this case your own machine). $ sudo vi /etc/squid3/squid.conf

Add/Edit the following lines:

http_access allow local_net
acl local_net src 192.168.1.0/255.255.255.0

Assuming your IP for the Linux machine is in 192.168.1/24 block.

1
  • Probably most readers of this question already installed squid ;), also set the http[s]_proxy ENV variables, and now wonder, why APT still doesn't work. But if you're getting HTTP 403 from everything except the machine with squid - do look once again for all occurences of the word deny which are not commented. (especially with http_access on the same line. Jan 8, 2019 at 21:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.