安装vsftp
#安装
yum install -y vsftpd
#设置开机启动
systemctl enable vsftpd.service
#启动
systemctl start vsftpd.service
#停止
systemctl stop vsftpd.service
#查看状态
systemctl status vsftpd.service
#以上是参考
yum install -y vsftpd
vim /etc/vsftpd/vsftpd.conf
修改配置文件:
#修改配置 12 行
anonymous_enable=NO
#修改配置 33 行
anon_mkdir_write_enable=YES
#修改配置48行
chown_uploads=YES
#修改配置72行
async_abor_enable=YES
#修改配置82行
ascii_upload_enable=YES
#修改配置83行
ascii_download_enable=YES
#修改配置86行
ftpd_banner=Welcome to blah FTP service.
#修改配置100行
chroot_local_user=YES
#添加下列内容到vsftpd.conf末尾
use_localtime=YES
listen_port=21
idle_session_timeout=300
guest_enable=YES
guest_username=vsftpd
user_config_dir=/etc/vsftpd/vconf
data_connection_timeout=1
virtual_use_local_privs=YES
pasv_min_port=40000
pasv_max_port=40010
accept_timeout=5
connect_timeout=10
allow_writeable_chroot=YES
/*也可以直接复制下面的*/
# 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_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.
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
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
use_localtime=YES
listen_port=21
idle_session_timeout=300
guest_enable=YES
guest_username=vsftpd
user_config_dir=/etc/vsftpd/vconf
data_connection_timeout=1
virtual_use_local_privs=YES
pasv_min_port=40000
pasv_max_port=40010
accept_timeout=5
connect_timeout=10
allow_writeable_chroot=YES
/*直接复制上面的将整个替换掉*/
#创建编辑用户文件
vim /etc/vsftpd/virtusers
#第一行为用户名,第二行为密码。不能使用root作为用户名
contentFtps
OrtlOOXX1p
4. 生产用户数据文件:
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 文件
# 修改前先备份
cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
vim /etc/pam.d/vsftpd
#先将配置文件中原有的 auth 及 account 的所有配置行均注释掉
auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/virtusers
account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/virtusers
/*最终内容如下*/
#%PAM-1.0
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
session required pam_loginuid.so
session include password-auth
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,用户目录为 /data/home/vsftp1
#用户登录终端设为/bin/false(即:使之不能登录系统)
(增删用户参考: https://www.imooc.com/article/17776 )
(建立错误删除语法参考: userdel vsftpd)
(删除文件夹语法参考: https://blog.csdn.net/qq_27930635/article/details/88413808 )
(删除文件夹: rm -rf /data/home/vsftp)
useradd vsftpd -d /data/home/vsftp1 -s /bin/false
chown -R vsftpd:vsftpd /data/home/vsftp1
授权:
chmod -R 775 /data/home/vsftp1
7. 建立虚拟用户授权文件
mkdir -p /etc/vsftpd/vconf
cd /etc/vsftpd/vconf
#这里建立虚拟用户leo配置文件
touch vsftp_user_1
#编辑leo用户配置文件,内容如下,其他用户类似
vim vsftp_user_1
local_root=/data/home/vsftp1/vsftp_user_1
write_enable=YES
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
#建立文件夹
mkdir -p /data/home/vsftp1/vsftp_user_1
#授权
chmod -R 775 /data/home/vsftp1/vsftp_user_1
8.防火墙设置:
IPtables 的设置方式:
vi /etc/sysconfig/iptables
#编辑iptables文件,添加如下内容,开启21端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 40000:40010 -j ACCEPT
firewall 的设置方式:
firewall-cmd --zone=public --add-service=ftp --permanent
firewall-cmd --zone=public --add-port=21/tcp --permanent
firewall-cmd --zone=public --add-port=40000-40010/tcp --permanent
9. 重启ftp服务器
10. 使用ftp之类的工具对ftp进行测试;
如果出现 500/503/200 等问题的时候;
可以进行如下配置:
1 关闭 SELINUX
#打开SELINUX配置文件
vim /etc/selinux/config
#修改配置参数
#注释
SELINUX=enforcing
#增加
SELINUX=disabled
#修改完成后,需要重启!
2 修改 SELINUX
setenforce 0 #暂时让SELinux进入Permissive模式
#列出与ftp相关的设置
getsebool -a|grep ftp
#以下是显示出来的权限,off是关闭权限,on是打开权限。不同的机器显示的可能不一样。我看了我的显示的,和网上其他教程就不太一样
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
#将包含有 ftp_home_dir 和 ftpd_full_access 相关的都设置为 1
setsebool -P ftp_home_dir 1
setsebool -P allow_ftpd_anon_write 1
setsebool -P ftp_home_dir 1
setenforce 1 #进入Enforcing模式
方式三、 SELINUX不对vsftp不做任何限制
setsebool -P ftpd_connect_all_unreserved 1
这个时候再使用工具连接,你发现,就可以正常的上传和下载文件了。
如果还是有问题尝试给我们用户的ftp目录,设置一下操作权限
chmod -R 775 /data/home/vsftp1/vsftp_user_1
还是链接不了? 查看防火墙配置:
防火墙设置参考: https://www.cnblogs.com/xxoome/p/7115614.html
firewall-cmd --state