安装_CENTOS_FTP

1 安装 vsftpd

1
2
3
4
5
6
7
8
# 安装vsftpd
yum install -y vsftpd
# 设置开机启动
chkconfig vsftpd on
# 重启
service vsftpd restart
# 查看vsftpd服务的状态
service vsftpd status

2 配置 vsftpd.conf

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
# 备份配置文件
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
# 执行以下命令修改配置文件
sed -i "s/anonymous_enable=YES/anonymous_enable=YES/g" '/etc/vsftpd/vsftpd.conf'
sed -i "s/#anon_upload_enable=YES/anon_upload_enable=NO/g" '/etc/vsftpd/vsftpd.conf'
sed -i "s/#anon_mkdir_write_enable=YES/anon_mkdir_write_enable=NO/g" '/etc/vsftpd/vsftpd.conf'
sed -i "s/#chown_uploads=YES/chown_uploads=NO/g" '/etc/vsftpd/vsftpd.conf'
sed -i "s/#async_abor_enable=YES/async_abor_enable=YES/g" '/etc/vsftpd/vsftpd.conf'
sed -i "s/#ascii_upload_enable=YES/ascii_upload_enable=YES/g" '/etc/vsftpd/vsftpd.conf'
sed -i "s/#ascii_download_enable=YES/ascii_download_enable=YES/g" '/etc/vsftpd/vsftpd.conf'
sed -i "s/#ftpd_banner=Welcome to blah FTP service./ftpd_banner=Welcome to FTP service./g" '/etc/vsftpd/vsftpd.conf'

#添加下列内容到vsftpd.conf末尾
use_localtime=YES
listen_port=21
chroot_local_user=YES
idle_session_timeout=300
guest_enable=YES
guest_username=vsftpd
user_config_dir=/etc/vsftpd/configusers
data_connection_timeout=1
virtual_use_local_privs=YES
pasv_min_port=10060
pasv_max_port=10090
accept_timeout=5
connect_timeout=1

# 匿名用户相关设置
anon_root=/home/vsftpd/public
local_root=/home/vsftpd/public
chroot_local_user=YES
no_anon_password=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO

3 建立用户文件

1
2
3
4
5
6
7
8
9
10
# 第一行用户名,第二行密码,不能使用root为用户名
vi /etc/vsftpd/virtusers
admin
123456
userA
123456
userB
123456
userC
123456

4 生成用户数据文件

1
2
3
4
# 注意如果后面对 /etc/vsftpd/virtusers 用户文件的任意修改比如增加用户, 需要重新执行 db_load 的操作才会生效
db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db
# 设定PAM验证文件,并指定对虚拟用户数据库文件进行读取
chmod 600 /etc/vsftpd/virtusers.db

5 修改/etc/pam.d/vsftpd文件

1
2
3
4
5
6
7
# 修改前先备份 
cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
# 将auth及account的所有配置行均注释掉
vi /etc/pam.d/vsftpd
auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/virtusers
account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/virtusers
# 如果系统为32位,上面改为lib

6 新建系统用户vsftpd,用户目录为/home/vsftpd

1
2
3
#用户登录终端设为/bin/false(即:使之不能登录系统)
useradd vsftpd -d /home/vsftpd -s /bin/false
chown -R vsftpd:vsftpd /home/vsftpd

7 建立虚拟用户个人配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mkdir /etc/vsftpd/configusers
cd /etc/vsftpd/configusers

#这里建立两个虚拟用户配合文件
touch admin

#建立用户根目录
mkdir -p /home/vsftpd/admin/

#编辑admin用户配置文件,内容如下,其他用户类似
vi admin

local_root=/home/vsftpd/admin/
write_enable=YES
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

8 防火墙设置

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
40
41
42
43
44
45
46
47
48
49
50
vi /etc/sysconfig/iptables-config
# 增加nf_conntrack_ftp, 也有文档ip_conntrack_ftp其实是它的别名
IPTABLES_MODULES="nf_conntrack_ftp"

# 保存上面的配置后在控制台执行下面的命令
iptables -A INPUT -p tcp -m tcp --dport 21 -m conntrack --ctstate ESTABLISHED,NEW -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
iptables -A OUTPUT -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
iptables -A INPUT -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
iptables -A OUTPUT -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 10060:10090 -j ACCEPT
iptables -A OUTPUT -m state --state NEW -p tcp --sport 10060:10090 -j ACCEPT
service iptables save
service iptables restart

# 如果以上设置不行则尝试进行下面的操作
# 如果以上设置不行则尝试进行下面的操作
# 如果以上设置不行则尝试进行下面的操作

# 这是替补方案
vi /etc/sysconfig/iptables
# 编辑iptables文件,添加如下内容,开启21端口

# Generated by iptables-save v1.4.7 on Wed Mar 21 13:50:00 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -m comment --comment "Allow ftp connections on port 21" -j ACCEPT
-A INPUT -p tcp -m tcp --dport 20 -m conntrack --ctstate RELATED,ESTABLISHED -m comment --comment "Allow ftp connections on port 20" -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10060:10090 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -m comment --comment "Allow ftp connections on port 21" -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED -m comment --comment "Allow ftp connections on port 20" -j ACCEPT
-A OUTPUT -p tcp -m state --state NEW -m tcp --sport 10060:10090 -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on Wed Mar 21 13:50:00 2018

# 重启防火墙使配置生效
service iptables restart
# 或者
/etc/init.d/iptables restart

9 重启vsftpd服务器

1
service vsftpd restart

10 连接测试

  • 在部署 vsftpd 服务的同一台机器上进行内网测试
    1
    2
    yum install ftp -y
    ftp 127.0.0.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Connected to 127.0.0.1 (127.0.0.1).
220 Welcome to FTP service.
Name (127.0.0.1:root): admin
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (127,0,0,1,39,88).
150 Here comes the directory listing.
226 Directory send OK.

# 至此说明内网可以访问
  • 在部署 vsftpd 服务的同一台机器上进行外网测试 (查看该服务器外网的ip为 10.10.10.10)
1
ftp -p 10.10.10.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Connected to 10.10.10.10 (10.10.10.10).
220 Welcome to FTP service.
Name (10.10.10.10:root): admin
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (10,47,12,111,39,77).
150 Here comes the directory listing.
226 Directory send OK.

# 至此说明外网可以访问

11 Connection refused

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 查看 21 端口是否已开启
netstat -na --ip

# 在客户端测试
$ telnet ip.ip.ip.ip 21
Trying ip.ip.ip.ip...
telnet: connect to address ip.ip.ip.ip: Connection refused
telnet: Unable to connect to remote host

# 发现在客户端连接不上,尝试关闭 vsftpd 服务器的防火墙
service vsftpd stop

# 在客户端重新 telnet
telnet ip.ip.ip.ip 21
Trying ip.ip.ip.ip...
Connected to v600000-97.dds.com.
Escape character is '^]'.
220 Welcome to FTP service.

# 发现连接成功,则问题出在防火墙的配置上
# 可以尝试把防火墙设置为 第8步的一模一样(注意备份)再试试

目录结构与成员权限配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 假设有以下目录结构
/home/vsftpd/
.
└── resources
└── organization
└── abc
└── apps
└── xyz
└── 4.5.1
├── debug
│   ├── android
│   └── ios
└── release
├── android
└── ios
1
2
3
4
5
6
7
8
vi /etc/vsftpd/vsftpd.conf
# 设置匿名用户只能访问 apps 目录
anon_root=/home/vsftpd/resources/organization/abc/apps
# 设置登录用户的默认访问目录 为abc, 权限比匿名用户高一级
local_root=/home/vsftpd/resources/organization/abc

#保存 重启 vsftpd
service vsftpd restart

遇到的问题

  • 发现有的用户可以登录, 但是有的用户报出 ‘Server response: ‘530 Login incorrect.’ ‘ 错误
    1
    2
    3
    4
    5
    # 原因是由于在 /etc/vsftpd/virtusers 中增加/修改/删除了用户, 需要重新执行以下命令, 问题解决
    db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db
    service vsftpd restart

    # 即对 /etc/vsftpd/virtusers 用户文件的任意修改, 需要重新执行 db_load 的操作才会生效

所以正确的新增/修改用户的步骤为

  1. 在 /etc/vsftpd/virtusers 文件中增加用户名密码
  2. 在 /etc/vsftpd/configusers 中新建该用户对应的配置文件
  3. 执行命令刷新数据库
    1
    db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db

上传文件失败 ‘Server response: ‘200 PORT command successful. Consider using PASV.’’

1
2
# 将 /home/vsftpd 本身及其所有子目录子文件的权限和组都改为 vsftpd 用户, 普通用户才会有权限上传文件
chown -R vsftpd:vsftpd /home/vsftpd
1
2
3
4
ls -l
total 8
drwxr-xr-x. 2 vsftpd vsftpd 4096 Mar 21 19:19 android
drwxr-xr-x. 2 vsftpd vsftpd 4096 Mar 21 16:22 ios

参考链接