How to use the command 'htpasswd' (with examples)

How to use the command 'htpasswd' (with examples)

The htpasswd command is used to create and manage htpasswd files, which are used to protect web server directories using basic authentication. It allows you to add users, update existing users, delete users, verify user passwords, and display the username and password in plain text or md5 format.

Use case 1: Create/overwrite htpasswd file

Code:

htpasswd -c path/to/file username

Motivation: You want to create a new htpasswd file or overwrite an existing one with a new username and password.

Explanation:

  • -c: This option is used to create a new htpasswd file or overwrite an existing one. If the file already exists, it will be deleted before creating a new one.
  • path/to/file: The path to the htpasswd file that you want to create or overwrite.
  • username: The username you want to add to the htpasswd file.

Example Output:

Adding password for user username.
New password:
Re-type new password:
Updating password for user username.

Use case 2: Add user to htpasswd file or update existing user

Code:

htpasswd path/to/file username

Motivation: You want to add a new user to an existing htpasswd file or update the password of an existing user.

Explanation:

  • path/to/file: The path to the htpasswd file that you want to add the user to or update the user’s password.
  • username: The username you want to add or update.

Example Output:

Adding password for user username.
New password:
Re-type new password:
Updating password for user username.

Use case 3: Add user to htpasswd file in batch mode without an interactive password prompt

Code:

htpasswd -b path/to/file username password

Motivation: You want to add a new user to an htpasswd file in batch mode, without being prompted to enter the password interactively. This is useful when using the command in scripts or automated processes.

Explanation:

  • -b: This option is used to enable batch mode, which allows you to provide the password directly on the command line.
  • path/to/file: The path to the htpasswd file that you want to add the user to.
  • username: The username you want to add.
  • password: The password you want to assign to the user.

Example Output:

None (the command doesn’t produce any output when used in batch mode).

Use case 4: Delete user from htpasswd file

Code:

htpasswd -D path/to/file username

Motivation: You want to remove a user from an existing htpasswd file, thereby preventing the user from accessing protected web server directories.

Explanation:

  • -D: This option is used to delete a user from the htpasswd file.
  • path/to/file: The path to the htpasswd file that you want to delete the user from.
  • username: The username you want to delete.

Example Output:

password for user username cleartext password deleted.

Use case 5: Verify user password

Code:

htpasswd -v path/to/file username

Motivation: You want to verify whether a given user’s password matches the password stored in the htpasswd file.

Explanation:

  • -v: This option is used to verify a user’s password.
  • path/to/file: The path to the htpasswd file that you want to verify the user’s password against.
  • username: The username whose password you want to verify.

Example Output:

username: password verification OK.

Use case 6: Display a string with username and password (md5)

Code:

htpasswd -nbm username password

Motivation: You want to generate a string that contains the username and password in md5 format. This can be useful in certain scenarios where the string needs to be used as input for another program or configuration file.

Explanation:

  • -nbm: These options are used to enable md5 encryption and specify the input as a username and password combination.
  • username: The username you want to include in the string.
  • password: The password you want to include in the string.

Example Output:

username:$apr1$tauLmLW1$HM49E3Xmj35P5nj0WDJO/1

Conclusion:

The htpasswd command is a powerful tool for creating and managing htpasswd files to protect web server directories using basic authentication. It provides various options for adding users, updating passwords, deleting users, verifying passwords, and generating strings with username and password. By understanding and utilizing these use cases, you can effectively secure your web server and control access to your resources.

Related Posts

How to use the command 'whisper' (with examples)

How to use the command 'whisper' (with examples)

Whisper is a command-line tool that allows you to convert audio files to various formats, such as txt, vtt, srt, tsv, and json.

Read More
How to use the command f3probe (with examples)

How to use the command f3probe (with examples)

The f3probe command is used to probe a block device, such as a flash drive or a microSD card, for counterfeit flash memory.

Read More
How to use the command rustup which (with examples)

How to use the command rustup which (with examples)

The rustup which command is used to display the path to the binary of a given command managed by rustup.

Read More