pure-ftp

匿名用户登录,系统中必须有用户「ftp」存在,否则就会提示错误

1
2
apt-get update
apt-get install pure-ftpd

创建系统用户

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

1
2
3
4
groupadd ftpgroup
useradd -g ftpgroup -d /home/ftp ftp -s /bin/false
mkdir /home/ftp
chown ftp:ftpgroup /home/ftp

关于禁止登录的 shell 设置,大家常用的是/usr/sbin/nologn/bin/false。更详细的信息也可以通过man falseman nologin得到(当然还可以顺便看看man true)。

When /sbin/nologin is set as the shell, if user with that shell logs in, they’ll get a polite message saying ‘This account is currently not available.’ This message can be changed with the file /etc/nologin.txt.
/bin/false is just a binary that immediately exits, returning false, when it’s called, so when someone who has false as shell logs in, they’re immediately logged out when false exits.
Setting the shell to /bin/true has the same effect of not allowing someone to log in but false is probably used as a convention over true since it’s much better at conveying the concept that person doesn’t have a shell.
refrence

创建虚拟用户(ftp登录的帐号)

下面的命令将创建一个用户名为ftp01的用户,将其信息写入到/etc/pure-ftpd/pureftpd.passwd中。
这个ftp01用户,就是虚拟用户。系统中不必(也不应该)存在这个用户。这个用户的权限,与我们通过-u-g参数指定的系统账户相同。
期间,pure-pw会要求你输入这个用户的 ftp 密码。

1
2
3
4
pure-pw useradd ftp01 -u ftp -g ftpgroup -d /home/ftp/ftp01
mkdir -p /home/ftp/ftp01
setfacl -d --set group:ftpgroup:rw user:ftp:rwx /home/ftp/
mkdir /home/ftp/ftp01

将用户信息更新到/etc/pure-ftpd/pureftpd.pdb文件:否则ftp01登录时无法进行验证

1
pure-pw mkdb

使用pureDB验证方式

auth中指定PureDB若不设置则虚拟用户无法登录,弹出503验证失败

  • 方法1
    1
    sudo ln -s /etc/pure-ftpd/conf/PureDB  /etc/pure-ftpd/auth/60puredb
  • 方法2
    1
    pure-pw usermod ssi -u ftp -g ftp -m

pure-pw command

1
2
3
4
5
6
7
8
# cnage user's password
pure-pw userdel ftp01

# change ftp's password
pure-pw passwd <user>

# 限制IP地址「-r 为允许地址 -R 为禁止IP地址」
pure-pw usermod ftp01 -r 192.168.100.96/27 -R 192.168.0.1/0

start pure-ftp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
echo '# /etc/systemd/system/pure-ftpd.service
# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
After=remote-fs.target

[Service]
Type=simple
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=yes
RemainAfterExit=yes
ExecStart=/usr/sbin/pure-ftpd /etc/pure-ftpd/pure-ftpd.conf
ExecStop=killall pure-ftpd
' > /etc/systemd/system/pure-ftpd.service
chmod -x /etc/init.d/pure-ftpd
systemctl daemon-reload
systemctl start pure-ftpd

Test Ftp server

install lftp

1
apt-get install lftp

connect the ftp server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# lftp username@ip
lftp [email protected]
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1001 ftpgroup 0 May 6 21:10 a
drwxr-xr-x 2 1001 ftpgroup 4096 May 6 21:35 ab
lftp [email protected]:/>
```sh

# Error

## 503登录验证错误

请查看[使用pureDB验证方式](#使用puredb验证方式)

## 530 Sorry, but I can’t trust you

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

解决方案有两个:
1. 修改 MinUID 的值使其小于 ftp 帐号的值(我的是14,所以要修改成13或更小),
2. 修改 ftp 帐号的 UID 。

# pure-ftpd over ssl

启用*Pure-FTPd SSL/TLS*连接方式在安装时需要检查以下两项:
1. 系统中是否已经安装了**openssl**和**openssl-devel**包
2. 在编译*Pure-FTPd*的时候需要加载*--with-tls*

## 证书制作

编译安装后的 Pure-FTPd 默认的证书存储位置为:/etc/ssl/private/pure-ftpd.pem,该文件会在 Pure-FTPd 启动时自动加载,若证书不存在或路径错误则 Pure-FTPd 启动失败。可以检查 /var/log/message 日志,其中记录了 Pure-FTPd 启动时的报错信息。

```sh
:~$ mkdir -p /etc/ssl/private
:~$ openssl req -x509 -nodes -days 7200 -newkey rsa:2048 -keyout /etc/ssl/private/pure-ftpd.pem -out /etc/ssl/private/pure-ftpd.pem
(按照提示输入证书的相关信息)
:~$ openssl dhparam -out /etc/ssl/private/pure-ftpd-dhparams.pem 2048
:~$ chmod 600 /etc/ssl/private/*.pem

配置 Pure-FTPd

Pure-FTPd配置文件pure-ftpd.conf中配置所需使用的登录方式。

1
2
3
4
5
TLS        0|1|2
# 其中:
# 0:禁用 SSL/TLS 登录模式;
# 1:同时支持普通模式登录和 SSL/TLS 模式登录;
# 2:仅支持有效的 SSL/TLS 登录模式(推荐!)。