How To Use wget With Username and Password for FTP / HTTP File Retrieval

How do I use GNU wget FTP or HTTP client tool to download files from password protected web pages on Linux or Unix-like system? Is there a way to download a file using username and password from a config file?

The gnu wget command supports username and password combo for both FTP and HTTP file retrieval.

Syntax

The syntax is:
wget options url
wget --user=NAME --password='PASSWORD' url
wget --user=NAME --password='PASSWORD' ftp://url/path/file.name
wget --user=NAME --password='PASSWORD' http://url/path/file.name

Where,

  1. --user=userNameHere: Your FTP/HTTP username.
  2. --password=passWordHere: Your HTTP/FTP password.

Specify the username user and password password for both FTP and HTTP file retrieval. These parameters can be overridden using the --ftp-user and --ftp-password options for FTP connections and the --http-user and --http-password options for HTTP connections. To download a file called foo.pdf from theos.in domain, enter:
$ wget --user=vivek --password='myPassword' http://theos.in/protected/area/foo.pdf
OR
wget --user=vivek --ask-password http://192.168.1.10/docs/foo.pdf
Sample outputs:

Fig.01: wget command in action with HTTP auth.

Ask for a password

The passwords on the command line are visible using ps command. This is a security feature. Pass the --ask-password option to pompt for a password for each connection established. Cannot be specified when --password is being used, because they are mutually exclusive:
$ wget --user=vivek --ask-password http://theos.in/protected/area/foo.pdf

How do I read username and password from a file?

This is a security feature. Create a ~/.wgetrc file. The syntax is as follows to store login credentials:

$ cat ~/.wgetrc
user=vivek
password=myCoolPasswordhere

Of course, you can configure separate FTP and HTTP credentials as follows too:

$ cat ~/.wgetrc
ftp_user=vivekftp
ftp_password=myCoolFTPPassword
http_user=vivekhttp
http_password=myCoolHTTPPassword

Make sure only you can read the ~/.wgetrc file:
$ chmod 0400 ~/.wgetrc
$ chown vivek:vivek ~/.wgetrc

Please note that storing unencrypted passwords is also considered a security risk.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.