Commands.page Logo

How to Use Wget with Username and Password on Ubuntu

This guide explains how to authenticate HTTP requests using the wget command-line utility on Ubuntu. You will learn the specific flags required to pass username and password credentials, understand the security risks of command-line arguments, and discover how to use a configuration file for safer automation.

Basic Authentication Syntax

To download a file from a server requiring HTTP Basic Authentication, use the --user and --password flags. Open your terminal and enter the following command structure:

wget --user=YOUR_USERNAME --password=YOUR_PASSWORD https://example.com/file.zip

Replace YOUR_USERNAME and YOUR_PASSWORD with your actual credentials and update the URL to the target resource.

Security Risks and Best Practices

Passing passwords directly in the command line is insecure. Other users on the system can view your password in the process list using commands like ps or top. Additionally, command history may store the plain text password.

For better security, always use HTTPS instead of HTTP to encrypt the traffic between your machine and the server. However, this does not hide the password from local process monitoring.

Using a .netrc File

To avoid exposing passwords in the command line, store credentials in a .netrc file in your home directory.

  1. Create or edit the file: bash nano ~/.netrc

  2. Add the following line: text machine example.com login YOUR_USERNAME password YOUR_PASSWORD

  3. Secure the file permissions so only you can read it: bash chmod 600 ~/.netrc

  4. Run wget without the password flags: bash wget https://example.com/file.zip

Wget will automatically read the credentials from the file when connecting to the specified machine.

Prompt for Password Interactively

If you prefer not to store the password but want to avoid command-line exposure, you can omit the --password flag. Wget will prompt you to enter it securely:

wget --user=YOUR_USERNAME https://example.com/file.zip

Enter the password when prompted. It will not be displayed on the screen or saved in the process list.

Troubleshooting Authentication Errors

If you receive a 401 Unauthorized error, verify the following:

Using these methods ensures you can access protected resources on Ubuntu while maintaining appropriate security standards.