127

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
  • 17,371
lapots
  • 1,383

7 Answers7

167

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,899
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
  • 17,371
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/";
15

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
  • 151
8

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
  • 11,313
j-hap
  • 181
4

Just once

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
  • 17,371
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,815
Neo
  • 17