Installation and configuration of FTP under CentOS

1. FTP download and installation

1.ftp download

yum install -y vsftpd

2.ftp start

systemctl enable vsftpd.service       #开机自启
systemctl start vsftpd.service        #启动
systemctl stop vsftpd.service         #停止
systemctl status vsftpd.service       #状态

3. Configure FTP

3.1 Open the configuration file

vim /etc/vsftpd/vsftpd.conf

3.2 Modify the content

#显示行号
:set number
#修改配置 12 行
anonymous_enable=NO
#修改配置 33 行
anon_mkdir_write_enable=YES
#修改配置48行
chown_uploads=YES
#修改配置72行
async_abor_enable=YES
#修改配置83行
ascii_upload_enable=YES
#修改配置84行
ascii_download_enable=YES
#修改配置87行
ftpd_banner=Welcome to blah FTP service.
#修改配置101行
chroot_local_user=YES

3.3 Add the following content to the end of 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=1
allow_writeable_chroot=YES

4. Create a virtual user file

4.1 Create and edit user files

vim /etc/vsftpd/ftpuser_list
#第一行为用户名,第二行为密码。不能使用root作为用户名 
southgis
southgis@123

4.2 Generate virtual user data files

db_load -T -t hash -f /etc/vsftpd/ftpuser_list /etc/vsftpd/ftpuser_list.db
#设定PAM验证文件,并指定对虚拟用户数据库文件进行读取
chmod 600 /etc/vsftpd/ftpuser_list.db

4.3 Modify the /etc/pam.d/vsftpd file

# 修改前先备份
cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak

vi /etc/pam.d/vsftpd
#先将配置文件中原有的 auth 及 account 的所有配置行均注释掉
#写入以下内容
auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/ftpuser_list
account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/ftpuser_list
# 如果系统为32位,上面改为lib

5. Create a new system user vsftpd, the user directory is /data/ftp

5.1 Create a new system user vsftpd, the user directory is /data/ftp

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

5.2 Establish virtual user personal configuration file

mkdir /etc/vsftpd/ftpuser_conf
cd /etc/vsftpd/ftpuser_conf
#编辑leo用户配置文件,内容如下,其他用户类似
vi southgis
local_root=/home/southgisdata/imap-service-publish
write_enable=YES
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

6. Firewall settings

6.1 How to set up 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

6.2 How to set up 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

7. Use the ftp tool to connect to the test

At this time, when using the ftp tool to connect, we found that the connection was possible. When transferring files, you will find that problems such as 500, 503, and 200 will appear when uploading and downloading files. At this time, you can perform the following operations:

Method 1. Close SELINUX

打开SELINUX配置文件
vim /etc/selinux/config
修改配置参数
注释 
SELINUX=enforcing
增加 
SELINUX=disabled
#修改完成后,需要重启!

Method 2. Modify 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模式

Method three: SELINUX does not impose any restrictions on vsftp

setsebool -P ftpd_connect_all_unreserved 1
这个时候再使用工具连接,你发现,就可以正常的上传和下载文件了。
如果还是有问题尝试给我们用户的ftp目录,设置一下操作权限
chmod -R 775 /home/vsftpd/leo

8. Virtual user principle

Because under Linux, after using vsftp to create a user, when accessing using ftp by default, the corresponding user's home directory will be accessed. If you want multiple users to access a certain directory at the same time and have different permissions on the same directory, for example, some users can only view but not modify, or some users can only download but not upload these permissions, these settings can only be set through vsftp Virtual users in the system are required to perform settings. Ordinary users cannot achieve this effect.
Therefore, first create a common system user and create a home directory, then map all virtual users to the corresponding common system user home directory, and then control the permissions of each virtual user to achieve the above effect.

Create new user

新建用户marility,用户锁定在/var/www/ruibiaofangxuan目录内
mkdir /var/www/ruibiaofangxuan
useradd -d /var/www/ruibiaofangxuan marility
chown marility.marility /var/www/ruibiaofangxuan
chmod u-w /var/www/ruibiaofangxuan
给虚拟用户生成随机密码
[root@marility ~]# tr -cd '[:alnum:]' < /dev/urandom | fold -w32 |head -n1
eFEBgVTfWJ66OhQ3rTuGB4kt5k5r0aMW

9. vsftp related configuration file settings

[root@marility vsftpd]# grep ^[^#] /etc/vsftpd/vsftpd.conf
anonymous_enable=NO          ##禁止匿名用户
local_enable=YES             ##开启本地账户,虚拟账户需要映射到本地账户
write_enable=YES           
local_umask=022              ##本地用户的umask值
anon_umask=022               ##虚拟用户的umask值设定
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_file=/var/log/xferlog        ##日志路径
xferlog_std_format=YES
acii_upload_enable=YES               ##允许上传ascii格式文件
ascii_download_enable=YES
ftpd_banner=Welcome to blah FTP service.
chroot_local_user=YES               ##禁锢用户在家目录
chroot_list_enable=NO               ##不启用本地禁用目录,使用虚拟用户时不需要开户本地列表过滤
listen=YES                          ##监听ipv4
listen_ipv6=NO
allow_writeable_chroot=YES          ##可以写,因为禁锢家目录需去掉用户写权限
tcp_wrappers=YES
guest_enable=YES                    ##开启虚拟账户功能
guest_username=marility             ##虚拟账号映射到本地哪个用户
pam_service_name=ftp                ##加载pam.d中哪个文件的机制
user_config_dir=/etc/vsftpd/config                 ##虚拟用户的单个权限
[root@marility vsftpd]# cat /etc/vsftpd/vuser_list
ruibiaofangxuan                                    ##账户
eFEBgVTfWJ66OhQ3rTuGB4kt5k5r0aMW                   ##rubiaofangxuan的密码
admini                                             ##账户
adminipw                                           ##admini的密码

10. Convert the password file to db format

[root@marility vsftpd]# db_load -T -t hash -f vuser_list /etc/vsftpd/vuser_list.db
没有db命令的可以先yum -y install db4-utils
然后再改变权限chmod 600 /etc/vsftpd/vuser_list.db
用户密码设定及db解析的设定
[root@marility vsftpd]# cat /etc/pam.d/ftp
auth  required  /lib64/security/pam_userdb.so  db=/etc/vsftpd/vuser_list
account required  /lib64/security/pam_userdb.so  db=/etc/vsftpd/vuser_list
auth是指对用户的用户名口令进行验证。
accout是指对用户的帐户有哪些权限哪些限制进行验证。
再后面的/lib/security/pam_userdb.so表示该条审核将调用pam_userdb.so这个库函数进行。
注意该函数会根据系统的位数而所在位置不同,可以通过rpm –ql pam查看
同时db指向的文件实际上指向的vuser_list.db ,但默认省略db后缀,故上述db的指向时在配置的时候不能写为vuser_list.db

11. Set permissions for virtual users

#注解
[root@marility vsftpd]# cat /etc/vsftpd/config/admini
anon_world_readable_only=NO                  ##关闭只可读
anon_upload_enable=YES                       ##允许上传
anon_mkdir_write_enable=YES                  ##允许新建目录
anon_other_write_enable=YES                  ##允许修改目录/文件名称,删除
local_root=/var/www/ruibiaofangxuan/home     ##家目录映射

#示例
[root@marility vsftpd]# cat /etc/vsftpd/config/ruibiaofangxuan
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
local_root=/var/www/ruibiaofangxuan/home

For security reasons, the user needs to be locked in the home directory, so the chroot_local_user=YES function is turned on in vsftpd.conf. At the same time, the user's write permission on the home directory needs to be turned off. After setting this way, the virtual user cannot also access the mapped
user's permissions. Write data to the home directory.
Therefore, you can create a new directory /var/www/ruibiaofangxuan/home under marility's home directory and
change the owner group of this directory to marility. At the same time, use chmod to change the permissions
in the /etc/vsftpd/config file. Then set the mapping path local_root of the two users to the created directory.
Systemctl restart vsftpd
systemctl enable vsftpd
restarts the service. That is,
ordinary users can only download and upload data, while the admini user has all permissions and is also locked in /var. /www/ruibiaofangxuanIn this directory

2. Detailed explanation of configuration files

#示例配置文件/etc/vsftpd/vsftpd.conf
#
#在设置中编译的默认值非常偏执。这个样本文件
#稍微放松一下,使ftp守护程序更可用。
#请参阅vsftpd.conf.5以了解所有默认编译的内容。
#
#阅读:此示例文件不是vsftpd选项的详尽列表。
#请阅读vsftpd.conf.5手册页以全面了解vsftpd
#功能。
#
#允许匿名FTP? (当心-如果您注释掉,默认情况下允许)。
aonymous_enable =是
#
#取消注释以允许本地用户登录。
#当SELinux强制执行时,检查SE bool ftp_home_dir
local_enable =是
#
#取消注释此选项以启用任何形式的FTP写命令。
write_enable = YES
#
#本地用户的默认umask为077。您可能希望将其更改为022,
#如果您的用户期望(大多数其他ftpd使用022)
local_umask = 022
#
#取消注释以允许匿名FTP用户上传文件。仅此
如果上述全局写使能被激活,#将起作用。另外,你会
#显然需要创建一个FTP用户可写的目录。
#当SELinux强制执行SE bool allow_ftpd_anon_write,allow_ftpd_full_access检查时
#anon_upload_enable =是
#
#如果希望匿名FTP用户能够创建,请取消注释
#个新目录。
#anon_mkdir_write_enable =是
#
#激活目录消息-远程用户在远程用户收到的消息
#进入某个目录。
dirmessage_enable =是
#
#激活上载/下载的日志记录。
xferlog_enable =是
#
#确保PORT传输连接来自端口20(ftp-data)。
connect_from_port_20 =是
#
#如果需要,您可以安排上载的匿名文件归所有者所有
#其他用户。注意!不能使用“ root”来上传文件
# 推荐的!
#chown_uploads =是
#chown_username =谁
#
#如果愿意,您可以覆盖日志文件所在的位置。显示默认值
#下面。
#xferlog_file = /var/log/xferlog
#
#如果需要,可以将日志文件设置为标准ftpd xferlog格式。
#请注意,在这种情况下,默认日志文件位置为/ var / log / xferlog。
xferlog_std_format =是
#
#您可以更改默认值以使空闲会话超时。
#idle_session_timeout = 600
#
#您可以更改默认值以使数据连接超时。
#data_connection_timeout = 120
#
#建议您在系统上定义一个唯一的用户,
#ftp服务器可以用作完全隔离且没有特权的用户。
#nopriv_user = ftp安全
#
#启用此选项,服务器将识别异步ABOR请求。不
#为安全起见,建议使用此代码(此代码不重要)。不启用它,
#但是,可能会使较旧的FTP客户端感到困惑。
#async_abor_enable =是
#
#默认情况下,服务器将假装允许ASCII模式,但实际上忽略
#请求。打开以下选项以使服务器实际执行ASCII
#在ASCII模式下处理文件。 vsftpd.conf(5)手册页介绍
#禁用这些选项时的行为。
#注意在某些FTP服务器上,ASCII支持会导致拒绝服务
通过命令“ SIZE / big / file”以ASCII模式进行#攻击(DoS)。 vsftpd
#预测了这种攻击,并且一直很安全,报告了
# 原文件。
#ASCII改写是该协议的可怕特征。
#ascii_upload_enable =是
#ascii_download_enable =是
#
#您可以完全自定义登录标题字符串:
#ftpd_banner =欢迎使用FTP服务。
#
#您可以指定一个不允许的匿名电子邮件地址文件。显然地
#对于抵抗某些DoS攻击很有用。
#deny_email_enable =是
#(遵循默认值)
#banned_email_file = / etc / vsftpd / banned_emails
#
#您可以指定本地用户的显式列表,以将chroot()移到其家中
# 目录。如果chroot_local_user为YES,则此列表将成为
#个用户不使用chroot()。
#(警告!chroot'ing可能非常危险。如果使用chroot,请确保
#用户无权访问目录中的顶层目录
#chroot)
#chroot_local_user =是
#chroot_list_enable =是
#(遵循默认值)
#chroot_list_file = / etc / vsftpd / chroot_list
#
#您可以激活内置ls的“ -R”选项。这被禁用
#default避免远程用户在大型服务器上造成过多的I / O
#个网站。但是,假定某些损坏的FTP客户端(例如“ ncftp”和“ mirror”)
#存在“ -R”选项,因此有很强的理由启用它。
#ls_recurse_enable =是
#
#启用“ listen”指令后,vsftpd以独立模式运行,并且
#侦听IPv4套接字。该指令不能结合使用
#使用listen_ipv6指令。
听=否
#
#此指令启用侦听IPv6套接字。默认情况下,监听
在IPv6“任何”地址(::)上的#将接受来自两个IPv6的连接
#和IPv4客户端。无需同时监听* IPv4和IPv6
#个套接字。如果您想要的话(也许是因为您想听特定的
#个地址),则必须使用两个配置运行vsftpd的两个副本
#个文件。
#确保其中一个监听选项已注释!
listen_ipv6 =是

pam_service_name = vsftpd
userlist_enable =是
tcp_wrappers =是

Guess you like

Origin blog.csdn.net/weixin_41166785/article/details/120865224