Is it Secure to Pass Passwords in a wget Command?

Passing passwords directly within a wget command line is highly insecure and exposes sensitive credentials to multiple security risks. While wget allows users to include usernames and passwords directly in the command string using parameters like --password or via URL embedding, this practice leaves credentials visible to other users on the system, stores them in plaintext command histories, and potentially leaks them through process monitoring tools. This article explores why this method is unsafe, where the specific vulnerabilities lie, and the best practices for securely handling authentication when downloading files.

The Risks of Using Passwords in Command Lines

When you execute a wget command with a plaintext password, the credential travels through several local systems before the network request is even made. This creates a wide attack surface for local users and malicious software.

Secure Alternatives for wget Authentication

To avoid exposing your credentials, you should utilize wget’s built-in mechanisms designed to handle sensitive data securely.

1. Use a .netrc File

The safest way to pass credentials to wget is by using a .netrc file located in your home directory. This file stores auto-login information for various accounts and is read automatically by wget.

To set it up securely, create or edit the file ~/.netrc and add your credentials:

machine example.com
login your_username
password your_password

After creating the file, you must restrict its permissions so that only your user can read it:

chmod 600 ~/.netrc

2. Prompt for Password Interactively

If you are running the command manually, you can use the --ask-password flag. This forces wget to prompt you for the password securely after the command is launched, ensuring it never enters the process argument list or shell history.

wget --user=your_username --ask-password https://example.com/file.zip

3. Use Environment Variables safely

While passing environment variables directly in the command line can still leak info, reading a password from a secure variable or restricted file descriptor at runtime is vastly preferable to hardcoding it into the string.