---
title: "lighttpd的权限认证模块 mod_auth"
author: "夜鸣猪"
date: "2009-12-16T18:17:29Z"
tags: []
categories: ["操作系统"]
slug: "lighttpd-mod-auth"
draft: false
lastmod: "2009-12-16T18:33:19Z"
id: 549981
url: "https://hlee.iteye.com/blog/549981"
---

互联网是不安全的。在利用互联网便利的同时，我们需要特别小心，特别是现在的搜索引擎可是会到处钻哦，要做好防止数据被盗用的准备。如果你也像我一样，想对某个域、目录或页面进行密码保护，下面就告诉你在Lighttpd下是如何进行的。[]()

**一、简单模式**  
Lighttpd使用mod\_auth模块可实现对域等进行用户名、密码保护的功能。这与Apache下用.htaccess实现的保护是类似的。  
mod\_auth的使用说明，请看：[这里](http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModAuth)。这里以最简单的basic明文方式说明。  
**1、修改配置文件**  
在/etc/lighttpd/lighttpd.conf配置文件中加入：

引用

\# 激活mod\_auth模块  
server.modules = (...,"mod\_auth",...)  
\# 运行lighttpd服务的用户名和组，后面定义密码文件时会用到  
server.username            = "lighttpd"  
server.groupname           = "lighttpd"  
\# 打开auth的debug模式，方便调试，正常后可以取消  
auth.debug = 2  
\# 使用明文密码，这是最简单的方式，当然也有安全问题，见后面  
auth.backend               = "plain"  
\# 定义用户名、密码存放的路径  
auth.backend.plain.userfile = "/etc/lighttpd/.lighttpd.user"  
\# 根据说明，下面的groupfile还未完全实现，不用设置咯  
#auth.backend.plain.groupfile = "lighttpd.group"  
\# 定义要加密的路径  
auth.require               = ( "/server-status" =&gt;  
(  
\# 可以使用多种认证方式，这里以basic为例  
"method"  =&gt; "basic",  
\# 访问时，对话框的提示信息  
"realm"   =&gt; "Server Status WebSite",  
\# 允许访问的用户名，用“|”号分割多个用户  
"require" =&gt; "user=linuxing|user=test01"  
),  
"/server-config" =&gt;  
(  
"method"  =&gt; "basic",  
"realm"   =&gt; "Server Config WebSite",  
\# valid-user用于表示所有合法的用户  
"require" =&gt; "valid-user"  
)  
)

**2、生成密码文件**  
用户名和密码写在同一个文件里面，并根据运行lighttpd服务的用户给予适当的权限，运行：

引用

\# cat /etc/passwd|grep lighttpd  
lighttpd:x:100:101:lighttpd web server:/srv/www/lighttpd:/sbin/nologin  
\# echo "linuxing:abc123" &gt; /etc/lighttpd/.lighttpd.user  
\# chown lighttpd:lighttpd /etc/lighttpd/.lighttpd.user  
\# chmod 440 /etc/lighttpd/.lighttpd.user  
\# ll /etc/lighttpd/.lighttpd.user  
\-r--r----- 1 lighttpd lighttpd 16 Feb  6 12:45 .lighttpd.user  
\# service lighttpd restart

**3、访问**  
访问http://ip/server-status或http://ip/server-config，则会提示：  
[](/admin/blogs/attachment.php?fid=168)  
![](http://dl.iteye.com/upload/attachment/181293/0f919dcd-a28a-3617-9392-c60d7312f958.jpg)  
   
输入正确的用户名（linuxing），密码（abc123）后即可正常访问。否则，会报401错误，error.log日志显示：

引用

2009-02-06 12:59:49: (http\_auth.c.876) get\_password failed  
2009-02-06 12:59:52: (http\_auth.c.876) get\_password failed  
2009-02-06 12:59:54: (http\_auth.c.876) get\_password failed

可见，在全局部分定义了该auth.require，可以较好的保护了/server-status和/server-config页面。需要注意的是，该页面有mod\_status提供，所以在加载模块部分，务必让mod\_auth比mod\_status在前。

**二、使用其他验证方式**  
上面介绍的是最简单的密码验证方式，但也存在很明显的安全问题：

引用

a、basic模式下，用户名和密码在浏览器与服务器之间的网络是明文传输的；  
b、用户名和密码也是以明文方式存在在本地；

为避免这些问题，Lighttpd提供了其他验证方式。

**1、验证方式的不同**  
[DocsModAuth](http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModAuth)的介绍，有basic和digest两种验证方式，其各自支持不同的后台密码文件保存模式：

引用

**basic auth:**  
该模式用户名和密码在网络中都是以base64编码明文传输的，容易被截取，不安全。支持的后台密码文件保存方式有：  
\- plain_  
\- htpasswd_   
\- htdigest_  
\- ldap_  
**digest auth:**  
该模式改为传输hashed值，相对比较安全。但支持的后台密码文件保存方式只有：  
\- plain_  
\- htdigest_

**2、设置方法**  
根据选择的验证模式和后台密码文件保存方式不同，配置文件和密码保存文件生成的方式就不同了。记住，把后台密码保存文件设置为只允许运行lighttpd服务的用户和组（如lighttpd）读，其他用户禁止访问。

a、plain方式  
正如前面提到的，把明文用户名和密码写入后台密码保存文件，一行一个，格式为：

引用

username:password

b、htpasswd  
这是利用Apache提供的htpasswd工具生成后台密码保存文件，它使用了crypt()加密函数：

引用

\# htpasswd -m -c .lighttpd.user linuxing  
\# cat .lighttpd.user  
linuxing:$apr1$o1ZZY/..$MO6rKCiwLXnIp8h.7PxvK1  
\# chown lighttpd.lighttpd .lighttpd.user  
\# chmod 440 .lighttpd.user

配置文件中则为：

引用

\# 定义后台密码文件验证方式  
auth.backend               = "htpasswd"  
\# 定义后台密码保存文件的路径  
auth.backend.htpasswd.userfile = "/etc/lighttpd/.lighttpd.user"

c、htdigest方式  
方法与htpasswd类似，也是借助Apache提供的htdigest工具生成后台密码保存文件，但其有三列，除用户名、密码外，还包括realm（提示信息），一行表示一个账号，格式为：

引用

username:realm:(password)

其中(password)并不是明文的，而是由username、realm、password组合后，通过md5计算出来的，用命令来实现就是这样：

引用

\# echo -n "linuxing:Info Messages:abc123"|md5sum|cut -b -32  
61ab4c1c5d8fea7b5d32c449993a114c

◎ 这里，请特别关注，这里的realm与lighttpd中使用到"realm"   =&gt; 部分**必须一样**，也就是说用户名、密码和后台密码保存文件中的realm三部分都会作为验证的依据。Wiki里面没有仔细说明，我当时在这里就搞了很长时间。

生成后台密码保存文件：

引用

\# htdigest -c .lighttpd.user 'Info Messages' linuxing  
Adding password for linuxing in realm Info Messages.  
New password:  
Re-type new password:  
\# cat .lighttpd.user  
linuxing:Info Messages:61ab4c1c5d8fea7b5d32c449993a114c  
\# chown lighttpd.lighttpd .lighttpd.user  
\# chmod 440 .lighttpd.user

配置文件中则为：

引用

auth.backend               = "htdigest"  
auth.backend.htdigest.userfile = "/etc/lighttpd/.lighttpd.user"  
\# 试试用digest验证模式  
auth.require               = ( "/server-status" =&gt;  
(  
"method"  =&gt; "digest",  
"realm"   =&gt; "Info Messages",  
"require" =&gt; "user=linuxing"  
)  
)

**◎ 记住咯，username、realm、password三部分都会验证的，缺一不可。即使输入的用户名和密码都与后台保存密码的文件相同，但realm与auth.require部分定义的不一样，也是不能登陆的。**  
**这里可以把realm作为一个组的标识使用。**

官方Wiki提供了一个方便易用的管理htdigest密码文件的脚本：  
（不过，这脚本也有问题，就是realm不能有空格，即使用引号隔开也不行）

下载文件

[点击这里下载文件](http://dl.iteye.com/topics/download/dba0853d-ee15-3980-9d55-722c23777072)

使用方法是：

引用

更新或增加新用户  
$ lightdigest.sh -u USERNAME -r REALM\_NAME -f PASSWORD\_FILE\_PATH  
删除旧用户  
$ lightdigest.sh -d -u USERNAME

最后还有一种方式，是使用ldap数据保留用户名和密码信息，适用于多用户的环境，但配置比较复杂，又需要的朋友，请看官方的Wiki介绍吧。

**三、用于域或目录的环境**  
只要把auth.require部分写入虚拟主机部分的配置中即可，类似：

引用

$HTTP\["url"] =~ "^/download|^/server-info" {   
        auth.require = (   "" =&gt; (     
                     "method"  =&gt; "digest",  
                     "realm"   =&gt; "download archiv",  
                     "require" =&gt; "user=linuxing|user=test01"   
                     )  
        )  
}

**四、参考资料**  
[DocsModAuth](http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModAuth)  
[Lighttpd setup a password protected directory (directories)](http://www.cyberciti.biz/tips/lighttpd-setup-a-password-protected-directory-directories.html)  
[Set Apache Password Protected Directories With .htaccess File](http://www.cyberciti.biz/faq/howto-setup-apache-password-protect-directory-with-htaccess-file/)

**相关日志**  
[lighttpd基础配置-限速、与Squid配合、监控状态](/admin/blogs/549997)  
[lighttpd基础配置-虚拟主机](/admin/blogs/549988)  
[lighttpd基础配置-perl CGI、php](/admin/blogs/549995)