Why sudo curl ignores proxy settings?

5

3

$ echo $http_proxy
http://my.proxy.com

$ curl -v http://files.com/a.txt
* About to connect() to proxy my.proxy.com
# Correct downloading

$ sudo echo $http_proxy
http://my.proxy.com

$sudo curl -v http://files.com/a.txt
# Hanging.

Last command doesn't use proxy. Why?

$su
$curl -v http://files.com/a.txt

Is also working correctly.

Jofsey

Posted 2012-10-27T10:17:48.637

Reputation: 917

Answers

6

This doesn't do what you think it does:

sudo echo $http_proxy

With that, $http_proxy is expanded by the shell before sudo gets called, so it picks up your own environment.

A plain su (without -, -l or --login) also keeps (most of) the environment intact, so the proxy settings are inherited.

sudo does not preserve the environment by default. You could try either:

sudo -E curl ...

(to preserve the whole environment, if you're allowed to do that), or

sudo http_proxy=$http_proxy curl ...

to only pass http_proxy along (safer).

Mat

Posted 2012-10-27T10:17:48.637

Reputation: 6 193

1

Specify the host as:

  • a command line argument (-x)
  • on the command line (var=moo command)
  • or export it to your environment

    $ curl http://icanhazip.com/ -x http://87-98-136-60.ovh.net:80  
    87.98.136.60

    $ curl http://icanhazip.com/ 
    84.202.82.63

    $ http_proxy=http://87-98-136-60.ovh.net:80 curl  http://icanhazip.com/ 
    87.98.136.60

    $ http_proxy=http://87-98-136-60.ovh.net:80;  curl  http://icanhazip.com/ 
    84.202.82.63

    $ export http_proxy=http://87-98-136-60.ovh.net:80;  curl  http://icanhazip.com/ 
    87.98.136.60

    $ http_proxy=http://87-98-136-60.ovh.net:80;  sudo curl  http://icanhazip.com/ 
    84.202.82.63

    $ export http_proxy=http://87-98-136-60.ovh.net:80;  sudo -E curl  http://icanhazip.com/ 

Ярослав Рахматуллин

Posted 2012-10-27T10:17:48.637

Reputation: 9 076