76

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

106

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.

  • 7
    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 8:06
  • Is there a proxy conf file on the user directory? – becko 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. – user692175 Nov 16 '17 at 4:45
25

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 for some reason 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 everytime for all users, then use of these.

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 incidentally apt doesn't obey 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 must be an option to configure proxy settings in the application itself. If not I'm sorry ;)

  • 2
    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
  • 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.) – Curt J. Sampson Sep 6 '18 at 2:08
  • 1
    What is the reason for APT to require additional configuration of proxy? Why doesn't it just obey with /etc/environment? – Loreno Aug 23 at 14:56
23

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
  • 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
  • 2
    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
  • 5
    Then ask your teacher. This is not a "do my homework for me" site. – Dennis Kaarsemaker Feb 17 '13 at 12:51
5

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.

  • That won't help with automated tasks and services invoking Apt without user interaction though. – David Foerster Jun 20 '17 at 17:05
  • Thank you for the notice, I will add your comment to the response. – Frank Qiu Jun 20 '17 at 17:53
3

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.

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.

  • 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 8 at 21:40

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