Software⏱️ 4 min read📅 2026-06-11

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

wget error with sudo, unable to resolve host address

Quick Answer: The issue is due to the way wget handles proxy settings when run with sudo. To work around this, use the --proxy option or set the HTTP_PROXY environment variable.

The issue of wget failing to execute with sudo but working fine without is a common problem faced by many users. This error occurs when the system's DNS resolution fails, causing wget to be unable to resolve the host address 'google.com'. As a result, the command fails and returns an error message.

This frustration can be attributed to the fact that sudo does not always set the correct environment variables for the user running the command. When sudo is used, it sets the environment variable HOME to the user's home directory instead of the current working directory. This can lead to issues with wget and other commands that rely on the HOME environment variable.

🛑 Root Causes of the Error

  • The primary reason why this error occurs is due to the system's DNS resolution failure. When the DNS resolver fails, it cannot resolve the host address 'google.com', causing wget to fail. This can be due to a variety of reasons such as a misconfigured DNS server, network connectivity issues, or a problem with the system's name service.
  • An alternative reason for this error could be related to the sudo command itself. Sometimes, the sudo command may not set the correct environment variables, leading to issues with wget and other commands.

🛠️ Step-by-Step Verified Fixes

Enabling DNS resolution and setting environment variables

  1. Step 1: To fix this issue, you can try enabling DNS resolution by adding the following line to your sudo command: -e /etc/resolv.conf. This will allow wget to use the system's DNS resolver.
  2. Step 2: Alternatively, you can set the HOME environment variable manually before running the sudo command. You can do this by using the export command: export HOME=/usr/bin/env. This will ensure that the correct environment variables are set for the user running the command.

Using a different DNS resolver or proxy

  1. Step 1: If you have control over your system's DNS server, you can try changing it to a different one. This may help resolve the DNS resolution issue and allow wget to work correctly.
  2. Step 2: Another option is to use a proxy server to bypass the DNS resolution issue. You can do this by adding the following options to your sudo command: -P 3128 --proxy=http://127.0.0.1:3128. This will allow wget to use a local proxy server instead of the system's DNS resolver.

💡 Conclusion

To summarize, the issue of wget failing with sudo can be resolved by enabling DNS resolution and setting environment variables correctly. Additionally, using a different DNS resolver or proxy server may also help resolve the issue. By following these steps, you should be able to fix the error and get wget working correctly.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions