Proxy does not work using sudo

8,356

Solution 1

In case anybody wonders:

The issue here, is that when sudoing, a newenvironment is loaded, meaning that http_proxy is no longer set.

To resolve this issue you can add Defaults env_keep += "http_proxy https_proxy" to your /etc/sudoers file in order to keep those variables when switching to the secure path.

Reference: https://help.ubuntu.com/community/EnvironmentVariables

Solution 2

Open /etc/apt/apt.conf file in Terminal and set your proxy setting as following:

Acquire::http::proxy "http://user:pass@host:port/";
Acquire::https::proxy "https://user:pass@host:port/";
Acquire::ftp::proxy "ftp://user:pass@host:port/";
Acquire::socks::proxy "socks://user:pass@host:port/";

Replace user with your username, pass with your password, host with proxy-webaddress and port with a portnumber.

Share:
8,356

Related videos on Youtube

Michele
Author by

Michele

Recently moved to an IT consultancy position after having spent 6 years studying condensed matter physics.

Updated on September 18, 2022

Comments

  • Michele
    Michele over 1 year

    I set the proxy using system setting -> Network -> Network Proxy. I then clicked on "Apply system wide", put my password, and nothing else.

    michele@rserver-VirtualBox:~$ wget http://bbc.co.uk
    --2014-09-30 13:02:20--  http://bbc.co.uk/
    Resolving datapx01-wcg (datapx01-wcg)... 10.123.7.31
    Connecting to datapx01-wcg (datapx01-wcg)|10.123.7.31|:8080... connected.
    Proxy request sent, awaiting response... 301 Moved Permanently
    Location: http://www.bbc.co.uk/ [following]
    --2014-09-30 13:02:21--  http://www.bbc.co.uk/
    Reusing existing connection to datapx01-wcg:8080.
    Proxy request sent, awaiting response... 200 OK
    Length: 103020 (101K) [text/html]
    Saving to: ‘index.html’
    
    100%[======================================>] 103,020      190KB/s   in 0.5s   
    
    2014-09-30 13:02:21 (190 KB/s) - ‘index.html’ saved [103020/103020]
    

    Instead using sudo:

    michele@rserver-VirtualBox:~$ sudo wget http://bbc.co.uk
    [sudo] password for michele: 
    --2014-09-30 13:02:30--  http://bbc.co.uk/
    Resolving bbc.co.uk (bbc.co.uk)... 212.58.246.104, 212.58.244.20, 212.58.246.103, ...
    Connecting to bbc.co.uk (bbc.co.uk)|212.58.246.104|:80... failed: Connection timed out.
    

    I am on 14.04 and I didn't have this issue before upgrading from 12.04 yesterday. Please tell me what other info you need.

    • αғsнιη
      αғsнιη over 9 years
      Why do you want to use sudo here while it works well without that?
    • αғsнιη
      αғsнιη over 9 years
      Check your /etc/apt/apt.conf file and see if your proxy setting is also appear there.
    • Michele
      Michele over 9 years
      that is simply a reproducible example... my server runs application on behalf of different users, so I need the proxy to work for all users.
    • s3lph
      s3lph over 9 years
      To workaround, use it like this: sudo env HTTP_PROXY=http://proxy.example.com:port wget http://bbc.co.uk.
    • Lety
      Lety over 9 years
      Did you try @the_Seppi's suggestion? When you run sudo, environment is restricted.
    • Michele
      Michele over 9 years
      @Letizia yes, it did not work
    • Lety
      Lety over 9 years
      Did you try to configure and uncomment http_proxy in /etc/wgetrc file?
    • Jones03
      Jones03 about 8 years
      You need to make some changes in your /etc/sudoers file. Take a look at this: ubuntuforums.org/showthread.php?t=2199416.
  • Michele
    Michele over 9 years
    Those 4 lines are already there. But without the username and password. I ll try adding them
  • Michele
    Michele over 9 years
    Just did, still the same
  • αғsнιη
    αғsнιη over 9 years
    @Michele run echo $http_proxy in terminal did you see the your user and pass and other configure?
  • Michele
    Michele over 9 years
    I just get the http://proxy:port/
  • αғsнιη
    αғsнιη over 9 years
    @Michele then your user and pass not set correctly!!
  • Michele
    Michele over 9 years
    triple checked they are fine
  • αғsнιη
    αғsнιη over 9 years
    did you restart the system
  • Michele
    Michele over 9 years
    No, I thought it was a Windows thing... :-) I'll try now
  • Michele
    Michele over 9 years
    Nada... still the same. I just read that sudo does a reset of the environment variables and things like http_proxy don't get used
  • αғsнιη
    αғsнιη over 9 years
    @Michele Sorry but I don't have any idea check here askubuntu.com/questions/175172/… and here askubuntu.com/questions/15719/… and also here askubuntu.com/questions/82880/…
  • Ubuntuser
    Ubuntuser about 6 years
    super answer!! worked in CentOS 7 as well.
  • rbaleksandar
    rbaleksandar about 2 years
    Just a note: this would still requires the respective environment variables to be set inside the profile of the user, who belongs to the sudoers. Add export http_proxy=... and export https_proxy=... to .bashrc or whatever your user's profile file is called. Also I recommend putting no_proxy, which is quite common for company networks, where internal network locations (e.g. intranet web portal) often not only do not require but will not work if going through proxy.