htpasswd Command in Linux



The htpasswd command is used to create and update files that store usernames and passwords for HTTP basic authentication. If htpasswd can't access or update these files for any reason, it returns an error and makes no changes. This ensures the integrity and security of the authentication files.

These authentication files restrict access to resources available from the Apache HTTP server, allowing only listed users to access certain content. Although htpasswd primarily manages usernames and passwords in a flat file, it can also encrypt passwords for use in other data storage systems.

The htpasswd command uses either MD5 encryption (modified for Apache) or the system's crypt() routine to secure passwords. It can handle files containing both MD5-encrypted and crypt()-encrypted passwords.

In addition, htpasswd returns codes, which can help you troubleshoot specific issues when running the command −

  • 0 − Successful addition or update of the username and password.
  • 1 − File access issues (e.g., insufficient permissions).
  • 2 − Command line syntax errors.
  • 3 − Password verification mismatch during interactive entry.
  • 4 − Operation was interrupted.
  • 5 − Value too long (e.g., username, filename, password).
  • 6 − Illegal characters in username (e.g., :).
  • 7 − Invalid password file.

Table of Contents

Here is a comprehensive guide to the options available with the htpasswd command −

Syntax of htpasswd Command

The following is the general syntax for the htpasswd command −

htpasswd [options] passwordfile username

htpasswd Command Options

The following is a list of options that can help you get a better grip on using htpasswd command −

Options Description
-b Use batch mode, meaning the password is taken directly from the command line rather than prompting for it. Be careful with this option as it makes the password visible on the command line, posing a security risk.
-c Create the passwdfile. If this file already exists, it will be rewritten and truncated. This option can't be used with the -n option.
-n Display the results on standard output instead of updating a file. Useful for generating password records for other data stores. This changes the command line syntax by omitting the passwdfile argument. It can't be combined with the -c option.
-m Use MD5 encryption for the password. This is the default on Windows, Netware, and TPF.
-d Use crypt() encryption for passwords. This is the default on other platforms but not supported by httpd server on Windows, Netware, and TPF.
-s Use SHA encryption for the password, useful for migrating to/from Netscape servers using the LDAP Directory Interchange Format (LDIF).
-p Use plaintext passwords. While htpasswd supports creating plaintext passwords on all platforms, the httpd daemon will only accept them on Windows, Netware, and TPF.
-D Delete a user from the passwdfile. If the username exists in the specified file, it will be deleted.
passwdfile The name of the file where the username and password will be stored. If the -c flag is given, this file is created if it doesn't exist, or rewritten and truncated if it does exist.
username The username to create or update in the passwdfile. If the username doesn't exist, an entry is added; if it does exist, the password is changed.
password The plaintext password to be encrypted and stored in the file. This is only used with the -b option.

Examples of htpasswd Command in Linux

In this section, we will explore various practical examples of the htpasswd command to demonstrate its different functionalities for managing HTTP authentication.

Create a New Password File with a Single User

To create a new password file and add a user to it, you can simply run the following command −

sudo htpasswd -c /etc/apache2/.htpasswd user1

This command creates a new file named .htpasswd in the /etc/apache2/ directory and adds user1 to it. You'll be prompted to enter and confirm a password for user1.

htpasswd Command in Linux1

Add a User to an Existing Password File

To add a user to an existing password file, simply run the following command −

sudo htpasswd /etc/apache2/.htpasswd user2

This command adds user2 to the .htpasswd file located in the /etc/apache2/ directory. You will be prompted to enter and confirm a password for user2.

htpasswd Command in Linux2

Update a User's Password

To update a user's password, you can use the following command −

sudo htpasswd /etc/apache2/.htpasswd user1

This command updates the password for user1 in the .htpasswd file located in the /etc/apache2/ directory.

htpasswd Command in Linux3

Use Batch Mode to Add a User with a Password from the Command Line

To use batch mode to add a user with a password from the command line, run the following command −

sudo htpasswd -b /etc/apache2/.htpasswd user3 password123

This command adds user3 to the .htpasswd file located in the /etc/apache2/ directory using the password password123. The "-b" flag allows you to specify the password directly in the command line.

htpasswd Command in Linux4

Create a Password File with MD5 Encryption

To create a password file with MD5 encryption, run the following command −

sudo htpasswd -cm /etc/apache2/.htpasswd user4

This command creates a new password file at /etc/apache2/.htpasswd and adds user4 to it using MD5 encryption for the password. The "-c" flag tells htpasswd to create a new file, and the "-m" flag specifies MD5 encryption.

htpasswd Command in Linux5

Use SHA Encryption

To use SHA encryption, you can simply run the following command −

sudo htpasswd -s /etc/apache2/.htpasswd user8

This command adds user8 to the .htpasswd file located in the /etc/apache2/ directory using SHA encryption for the password. The "-s" flag specifies that SHA encryption is the one being used.

htpasswd Command in Linux6

Display Encrypted Password on Standard Output

To display the encrypted password on standard output, run the following command −

sudo htpasswd -nb user6 password456

This command prints the username (user6) and the encrypted password (password456) to the standard output, rather than writing it to a file. The "-n" flag displays the result on the screen, and the "-b" flag allows you to provide the password directly.

htpasswd Command in Linux7

Set User's Password Using Plaintext

To set a user's password using plaintext (not recommended), run the following command −

sudo htpasswd -p /etc/apache2/.htpasswd user7

This command adds user7 to the .htpasswd file located in the /etc/apache2/ directory with the password stored in plaintext. The "-p" flag specifies that the password should be stored as plaintext.

htpasswd Command in Linux8

Conclusion

In this tutorial, we explained in detail the htpasswd command including its syntax, various options, and practical examples, highlighting its versatility in creating, updating, and maintaining password files.

It's crucial to consider security best practices when using the htpasswd command. Password files should be stored outside of the web server's accessible directories to prevent unauthorized retrieval. Keep in mind that there are restrictions on password and username lengths, as well as character limitations. Moreover, while the MD5 algorithm is a popular choice within Apache, it's important to note that it is not universally compatible with all web servers.

By adhering to these guidelines, you can effectively utilize the htpasswd command to enhance the security of your web applications while minimizing risks.

Advertisements