Centos7 安装配置FTP服务
1、FTP简介
ftp(File Transfer Protocol文件传输协议)是基于TCP/IP 协议的应用层协议,用于文件的传输,包括ftp服务器(或服务端)和ftp客户端
FTP协议会在客户端和服务端创建两个连接,一个用于命令传输,一个用于数据传输。
主动模式和被动模式是面向服务端和数据传输来讲的。对于命令传输,都是客户端主动连接服务端。
主动模式:客户端创建一个listen端口,服务端主动连接,建立数据传输通道
被动模式:服务端创建一个listen端口,客户端主动链接,建立数据传输通道。
站在服务端的角度:
主动:客户端你来建立端口,我来连接。
被动:我建立好端口了,你来连接我。
2、关闭防火墙设置selinux
systemclt stop firewalld.service && systemclt disable firewalld.service
SELINUX=disabled
setenforce 0使修改立即生效
3、安装FTP
yum install vsftpd* -y
打印vsftpd安装后的配置文件路径
rpm –ql vsftpd | more
4、修改配置文件
vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_file=/var/log/xferlog
xferlog_std_format=YES
chroot_local_user=YES
chroot_list_enable=NO
allow_writeable_chroot=YES
chroot_list_file=/etc/vsftpd/chroot_list
listen=NO
listen_ipv6=YES
port_enable=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
pasv_min_port=64000
pasv_max_port=65000
local_root=/data/ftp/
# 是否开启匿名用户,匿名都不安全,默认NO
anonymous_enable=NO
# 允许本机账号登录FTP
# 这个设定值必须要为YES时,在/etc/passwd内的账号才能以实体用户的方式登入我们的vsftpd主机
local_enable=YES
# 允许账号都有写操作
write_enable=YES
# 本地用户创建文件或目录的掩码
# 意思是指:文件目录权限:777-022=755,文件权限:666-022=644
local_umask=022
# 进入某个目录的时候,是否在客户端提示一下
dirmessage_enable=YES
# 当设定为YES时,使用者上传与下载日志都会被记录起来
xferlog_enable=YES
# 日志成为std格式
xferlog_std_format=YES
# 上传与下载日志存放路径
xferlog_file=/var/log/xferlog
# 开放port模式的20端口的连接
connect_from_port_20=YES
# 关于系统安全的设定值:
# ascii_download_enable=YES(NO)
# 如果设定为YES,那么client就可以使用ASCII格式下载档案
# 一般来说,由于启动了这个设定项目可能会导致DoS的攻击,因此预设是NO
# ascii_upload_enable=YES(NO)
# 与上一个设定类似的,只是这个设定针对上传而言,预设是NO
ascii_upload_enable=NO
ascii_download_enable=NO
# 通过搭配能实现以下几种效果:
# ①当chroot_list_enable=YES,chroot_local_user=YES时,在/etc/vsftpd/chroot_list文件中列出的用户,可以切换到其他目录;未在文件中列出的用户,不能切换到其他目录
# ②当chroot_list_enable=YES,chroot_local_user=NO时,在/etc/vsftpd/chroot_list文件中列出的用户,不能切换到其他目录;未在文件中列出的用户,可以切换到其他目录
# ③当chroot_list_enable=NO,chroot_local_user=YES时,所有的用户均不能切换到其他目录
# ④当chroot_list_enable=NO,chroot_local_user=NO时,所有的用户均可以切换到其他目录
# 限制用户只能在自己的目录活动
chroot_local_user=YES
chroot_list_enable=NO
chroot_list_file=/etc/vsftpd/chroot_list
# 可以更改ftp的端口号,使用默认值21
# listen_port=60021
# 监听ipv4端口,开了这个就说明vsftpd可以独立运行,不用依赖其他服务
listen=NO
# 监听ipv6端口
listen_ipv6=YES
# 打开主动模式
port_enable=YES
# 启动被动式联机(passivemode)
pasv_enable=YES
# 被动模式端口范围:注意:linux客户端默认使用被动模式,windows 客户端默认使用主动模式。在ftp客户端中执行"passive"来切换数据通道的模式。也可以使用"ftp -A ip"直接使用主动模式。主动模式、被动模式是有客户端来指定的
# 上面两个是与passive mode使用的port number有关,如果您想要使用64000到65000这1000个port来进行被动式资料的连接,可以这样设定
# 这两项定义了可以同时执行下载链接的数量
# 被动模式起始端口,0为随机分配
pasv_min_port=64000
# 被动模式结束端口,0为随机分配
pasv_max_port=65000
# 文件末尾添加
# 这个是pam模块的名称,我们放置在/etc/pam.d/vsftpd,认证用
pam_service_name=vsftpd
# 使用允许登录的名单,在/etc/vsftpd/user_list文件中添加新建的用户ftpuser
userlist_enable=YES
# 限制允许登录的名单,前提是userlist_enable=YES,其实这里有点怪,禁止访问名单在/etc/vsftpd/ftpusers
userlist_deny=NO
# 允许限制在自己的目录活动的用户拥有写权限
# 不添加下面这个会报错:500 OOPS: vsftpd: refusing to run with writable root inside chroot()
allow_writeable_chroot=YES
# 当然我们都习惯支持TCP Wrappers的啦
# Tcp wrappers : Transmission Control Protocol (TCP) Wrappers 为由 inetd 生成的服务提供了增强的安全性
tcp_wrappers=YES
# FTP访问目录
local_root=/data/ftp/ftpuser
5、创建ftp用户和目录
useradd -d /data/ftp/ -s /sbin/nologin ftpuser
passwd ftpuser
mkdir -pv /data/ftp
chown -R ftpuser /data/ftp
6、启动ftp服务
systemctl enable vsftpd.service && systemctl start vsftpd.service && systemctl status vsftpd.service
7、报错修改
ftp 192.168.33.207
530 Login incorrect.
Login failed.
vim /etc/pam.d/vsftpd
注释这一行
auth required pam_shells.so
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
vim /etc/vsftpd/vsftpd.conf
添加
allow_writeable_chroot=YES
8、登陆成功
9、配置防火墙策略
systemctl enable firewalld.service # 重启防火墙开机自启动
systemctl restart firewalld.service # 重启防火墙服务
firewall-cmd --version # 查看防火墙版本
firewall-cmd --list-all # 查看已开放的端口
firewall-cmd --permanent --zone=public --add-service=ftp # 防火墙开通ftp服务
firewall-cmd --permanent --zone=public --add-port=21/tcp # 开通ftp服务21命令控制端口
# 主动模式下数据传输端口等于命令控制端口-1 ======> 21 - 1 = 20
# 开通ftp服务主动模式的20数据传输端口
firewall-cmd --permanent --zone=public --add-port=20/tcp
# 开通ftp服务被动模式的数据端口范围
firewall-cmd --permanent --zone=public --add-port=64000-65000/tcp
firewall-cmd --reload # 刷新防火墙,重新载入
#————————————————————————————————firewall端口管理————————————————————————————————————————————————
#开放端口
firewall-cmd --zone=public --add-port=5672/tcp
#永久开放端口
firewall-cmd --zone=public --add-port=5672/tcp --permanent
#关闭端口
firewall-cmd --zone=public --remove-port=5672/tcp --permanent
# 配置立即生效
firewall-cmd --reload
#查看防火墙
firewall-cmd --list-all
#查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
——————————————————————————————————————————————————————————————————————————————————————————————————
# 设置关闭SELinux对ftp的限制
setsebool -P ftpd_full_access on
sed -i s#enforcing#disabled#g /etc/sysconfig/selinux
setenforce 0 && getenforce
getenforce
10、修改FTP服务默认端口号
vim /etc/vsftpd/vsftpd.conf
添加
listen_port=8021
ftp_data_port=8020
vim /etc/services
找到ftp选项并将21修改成你设置的端口
重启服务
systemctl restart vsftpd.service
配置文件(修改无误)
vim /etc/vsftpd/vsftpd.conf
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#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. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# 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_enabl=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=NO
allow_writeable_chroot=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.
listen=NO
#
# 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
port_enable=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
pasv_min_port=50000
pasv_max_port=50002
#FTP访问目录
local_root=/data/ftp/
listen_port=8021
ftp_data_port=8020