Why does wget give an error when executed with sudo, but works fine without?

h0ch5tr4355

I tried the following command:

$ wget -q --tries=10 --timeout=20 --spider http://google.com

(From this SO post. I want to check my internet connection in bash.)

I get following output:

Spider mode enabled. Check if remote file exists.
--2015-09-28 09:55:50--  http://google.com/
Connecting to 127.0.0.1:3128... connected.
Proxy request sent, awaiting response... 302 Found
Location: http://www.google.de/?gfe_rd=cr&ei=k_IIVreaN-yH8Qfe1Yu4CA [following]
Spider mode enabled. Check if remote file exists.
--2015-09-28 09:55:50--  http://www.google.de/?gfe_rd=cr&ei=k_IIVreaN-yH8Qfe1Yu4CA
Connecting to 127.0.0.1:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.

Seems OK, however running the cmd with sudo, I receive this:

Spider mode enabled. Check if remote file exists.
--2015-09-28 09:55:27--  http://google.com/
Resolving google.com (google.com)... failed: Name or service not known.
wget: unable to resolve host address ‘google.com’

I need this line in a script, which I call with sudo and so it always fails.

Can somebody tell me the reason for this? How can I work around that?

Deltik

You have a proxy defined in your environment. Yours appears to be 127.0.0.1:3128.

When you run sudo, the proxy environment variable isn't passed, which is why you can't directly resolve google.com.

You can see what proxy/proxies you have defined in your environment variables with this command:

env | grep proxy

Additional information on Ask Ubuntu

Note: If you want sudo to pass the HTTP proxy environment variable, try this:

sudo http_proxy="$http_proxy" wget -q --tries=10 --timeout=20 --spider http://google.com

You can also pass all environment variables using sudo -E:

sudo -E wget -q --tries=10 --timeout=20 --spider http://google.com

Stack Overflow has other options for keeping the environment variable when sudoing.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does OS X `install` errors on redirected input when the same version of `install` on linux works fine?

분류에서Dev

Why gcc does not give a warning or error when invalid mode is used in fopen()?

분류에서Dev

Twitter doesn't give access on trying to post tweet for second time without exiting fragment but works fine on posting tweet for first time

분류에서Dev

Why does print with and without parentheses give __str__() and __repr__() in

분류에서Dev

Sed command does not execute correctly when called from bash script and works fine when called from prompt

분류에서Dev

Why does "sbt run" fail with OutOfMemoryError while "activator run" works fine?

분류에서Dev

Why does ssh look for keys in /root/.ssh when run with sudo?

분류에서Dev

Why does the below code run without an error when both overlapping fields are used?

분류에서Dev

Script in ~/.bashrc works when manually executed, but not otherwise

분류에서Dev

Why does synchronized(this) works?

분류에서Dev

Why does QuickCheck give up?

분류에서Dev

Why does this code give a warning?

분류에서Dev

sudo: unable to resolve host error but still works

분류에서Dev

Why does sudo not work with curl?

분류에서Dev

Does `sudo halt' give bash a chance to write its history?

분류에서Dev

C++ probability calculating program works when bookmarked in Visual Studio but does not work when run without a bookmark

분류에서Dev

Why does updating Oh My Zsh give me an error about rebasing with unstaged changes?

분류에서Dev

500 Internal Server Error, but works fine on localhost - $mysqli = new mysqli

분류에서Dev

403 error only in chrome, works fine in FF and IE

분류에서Dev

Why does renewing an optimizer give a bad result?

분류에서Dev

Why does renewing an optimizer give a bad result?

분류에서Dev

Why this Ansi C program does not give result?

분류에서Dev

Why does the "which" command give duplicate results?

분류에서Dev

Why does unwrap not give a monotonic phase?

분류에서Dev

Why do I get an "Operation not permitted" error when running duplicity as sudo?

분류에서Dev

Why does my import statement have a syntax error in Deno but works ok in Nodejs?

분류에서Dev

Why does this line of GLSL for calculating distance produce inconsistent results, when this one works as expected?

분류에서Dev

DNS works with host, but not with wget

분류에서Dev

Why does this compound command {...} not exit on error, when used with ||?

Related 관련 기사

  1. 1

    Why does OS X `install` errors on redirected input when the same version of `install` on linux works fine?

  2. 2

    Why gcc does not give a warning or error when invalid mode is used in fopen()?

  3. 3

    Twitter doesn't give access on trying to post tweet for second time without exiting fragment but works fine on posting tweet for first time

  4. 4

    Why does print with and without parentheses give __str__() and __repr__() in

  5. 5

    Sed command does not execute correctly when called from bash script and works fine when called from prompt

  6. 6

    Why does "sbt run" fail with OutOfMemoryError while "activator run" works fine?

  7. 7

    Why does ssh look for keys in /root/.ssh when run with sudo?

  8. 8

    Why does the below code run without an error when both overlapping fields are used?

  9. 9

    Script in ~/.bashrc works when manually executed, but not otherwise

  10. 10

    Why does synchronized(this) works?

  11. 11

    Why does QuickCheck give up?

  12. 12

    Why does this code give a warning?

  13. 13

    sudo: unable to resolve host error but still works

  14. 14

    Why does sudo not work with curl?

  15. 15

    Does `sudo halt' give bash a chance to write its history?

  16. 16

    C++ probability calculating program works when bookmarked in Visual Studio but does not work when run without a bookmark

  17. 17

    Why does updating Oh My Zsh give me an error about rebasing with unstaged changes?

  18. 18

    500 Internal Server Error, but works fine on localhost - $mysqli = new mysqli

  19. 19

    403 error only in chrome, works fine in FF and IE

  20. 20

    Why does renewing an optimizer give a bad result?

  21. 21

    Why does renewing an optimizer give a bad result?

  22. 22

    Why this Ansi C program does not give result?

  23. 23

    Why does the "which" command give duplicate results?

  24. 24

    Why does unwrap not give a monotonic phase?

  25. 25

    Why do I get an "Operation not permitted" error when running duplicity as sudo?

  26. 26

    Why does my import statement have a syntax error in Deno but works ok in Nodejs?

  27. 27

    Why does this line of GLSL for calculating distance produce inconsistent results, when this one works as expected?

  28. 28

    DNS works with host, but not with wget

  29. 29

    Why does this compound command {...} not exit on error, when used with ||?

뜨겁다태그

보관