ubuntu 16.04_ubuntu中pure-ftpd FTP服务器530 Login authentication failed彻底解决

更新时间:2019-10-16    来源:Ubuntu    手机版     字体:

【www.bbyears.com--Ubuntu】

在 ubuntu 配置 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

那么,下面是解决方案。

1. 前置知识

在 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 查看。

好了,这就进入正题。为了描述清楚,我还是把完整的配置流程说一遍。

2. 创建系统用户

创建一个 ftp 专用账号,同时创建一个 ftp 用户组。设置其 shell 禁止登录。注意,这里设置的 home 值就是 Anonymous 账户的访问地址。

useradd -M -U -s /usr/sbin/nologin -d /var/ftp ftp

关于禁止登录的 shell 设置,大家常用的是 /usr/sbin/nologn 和 /bin/false ,在 这里 可以了解一下它们的区别。更详细的信息也可以通过 man false 和 man nologin 得到(当然还可以顺便看看 man true )。

3. 创建虚拟用户

下面的命令将创建一个用户名为 ssi 的用户,将其信息写入到 /etc/pure-ftpd/pureftpd.passwd 中。

这个 ssi 用户,就是虚拟用户。系统中不必(也不应该)存在这个用户。这个用户的权限,与我们通过 -u 和 -g 参数指定的系统账户相同。

期间, pure-pw 会要求你输入这个用户的 ftp 密码。

pure-pw useradd ssi -u ftp -g ftp -d /var/ftp/ssi

ftp 大多数时候都是为 www 服务的,有时候为了方便(不一定安全),甚至可以直接将虚拟用户绑定到 www-data 用户上。

将用户信息更新到 /etc/pure-ftpd/pureftpd.pdb 文件:

pure-pw mkdb

4. 设置掩码

将需要的文件和目录掩码写入 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 上的特点吧。 ;)

5. 解决登录错误

终于到正题了。

根据 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

6. 分析

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 模块。

7. 其他问题

CentOS 上,我是编译安装 pure-ftpd 的,这里一起说一些注意事项。
7.1 启动服务用的 script

源码并没有提供启动 script,我将一个可用的传到了 gist ,下载后修改路径然后将其复制到 /etc/init.d 即可使用。

记得在编译的时候要加入 --with-ftpwho 选择,这样使用上面的脚本实现 service pureftpd status 的时候,可以看到当前正在连接的客户端的信息。
7.2 重启服务出现 421 错误

是因为在编译的时候没有加入 --with-puredb 选项。需要重新编译。

421 Unknown authentication method: puredb:/opt/pureftpd/etc/pureftpd.pdb

7.3 530 Sorry, but I can’t trust you

同样是 530 登录错误,提示如下:

530 Sorry, but I can't trust you
ftp: Login failed.

这是因为系统的 ftp 帐号是系统帐号(system account),可能是在创建 ftp 帐号的时候加了 -r 参数。查看一下 ftp 帐号的 id:

id ftp
uid=14(ftp) gid=50(ftp) groups=50(ftp)

在 pure-ftp.conf 配置文件中有一个选项,设置最小帐号 UID,若小于这个 UID 则会出现上面的 530 错误。这个值默认为 100 。

# Minimum UID for an authenticated user to log in.
MinUID                      100

解决方案有两个:

    修改 MinUID 的值使其小于 ftp 帐号的值(我的是14,所以要修改成13或更小),
    修改 ftp 帐号的 UID 。

这里我选择后者:

usermod -u 600 ftp
groupmod -g 600 ftp

必须要更新虚拟帐号才会生效:

pure-pw usermod ssi -u ftp -g ftp -m

本文来源:http://www.bbyears.com/caozuoxitong/73462.html