← 返回命令列表

Linux command

htpasswd 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Create a new password file

htpasswd -c [path/to/.htpasswd] [username]

Add or update a user

htpasswd [path/to/.htpasswd] [username]

Add user with password

htpasswd -b [path/to/.htpasswd] [username] [password]

Delete a user

htpasswd -D [path/to/.htpasswd] [username]

Verify a user's password

htpasswd -v [path/to/.htpasswd] [username]

Use bcrypt

htpasswd -B [path/to/.htpasswd] [username]

Display password hash

htpasswd -n [username]

Create file using SHA

htpasswd -c -s [path/to/.htpasswd] [username]

说明

htpasswd manages user authentication files for Apache HTTP Server's basic authentication. It creates and updates flat-file databases containing usernames and encrypted passwords used with .htaccess or Apache configuration directives. The password file format is simple: one line per user with username:encrypted_password. Apache's mod_auth_basic reads this file to authenticate requests. The file should be stored outside the web root and have restrictive permissions. Password encryption defaults to MD5-based algorithm (prefixed with $apr1$). The -B flag enables bcrypt, which is more resistant to brute-force attacks and recommended for new installations. The cost factor (-C) controls bcrypt's computational intensity. For non-interactive use in scripts, -b allows specifying the password on the command line, though this exposes the password in process lists. The -i flag reads from stdin, which is safer for scripting. The tool is often used with Nginx as well, since Nginx can read Apache-format password files for basic authentication.

参数

-c
Create a new file (overwrites existing).
-n
Display results on stdout, don't update file.
-b
Use password from command line (batch mode, insecure).
-i
Read password from stdin without verification.
-m
Use MD5 encryption (default on most systems).
-B
Use bcrypt encryption (most secure).
-C _cost_
Set bcrypt cost (4-17, default 5, higher = slower).
-d
Use crypt() encryption (insecure, limited to 8 chars).
-s
Use SHA encryption (insecure).
-p
Use plaintext (insecure, for testing only).
-D
Delete the specified user.
-v
Verify password for user.

FAQ

What is the htpasswd command used for?

htpasswd manages user authentication files for Apache HTTP Server's basic authentication. It creates and updates flat-file databases containing usernames and encrypted passwords used with .htaccess or Apache configuration directives. The password file format is simple: one line per user with username:encrypted_password. Apache's mod_auth_basic reads this file to authenticate requests. The file should be stored outside the web root and have restrictive permissions. Password encryption defaults to MD5-based algorithm (prefixed with $apr1$). The -B flag enables bcrypt, which is more resistant to brute-force attacks and recommended for new installations. The cost factor (-C) controls bcrypt's computational intensity. For non-interactive use in scripts, -b allows specifying the password on the command line, though this exposes the password in process lists. The -i flag reads from stdin, which is safer for scripting. The tool is often used with Nginx as well, since Nginx can read Apache-format password files for basic authentication.

How do I run a basic htpasswd example?

Run `htpasswd -c [path/to/.htpasswd] [username]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -c do in htpasswd?

Create a new file (overwrites existing).