5 UNIX / Linux Apache HTTPD htdigest Password Command Examples

byRamesh

When you are using HTTP digest authentication, you need to specify the list of users who can access the secured page. Use the htdigest command as explained in this article to manipulate the password file that is used by Apache HTTPD digest authentication.
htdigest command syntax: htdigest [-c] passwordfile realm username

1. Create a new Apache Password file (and add a new user)

The following command will create a new password file called httpd-pwd-file, and add “ramesh” user to the file. “sysadmin” is the name of the real specified in this example. You can use any value for a realm that matches your environment.

This command will also ask you to enter the password for the user “ramesh”

Warning: Use -c flag only when you want to create a new password file. If you give -c by mistake, and if the password file already exist, it will be deleted. i.e it will overwrite all the user entries that are already present in the password file.

2. Apache Password File Format

The password file created by the htdigest command is a text file, which you can view it. The context of the file will be in the following format:

user-name:real-name:encrypted-password

For example, if you do a cat on the file that we created above, you’ll see something like the following:

# cat httpd-pwd-file
ramesh:sysadmin:1b2c1be8667731e7289

3. Add (i.e Append) another User to Apache Password file

If you already have users defined in an existing apache password file, you can add new users as shown below.

The following example will add user “john” to the existing httpd-pwd-file in the “dba” realm

# htdigest httpd-pwd-file dba john
Adding user john in realm dba
New password:
Re-type new password:

Now if you view the password file you’ll see both ramesh and john as shown below.

5. Adding an User to Multiple Realms in Password File

If you want to add the same user to multiple realms, you just need to add the user to a particular realm. This is similar to adding a user to a new realm.

The following example will add user “john” to the existing httpd-pwd-file in the “sysadmin” realm

# htdigest httpd-pwd-file sysadmin john
Adding user john in realm sysadmin
New password:
Re-type new password:

Now, if you view the password file, you’ll see two entries for John, as he belongs to different realms. Please note that the password for john on these two realms could be different, depending on what you entered as password while adding the user to that particular realm.