121

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).

Pablo Bianchi
  • 15,657
lapots
  • 1,323
  • There is a change in proxy settings to get work in corporate proxy in ubuntu visit https://github.com/navinas33/docker_ubuntu.git it works... – Navin a.s Jan 12 '17 at 06:32

7 Answers7

162

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.

laurent
  • 6,779
  • 17
    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 '17 at 08:06
  • Is there a proxy conf file on the user directory? – a06e Feb 15 '17 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. –  Nov 16 '17 at 04:45
56

This is a more general answer along with apt config.

As there are 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/

To apply user-wide use ~/.bashrc.

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 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.

Pablo Bianchi
  • 15,657
  • 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 '17 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 06 '18 at 02:08
  • 4
    What is the reason for APT to require additional configuration of proxy? Why doesn't it just obey with /etc/environment? – mnj Aug 23 '19 at 14:56
  • 1
    @cjs agreed, the 's' in https:// shouldn't be there. – Tom Hundt Jul 23 '20 at 01:07
25

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/";
  • Is there is difference what port and ip to use? – lapots Feb 17 '13 at 12:36
  • 1
    Well, yes, you need to put in the correct details for the proxy you want to use of course. – Dennis Kaarsemaker Feb 17 '13 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 '13 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? – Dennis Kaarsemaker Feb 17 '13 at 12:47
  • It's my university's laboratory work(simple task). I can configure dhcp, dns, ssh but I don't know how to configure proxy, what ip and port to use. – lapots Feb 17 '13 at 12:49
  • 7
    Then ask your teacher. This is not a "do my homework for me" site. – Dennis Kaarsemaker Feb 17 '13 at 12:51
  • @DennisKaarsemaker Why do you assume that getting a Linux machine to work in a university lab environment is a "homework assignment"? It's just as likely that this is not a class on Linux network configuration and he's just trying to use Linux in an environment where most people use Windows and things "just work." In which case, your response is explicitly discouraging people from using Linux instead of Windows. – cjs Jul 23 '20 at 06:10
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.

Frank Qiu
  • 141
7

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.

abu_bua
  • 10,783
j-hap
  • 171
  • /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... – Gert van den Berg Jan 21 '20 at 08:23
3

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.

Pablo Bianchi
  • 15,657
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.

Videonauth
  • 33,355
  • 17
  • 105
  • 120
Neo
  • 17
  • 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. – Tomasz Gandor Jan 08 '19 at 21:40