Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
$ curl  # DNS resolution works fine
$ sudo curl
curl: (6) Couldn't resolve host ''

$ echo $http_proxy

$ cat /etc/hosts   localhost.localdomain localhost   debian
# ip6-stuff below

$ hostname

I am using Debian squeeze (LXDE) inside VirtualBox and can do admin stuff via a real root console or via su root. I added my user to the sudoers group and can do stuff like editing via sudo, e.g., sudo vim /etc/hosts. I'd like to use sudo instead of su root.

Why are curl (and other tools like aptitude) not able to resolve the hostnames, but when using them as normal user or plain root everything works fine?

Edit: Here is the solution (Thx to Paul):

$ sudo visudo
# add this line
Defaults        env_keep = "http_proxy https_proxy ftp_proxy"
# above this line
Defaults        env_reset
share|improve this question
I would first try either setting http_variable in root's environment or using curl's -x option to use proxy while using sudo. -x, --proxy <[protocol://][user@password]proxyhost[:port]> – Sachin Divekar Nov 25 '11 at 13:12
I added the http_* var in /etc/bash.bashrc, but Pauls solution also worked. Do I also have to set such var in /etc/environment? – Juve Nov 25 '11 at 13:44
up vote 2 down vote accepted

When you are running sudo, you are running a new shell as root, in which the command is executed. Only the environment variables in the /etc/sudoers file are copied into the new shell.

To get around this for one command you can do

sudo env http_proxy=$http_proxy curl

Or you can edit /etc/sudoers (use visudo!) and add

Defaults     env_keep = http_proxy

This will pass the variable through automatically.

share|improve this answer
I changed the settings via visudo (<-- nice!!) and added Defaults env_keep = "http_proxy https_proxy ftp_proxy" above Defaults env_reset. Thx, this works fine now – Juve Nov 25 '11 at 13:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .