How to Use Wget with Basic Authentication on Ubuntu
This guide explains how to download files using the wget command on Ubuntu when the server requires basic authentication. You will learn the specific command syntax to include your username and password to access protected resources directly from the terminal without needing a browser.
Standard Command Syntax
To download a file from a server that requires basic authentication,
you need to pass your credentials using the --user and
--password flags. The basic structure of the command is as
follows:
wget --user=YOUR_USERNAME --password=YOUR_PASSWORD https://example.com/file.zipReplace YOUR_USERNAME and YOUR_PASSWORD
with your actual credentials and update the URL to point to the file you
wish to download.
Using the Ask Password Option
For better security, you can avoid typing your password directly into
the terminal command line. This prevents your password from being saved
in your bash history. Use the --ask-password flag instead
of specifying the password inline:
wget --user=YOUR_USERNAME --ask-password https://example.com/file.zipWhen you run this command, wget will prompt you to enter the password securely without displaying it on the screen.
Handling Special Characters
If your username or password contains special characters (such as
@, #, or $), you may need to URL
encode them or wrap them in quotes to prevent shell interpretation
errors. For example:
wget --user="[email protected]" --password="p@ssw0rd!" https://example.com/file.zipVerifying the Download
Once the command executes, wget will attempt to connect to the server
using the provided credentials. If the authentication is successful, the
file will begin downloading to your current directory. If authentication
fails, wget will return a 401 Unauthorized error indicating
the credentials were incorrect.