
htdigest Command in Linux
The htdigest command is a robust utility used in Unix and Linux to manage user files for digest authentication. This commandline utility creates and updates special files called "flat-files."
These files keep track of the following −
- Usernames − The names of users.
- Realm − A realm is like a domain or a grouping of resources that need protection.
- Password − The secret code that users use to access protected resources.
So, if you want to control who gets to access certain resources on your Apache HTTP server, you use htdigest to create these files. Then, only the users listed in these files can access the protected resources. Think of it like a guest list for an exclusive event-only the people on the list (the flat-file) get in (access the resources) or think of a members-only gym, to get through the door, you need to swipe a special access card.
The htdigest command is like the security system that manages who gets a card (username, realm, and password) and who doesn't. Only those with a card can enter and use the gym facilities.
The htdigest command is particularly useful for restricting resources on the Apache HTTP server to just the users listed in these files.
Table of Contents
Here is a comprehensive guide to the options available with the htdigest command −
Syntax of htdigest Command
The following is the general syntax for the htdigest Command −
htdigest [ -c ] passwdfile realm username
htdigest Command Options
The following are list options used alongside the htdigest command −
Options | Description |
---|---|
-c | This option creates a new file. If the file already exists, it will be overwritten. |
passwdfile | The file that will store the username, realm, and password. If -c is given, this file is created if it does not already exist, or deleted and recreated if it does exist. |
realm | The protection domain. Think of it as a grouping of resources you're securing. |
username | The user name to create or update in the passwdfile. If username does not exist in this file, an entry is added. If it does exist, the password is changed. |
Examples of htdigest Command in Linux
In this section, we'll look at how to create files using the htdigest command followed by an application example −
Create the User File
To create a new .htdigest file with the specified realm and username, you can simply use the following command −
htdigest -c /home/text/.htdigest myRealm Tutorialspoint

Application Example
Let's say you want to restrict access to a directory on your Apache server. Here's how we'll go about it. First, we'll create the user file −
sudo htdigest -c /var/www/html/.htdigest myRealm myUsername

Next, we will configure Apache to use digest authentication. To achieve this, we'll first create the .htaccess file. Navigate to the directory you want to protect and create a file named .htaccess −
cd /var/www/html/protected_directory sudo nano .htaccess
Then, add the following lines to your Apache configuration file (.htaccess) −
AuthType Digest AuthName "myRealm" AuthUserFile /var/www/html/.htdigest Require valid-user
Where −
- AuthType Digest − Specifies the type of authentication.
- AuthName "myRealm" − The name of the authentication realm.
- AuthUserFile /var/www/html/.htdigest − Path to the password file.
- Require valid-user − Ensures that only valid users can access the directory.
Save the file and exit the editor.
Next, we'll ensure that Apache is configured to allow .htaccess overrides. To perform this, edit the main Apache configuration file (/etc/apache2/apache2.conf) −
sudo nano /etc/apache2/apache2.conf
Find the <Directory /var/www/html> section and ensure it allows .htaccess overrides −
<Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
Restart the Apache server to apply the changes −
sudo systemctl restart apache2
Now, when you try to access the protected directory via your browser (http://your_server_ip/protected_directory), you'll be prompted to enter the username and password you set up with htdigest. Only those with credentials can be able to access the .htdigest file.

Conclusion
The practical examples outlined here show how straightforward it is to implement htdigest for controlling access to sensitive directories. By following the outlined syntax and options, along with the necessary Apache configuration, you can ensure that your server remains secure and that only trusted users have access to protected resources.
Whether you're managing a small personal site or a large-scale application, mastering the htdigest command is a valuable skill in your toolkit for web security.