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.

ADVERTISEMENTS

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.

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
31 comments… add one
  • lakshminarayanan May 14, 2008 @ 13:17

    wow…that was cool!

  • chadido Aug 8, 2009 @ 7:36

    thats great u saved me

  • Mauricio Hernandez Aug 21, 2009 @ 3:21

    Could this be used for rapidshare?

  • Vagin Aug 24, 2009 @ 0:49

    Didn’t work for me. I am on a windows machine. Not sure if that makes a diffrence.

  • iqueen Nov 18, 2009 @ 11:35

    gnuwin32.sf.net
    wget 1.12
    –ftp-user=
    –ftp-password=

  • Asterisk Jan 12, 2010 @ 9:30

    Works Like a charm!!!
    tx.

  • meitham Jul 10, 2010 @ 12:40

    I need to download a file from my server on another machine with wget, using -p password as an argument is not an option since people could simply find my password with a ps command! Do you know anyway I could force wget to ask for my password after typing the command? like when do mysql -u root -p and it will ask you for the root password?
    thanks

  • Rahman Aug 25, 2010 @ 6:03

    Works Like a charm!!!
    tx. [2]

  • Tony Palmer Nov 2, 2010 @ 16:04

    wget does not work directly if the site redirects to form based login page, i.e. no HTTP 401 returned

  • Mohamed Jameer Mar 24, 2011 @ 7:24

    Its Working fine, Thx a lots..

  • Chris Shennan Aug 4, 2011 @ 9:09

    As easy at that! This made downloading a large XML file much easier than using a browser which ended up crashing 9 out of 10 times.

    Many thanks.

    Chris.

  • Reddy Siva Saran K Jan 18, 2012 @ 4:39

    I tried the following command which gave a Error Response
    The Response is
    Resolving cambuild.qalab.cam.novell.com… 164.99.129.35
    Connecting to cambuild.qalab.cam.novell.com|164.99.129.35|:80… connected.
    HTTP request sent, awaiting response… 403 Forbidden
    2012-01-18 10:07:51 ERROR 403: Forbidden.

    Have any one faced a similar issue and if yes can you let me know how to come out from this

  • John Mar 19, 2012 @ 13:04

    I have the same problem. Any ideas on how to fix?

  • Josh May 13, 2012 @ 23:12

    Thank you so much, this was exactly what I needed, and it works wonderfully!

  • Pawan Jul 24, 2012 @ 10:23

    Hi Reddy,
    You must have got your answer by now, posting info hopeing it would help others

    Connecting to cambuild.qalab.cam.novell.com|164.99.129.35|:80… connected.
    HTTP request sent, awaiting response… 403 Forbidden
    2012-01-18 10:07:51 ERROR 403: Forbidden.

    If you see the error above, it show your machine succesfully connected to remote host on port 80, however could not find the page you have requsted for403 Forbidden.
    Hope this clarify.

    • fsilva Jun 26, 2014 @ 18:37

      This doesnt mean the page could not be found that’s a 404 error. This mean the user who attempted to login does not have the permission to access any further. Therefore “Forbidden” request error is thrown.

  • sujith Aug 28, 2012 @ 10:05

    Working Perfectly Thanks a lot

  • Parthiban Sep 17, 2012 @ 9:30

    I want to download two of three files (file-abc.zip, file-xyz.zip, sample.zip) from a ftp server. How can I use wget like————- file*.zip in a shell script.

  • kelvin kalaria Oct 15, 2012 @ 15:26

    very very useful command for me

    Thanks a ton..

  • Emile Jul 10, 2013 @ 10:00

    You can also use this:

    wget ftp://userName:passWord@wwww.example.com/file.pdf
  • Nabil Sep 6, 2013 @ 19:39

    Add a space before the command to prevent the command from being stored on your bash history.

  • Tyzoid Sep 20, 2013 @ 20:28

    To protect yourself from having your password in your terminal history, you can use the following:

    read -s q; wget http://example.com/path/to/file --user=username --password="$q";
    • Tyzoid Sep 20, 2013 @ 20:30

      Remember to set q to null again afterwards!

      q="";
      • donnie Aug 16, 2015 @ 1:46

        ……….cute

  • امیر محمد Oct 1, 2013 @ 13:44

    واقعا ممنون به دردم خورد

    thanks a lot very Nice :x

  • Matt Nov 20, 2014 @ 6:18

    Thank you for this :-) previously I had to use HTTrack, this is much easier

  • Barun Aug 23, 2015 @ 12:26

    wonderful!!! thanx bro..

  • Mohit Pal Mar 30, 2016 @ 9:57

    I want to execute this command using an automated process. Is there any way to hide or encrypt password?

  • Martin Jul 21, 2017 @ 13:33

    Hi Vivek,

    I’m completely new to Linux, and I’m setting up a development server at home using Ubuntu Server. Hours of, erm … fun? Anyway I found this really helpful.

    Many thanks

    Martin

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.