Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
$ curl http://google.de  # DNS resolution works fine
...
$ sudo curl http://google.de
curl: (6) Couldn't resolve host 'google.de'

$ echo $http_proxy
http://proxy:8080

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

$ hostname
debian

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
add comment

1 Answer

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 http://google.de

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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