Sudo can't resolve hostname

2

$ 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

Juve

Posted 2011-11-25T13:02:09.447

Reputation: 336

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 – 2011-11-25T13:12:47.210

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 – 2011-11-25T13:44:09.737

Answers

2

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.

Paul

Posted 2011-11-25T13:02:09.447

Reputation: 52 173

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 – 2011-11-25T13:45:05.537