Centos7搭建FTP服务器

前言

阿里云上使用Ngnix部署了一个静态项目,想通过FTP来管理这个静态项目。搭建前先去了解了一下FTP,FTP是文件传输协议,用于Internet上的控制文件的双向传输,实现服务端和客户端的文件上传和下载。阿里云线上环境是Centos7,这里使用vsftpd来搭建FTP服务。

安装过程

安装vsftpd的服务器及相关软件

安装vsftpd服务器和客户端

yum install ftp vsftpd

安装加密工具

yum install libdb-utils.x86_64

配置vsftpd

创建vsftpd的登录用户和主目录

useradd -s /sbin/nologin znuser

修改密码

passwd znuser

创建vsftpd虚拟用户

1
2
3
4
vi /etc/vsftpd/vftpuser.txt
www
123456

hash加密

db_load -T -t hash -f /etc/vsftpd/vftpuser.txt /etc/vsftpd/vftpuser.db

添加vsftpd的虚拟用户验证

vi /etc/pam.d/vsftpd

把auth和account替换

1
2
3
4
5
6
7
8
9
session optional pam_keyinit.so force revoke
#auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
#auth required pam_shells.so
#auth include password-auth
#account include password-auth
auth required pam_userdb.so db=/etc/vsftpd/vftpuser
account required pam_userdb.so db=/etc/vsftpd/vftpuser
session required pam_loginuid.so
session include password-auth

修改vsftpd的配置文件vsftpd.conf

vi /etc/vsftpd/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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 是否运行匿名登录(NO)
anonymous_enable=NO
# 是否允许本地用户登录
local_enable=YES
# 允许ftp的任何写方式
write_enable=YES
# 文件创建只有的权限是755=(777-022)
local_umask=022
# 是否允许匿名用户上传文件
#anon_upload_enable=YES
# 是否允许匿名用户创建文件或者删除文件
#anon_mkdir_write_enable=YES
# 用户进入目录信息提示
dirmessage_enable=YES
# 是否允许vsftp的日志
xferlog_enable=YES
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
#chown_uploads=YES
#chown_username=whoever
# 日志文件的路径
xferlog_file=/var/log/xferlog
# 日志文件的格式
xferlog_std_format=YES
#
#idle_session_timeout=600
#
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
# 只监听ipv4的地址
listen=YES
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
# 下面是自己添加的
virtual_use_local_privs=YES
guest_enable=YES
guest_username=znuser
# 虚拟用户的配置文件(可以对每一个虚拟用户进行单独的权限配置)
user_config_dir=/etc/vsftpd/vconf/
chroot_local_user=YES
allow_writeable_chroot=YES
启用pasv模式
pasv_enable=YES
#启用被动模式
pasv_min_port=10060 #被动模式使用端口范围
pasv_max_port=10090 #被动模式使用端口范围

创建虚拟用户的权限配置

1
2
3
4
5
6
7
8
9
10
11
mkdir /etc/vsftpd/vconf/
cd /etc/vsftpd/vconf/
vi www
local_root=/home/znuser/www/
write_enable=yes
download_enable=yes
anon_upload_enable=yes
anon_mkdir_write_enable=yes
anon_other_write_enable=yes
anon_world_readable_only=no

配置防火墙

vi /etc/sysconfig/iptables

新增以下内容

1
2
3
-A INPUT -p tcp -m state --state NEW -m tcp --dport 20 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10060:10090 -j ACCEPT

启动vsftpd服务

1
2
systemctl enable vsftpd
systemctl start vsftpd

如果启动失败,查看日志

1
tail -f /var/log/secure

本地测试能否登录,如下图显示成功

搭建中遇到的问题

530 login incorrect

搭建中碰到的唯一的一个问题,花了不少时间才解决。首先检查了用户名密码是否正确,确认正确以后尝试了各种网上碰到这个错误的解决办法还是不行。查看日志里面有这么一段话Unregistered Authentication Agent for unix-process:11386:896659 (system bus name :1.69, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus),尝试了网上提供的解决办法还是没用。最后删除了vsftp重新安装,安装的过程中发现了原因。第一次安装在创建vsftpd虚拟用户的时创建的是vftpuser,而在/etc/pam.d/vsftpd中有那么一段:

1
2
db=/etc/vsftpd/vftpuser
account required pam_userdb.so db=/etc/vsftpd/vftpuser

这里的vftpuser冲突了,所以一直验证失败,将创建的文件改为vftpuser.txt即可解决。

后记

安装好FTP以后就把静态项目放到了虚拟用户目录之下,我这里的路径是/home/znuser/www,然后使用Ngnix将项目路径配置为虚拟用户目录,将静态项目发布出去。如果静态项目需要修改,就可以通过ftp客户端连接到服务器,更新内容。这比直接用ssh连接服务器更加安全,通过ftp客户端登录的人只能访问静态项目的目录,不会对服务器其他地方造成误操作。通过ftp服务可以更好的管理整个服务器。