How to Authenticate with FTP Using wget?
This article provides a quick overview and step-by-step guide on how
to authenticate with an FTP server using the wget
command-line utility. You will learn the standard syntax for passing
credentials, how to handle special characters in passwords, and how to
securely connect without leaving your password in your terminal
history.
Standard FTP Authentication Syntax
The most straightforward way to authenticate with an FTP server using
wget is to include the username and password directly
within the URL. The standard format is as follows:
wget ftp://username:password@ftp.example.com/filename.txtAlternatively, you can use explicit command-line flags to separate your credentials from the URL, which can make the command easier to read:
wget --ftp-user=username --ftp-password=password ftp://ftp.example.com/filename.txtHandling Special Characters in Passwords
If your FTP password contains special characters (such as
@, :, /, or !),
including them directly in the URL can break the command syntax. To
resolve this, you must URL-encode the special characters. For example,
if your password is P@ss!, you would replace the
@ with %40 and the ! with
%21.
Using the --ftp-password flag instead of embedding the
password in the URL often bypasses the need for URL encoding, making it
the preferred method for complex passwords.
Secure Authentication Methods
Specifying your password directly in the terminal leaves it visible in your shell history. To authenticate more securely, you can use the following methods:
1. Prompting for the Password
If you omit the password flag, wget will automatically
prompt you to enter it securely without echoing the characters on the
screen:
wget --ftp-user=username ftp://ftp.example.com/filename.txt2. Using a .netrc File
For automated scripts, you can store your credentials in a private
file named .netrc in your home directory. wget
will automatically look for this file to authenticate.
Add the following lines to your ~/.netrc file:
machine ftp.example.com
login username
password your_secure_password
Make sure to restrict the file permissions so other users cannot read it:
chmod 600 ~/.netrcOnce configured, you can download files by simply referencing the URL:
wget ftp://ftp.example.com/filename.txt