Why Sudo Curl Ignores Proxy Settings?
$ echo $http_proxy


$ curl -v 
* About to connect() to proxy my.proxy.com
# Correct downloading

$ sudo echo $http_proxy


$sudo curl -v 
# Hanging.

Last command doesn't use proxy. Why?

$su
$curl -v 

Is also working correctly.

2 Answers

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

Specify the host as:

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

    $ curl  -x   
    87.98.136.60

    $ curl  
    84.202.82.63

    $ http_proxy= curl   
    87.98.136.60

    $ http_proxy=  curl   
    84.202.82.63

    $ export http_proxy=  curl   
    87.98.136.60

    $ http_proxy=  sudo curl   
    84.202.82.63

    $ export http_proxy=  sudo -E curl   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest