转到apache/bin目录下,输入:
htpasswd -cmb user abc 123456
即可在当前目录下生成文件,user,且用户名为abc,密码为123456(加密的),
或依此步骤生成
E:/web/Apache2/bin>htpasswd -c userpass user1
Automatically using MD5 format.
New password: ******
Re-type new password: ******
Adding password for user user1
更新密码:
E:/web/Apache2/bin>htpasswd userpass user1
Automatically using MD5 format.
New password: ******
Re-type new password: ******
Updating password for user user1
添加新用户:
E:/web/Apache2/bin>htpasswd userpass user2
Automatically using MD5 format.
New password: ******
Re-type new password: ******
Updating password for user user2
打开文件userpass,可以看到有两条记录:
user1:$apr1$Rr......$lHobT0z67Sp8heUkS0B.b0
user2:$apr1$Px......$XQWTfGNlfVBgEvQKBK8gt1
删除用户:
htpasswd -D userpass user1
Automatically using MD5 format
Deleting password for user user1
打开文件userpass,可以看到有只一条记录了:
user2:$apr1$Px......$XQWTfGNlfVBgEvQKBK8gt1
当然你也可以用些指令生成.htaccess
E:/web/Apache2/bin>htpasswd -c .htaccess user
Automatically using MD5 format.
New password: ******
Re-type new password: ******
Adding password for user user
选项:
-c 创建新的文件
-m 用md5方式加密密码
-b 在命令行上输入密码
Usage:
htpasswd [-cmdpsD] passwordfile username
htpasswd -b[cmdpsD] passwordfile username password
htpasswd -n[mdps] username
htpasswd -nb[mdps] username password
-c Create a new file.
-n Don't update file; display results on stdout.
-m Force MD5 encryption of the password (default).
-d Force CRYPT encryption of the password.
-p Do not encrypt the password (plaintext).
-s Force SHA encryption of the password.
-b Use the password from the command line rather than prompting for it.
-D Delete the specified user.
On Windows, NetWare and TPF systems the '-m' flag is used by default.
On all other systems, the '-p' flag will probably not work.
E:/web/Apache2/bin>htpasswd -mb user abc 123456
htpasswd: cannot modify file user; use '-c' to create it
E:/web/Apache2/bin>htpasswd -cmb user abc 123456
Adding password for user abc
本手册页仅列出命令行参数,配置基本认证的相关指令的细节请参见mod_auth_basic文档。
top
语法
htpasswd [ -c ] [ -m ] [ -D ] passwdfile username
htpasswd -b [ -c ] [ -m | -d | -p | -s ] [ -D ] passwdfile username password
htpasswd -n [ -m | -d | -s | -p ] username
htpasswd -nb [ -m | -d | -s | -p ] username password
top
选项
-b
使用批处理方式。也就是直接从命令行获取密码而不进行提醒。使用这个选项需要特别注意,因为命令行中的密码是清晰可见的。
-c
创建passwdfile文件。如果passwdfile已经存在,那么将被清空并改写。该选项不能和 -n 同时使用。
-n
在标准输出上显示结果而不是更新文件。用于生成可以为Apache非文本输出存储格式所接受的密码记录。这个选项改变了命令行语法,因为passwdfile参数(通常是第一个)被忽略了。该选项不能和 -c 同时使用。
-m
使用MD5加密密码。在Windows, Netware, TPF上这是默认方法。
-d
使用crypt()对密码进行加密。在Windows, Netware, TPF以外的平台上这是默认方法。虽然有可能在所有的平台上被htpasswd支持,但是在Windows, Netware, TPF上,该方法不能被httpd所支持。
-s
使用 SHA 对密码进行加密。这种方法易于通过LDAP目录交换格式和Netscape server进行交换。
-p
使用明文密码(不加密)。虽然htpasswd在所有平台上都支持这种方法,但是httpd只能在Windows, Netware, TPF上支持这种方法。
-D
如果username存在于passwdfile中,则删除该用户。
passwdfile
包含用户名和密码的文本文件的名称。如果使用了 -c 选项,若文件已存在则更新它,若不存在则创建它。
username
在passwdfile中添加或更新记录。若username不存在则添加一条记录,若存在则更新其密码。
password
将被加密并存储到文件中的明文密码。必须和 -b 同时使用。
top
返回值
htpasswd仅在用户名和密码被成功存入passwdfile或成功更新的情况下返回"0"。若访问文件发生错误则返回"1";若命令行语法错误则返回"2";若密码验证失败则返回"3";若正在进行中的操作被打断则返回"4";若值(username, filename, password, 计算结果)长度超标则返回"5";若用户名包含非法字符(参见限制)则返回"6";若指定的文件不能被正确识别则返回"7"。
top
示例
htpasswd /usr/local/etc/apache/.htpasswd-users jsmith
添加或修改用户jsmith的密码。密码将被提示输入。在Windows平台上,密码将使用Apache修改过的MD5算法进行加密;在其它平台上将使用crypt()进行加密。如果指定的文件不存在,htpasswd将只返回一个错误代码,而不做其它任何事。
htpasswd -c /home/doe/public_html/.htpasswd jane
创建一个新文件并在其中添加一条用户jane的记录。密码将被提示输入。如果文件存在但是不能被读取或写入,则不会有任何记录被修改,同时htpasswd将会显示一个错误信息并返回一个错误代码。
htpasswd -mb /usr/web/.htpasswd-all jones Pwd4Steve
将来自命令行的密码(Pwd4Steve)使用MD5算法加密,并将其存入指定的文件。
top
安全方面的考虑
Web密码文件(比如由htpasswd管理)不应当存在于网络空间中,即不能被客户端有机会访问。
我们反对使用 -b 选项,因为密码将以明文的形式出现在命令行中。
top
限制
在Windows和MPE平台上,用htdbm加密的密码最大长度是255字符。超出部分将被截断。
htdbm使用的MD5加密算法已经被Apache修改过了,仅能够被Apache识别,不能被其它Web服务器识别。
用户最大长度是255字节,并且不能包含冒号(:)。