
On a Red Hat distribution (CentOS, Fedora, Red Hat), the dnf install or yum install command can be used to install httpd-tools which includes the htpasswd command.
dnf install httpd-tools
The htpasswd command with the -c option is used to create a or overwrite file that will contain users htpasswd. A single file can contain multiple users htpasswd, so it usually makes sense to place the file in some shared location, such as /usr/local/share. You will be prompted to provide the users password.
AVOID TROUBLE
If the htpasswd file already exists, the -c option will overwrite the file. Do not use the -c option if the file already exists and you want to append a new user to the file.
~]# htpasswd -c /usr/local/share/users.htpasswd john.doe
New password:
Re-type new password:
Adding password for user john.doe
The -B option can be used to create an encrypted, hashed password.
htpasswd -B -c /usr/local/share/users.htpasswd john.doe
The -b (batch mode) option can be used to include the password on the command line, but this is not ideal, as this exposes the password to the ps command and history.
~]# htpasswd -b -B -c /usr/local/share/users.htpasswd john.doe itsasecret
Adding password for user john.doe
In this example, the file named "users.htpasswd" would have been created.
~]# ls -l /usr/local/share
-rw-r--r--. 1 root root 77 Jul 7 18:43 users.htpasswd
The cat command can be used to view the contents of the htpasswd file.
~]# cat /usr/local/share/users.htpasswd
john.doe:$2y$05$sLPYyKUXPaaDfnCXCiz0CueZxRp2eX3NYhgPXaMHBYqCEFeVBZGi6
The -v (verify) option can be used to ensure the users password is correct.
~]# htpasswd -v j/usr/local/share/users.htpasswd john.doe
Enter password:
Password for user john.doe correct.
The -b (batch mode) option can be used to include the password on the command line.
~]# htpasswd -b -v /usr/local/share/users.htpasswd john.doe itsasecret
Password for user john.doe correct.
To append additional users to the file, do not use the -c option.
~]# htpasswd -b -B /usr/local/share/users.htpasswd jane.doe nottelling
Adding password for user jane.doe
Did you find this article helpful?
If so, consider buying me a coffee over at