在服务器上,我一直使用的是 vsftpd ,但由于 vsftpd 不支持 MLSD ,我开始转向 pure-ftpd 。
简单配置完毕后,登录一直出现错误。
-> % ftp ssi@xxxx.xx Connected to xxxx.xx. 220---------- Welcome to Pure-FTPd [privsep] [TLS] ---------- 220-You are user number 1 of 50 allowed. 220-Local time is now 12:08. Server port: 21. 220-This is a private system - No anonymous login 220-IPv6 connections are also welcome on this server. 220 You will be disconnected after 15 minutes of inactivity. 331 User ssi OK. Password required Password: 530 Login authentication failed ftp: Login failed 在 ubuntu 上,直接使用 apt-get 可以安装 pure-ftpd 的最新版。但它的配置有些古怪。
pure-ftpd 的配置文件默认处于 /etc/pure-ftpd/conf ,其中的每个文件就是一个命令行参数的名字,文件中的内容就是这个参数的值。
pure-ftpd 使用 pure-ftpd-wrapper 命令来将这些配置文件转换成命令行参数,用它们来启动服务。
例如,我要禁止 chroot,需要增加一个 /etc/pure-ftpd/conf/ChrootEveryone 文件,在其中写入 yes 这个值。
pure-ftpd 的所有命令行参数都对应一个短参数和一个长参数,可以通过 man pure-ftpd 来查看所有支持的参数。
这些参数都是全小写的,如果要查看它们的标准形式,可以使用 man pure-ftpd-wrapper 查看。
好了,这就进入正题。为了描述清楚,我还是把完整的配置流程说一遍。
创建一个 ftp 专用账号,同时创建一个 ftp 用户组。设置其 shell 禁止登录。
useradd -M -U -s /usr/sbin/nologin ftp 关于禁止登录的 shell 设置,大家常用的是 /usr/sbin/nologn 和 /bin/false ,在 这里 可以了解一下它们的区别。更详细的信息也可以通过 man false 和 man nologin 得到(当然还可以顺便看看 man true )。
下面的命令将创建一个用户名为 ssi 的用户,将其信息写入到 /etc/pure-ftpd/pureftpd.passwd 中。
这个 ssi 用户,就是虚拟用户。系统中不必(也不应该)存在这个用户。这个用户的权限,与我们通过 -u 和 -g 参数指定的系统账户相同。
期间, pure-pw 会要求你输入这个用户的 ftp 密码。
pure-pw useradd ssi -u ftp -g ftp ftp 大多数时候都是为 www 服务的,有时候为了方便(不一定安全),甚至可以直接将虚拟用户绑定到 www-data 用户上。
将用户信息更新到 /etc/pure-ftpd/pureftpd.pdb 文件:
pure-pw mkdb 将需要的文件和目录掩码写入 Umask 配置文件中,中间用空格分隔:
echo '003 002' > /etc/pure-ftpd/conf/Umask 注意,虽然 pure-ftp 的 man 页中这样说:
-U umask files:umask dirs
Change the mask for creation of new files and directories. The default are 133 (files
are readable -but not writable- by other users) and 022 (same thing for directory,
with the execute bit on). If new files should only be readable by the user, use
177:077. If you want uploaded files to be executable, use 022:022 (files will be read‐
able by other people) or 077:077 (files will only be readable by their owner).
但你如果真的设置成这样 003:002 这样,pure-ftpd 重启时会报错:
Restarting ftp server: /usr/sbin/pure-ftpd-wrapper: Invalid configuration file /etc/pure-ftpd/conf/Umask: “003:002″ not two octal numbers
所以,应该将分隔符设置成空格。可能这也是 ubuntu 上的特点吧。
终于到正题了。
根据 google 到的 信息 ,先是在 auth 中指定 PureDB。
ln -s /etc/pure-ftpd/conf/PureDB /etc/pure-ftpd/auth/50pure 处理之后,问题依旧。
然后在 pure-ftpd 的 README 中找到这段话:
–with-pam: use pluggable authentification modules. Don’t use this option
if your login/passwd pairs are always refused (but the real fix would be to
fix your PAM configuration). You need to create a /etc/pam.d/pure-ftpd file
to properly use the PAM authentication. The ‘pam’ directory contains an
example of such a file.
同时又找到 这里 ,确定取消了 PAM 验证,登录就成功了。
echo no > /etc/pure-ftpd/conf/PAMAuthentication 但取消 PAM 是不合理的,也会让系统不安全。查看 /etc/pam.d/pure-ftpd 的配置都是正确的,说明这不是 PAM 的问题。
看到 PAM 的配置文件中有 pam_shells.so 的字样,然后鬼使神差地看了一眼 /etc/shells ,发现其中并不包含 /usr/sbin/nologin 。
于是将其加入:
echo '/usr/sbin/nologin' >> /etc/shells 然后还原 PAMAuthentication 的设置,重启服务,搞定:
service pure-ftpd restart man pam_shells 可以看到下面的信息:
NAME pam_shells - PAM module to check for valid login shell SYNOPSIS pam_shells.so DESCRIPTION pam_shells is a PAM module that only allows access to the system if the users shell is listed in /etc/shells. It also checks if /etc/shells is a plain file and not world writable. 而在 /etc/pam.d/pure-ftpd 中配置了下面的内容:
auth required pam_shells.so 因为 ftp 用户的 shell 是 /usr/sbin/nologin ,那么这个 shell 就必须存在于 /etc/shells 中,才能通过 PAM 模块。
(全文完)