Linux installation and configuration vsftpd

vsftpd (very secure ftp daemon, very secure FTP daemon) is an FTP service program running on the Linux operating system. It is not only completely open source and free, but also has high security, transmission speed, and support for virtual Features that other FTP service programs do not have, such as user authentication.
vsftpd can be installed with yum:yum install vsftpd

The test environment is RHEL8

1. Related configuration files

/etc/vsftpd/vsftpd.conf:主配置文件
/etc/vsftpd/ftpusers :禁止登陆vsftpd的用户列表文件。
/etc/vsftpd/user_list:禁止或允许使用vsftpd的用户列表文件。
/etc/pam.d/vsftpd : PAM认证文件(此文件中file=/etc/vsftpd/ftpusers字段,指明阻止访问的用户来自/etc/vsftpd/ftpusers文件中的用户)

Two, login method

1. Anonymous user login

The home directory of anonymous users (anonymous, ftp) after login:, /var/ftpthis directory generally has 755 permissions, if it is 777 permissions, an error message will appear
. Download directory /var/ftp/pubof anonymous users :, anonymous users generally work in this directory, this directory needs Set 777 authority root, set SBIT special authority if necessary (allow users to upload but not delete)chmod 1777 /var/ftp

2. Local user login

The home directory of the local user after login: the home directory of the user in the system

3. Virtual user login

Home directory after virtual user login: the home directory of any user designated by the administrator

3. Description of common parameters of the main configuration file

[root@hollowman ~]#  vim  /etc/vsftpd/vsftpd.conf

#匿名用户相关配置
anonymous_enable=NO  #是否允许匿名登录FTP服务器,如果为YES,则用户可通过用户名ftp或anonymous来登录
anon_upload_enable=YES  #是否允许匿名用户上传文件,当SELinux 为强制模式(enforcing )时,需检查selinux中的allow_ftpd_anon_write, allow_ftpd_full_access规则是否开启
anon_mkdir_write_enable=YES  # 是否允许匿名用户创建目录
anon_root=/var/ftp  #匿名用户的FTP根目录,默认为/var/ftp
anon_other_write_enable=YES	#是否开放匿名用户的其他写入权限(包括重命名、删除等操作权限)
anon_max_rate=0   #设置匿名用户的最大传输速率,单位为B/s,值为0表示不限制
anon_umask=022	 #匿名用户上传文件的umask值

#本地用户相关配置
local_enable=YES  #是否允许本地用户(即linux系统中的用户帐号)登录FTP服务器,当SELinux 为强制模式(enforcing )时,需检查selinux中的ftp_home_dir规则是否开启
write_enable=YES  # 是否允许本地用户对FTP服务器文件具有写权限,默认设置为YES允许
local_max_rate=0   #设置本地用户的最大传输速率,单位为B/s,值为0时表示不限制
local_umask=022   #本地用户上传文件的umask值,其中022和077最为常见

#虚拟用户有关配置
guest_enable=YES    #设置启用虚拟用户功能
guest_username=vsftpduser    #指定虚拟用户的宿主用户
user_config_dir=/etc/vsftpd/vsftpduser    #指定虚拟用户的配置文件存放目录,该目录下保存着单个虚拟用户的配置文件


#其他有关全局配置
dirmessage_enable=YES   #是否激活目录欢迎信息功能,一般欢迎信息是通过该目录下的.message文件(用户自己建立)获得的
ftpd_banner=Welcome to blah FTP service.  #设置登陆显示文字信息

xferlog_enable=YES  # 是否开启日志功能
xferlog_file=/var/log/xferlog  # 设置日志文件存储路径,默认为/var/log/xferlog
xferlog_std_format=YES  # 设置日志文件的标准格式,/var/log/xferlog 日志文件使用的是xferlog_std_format标准

connect_from_port_20=YES  #数据传输端口开启,注意确保ftp-data数据传输端口为20 

# 设定匿名用户上传文件的属主。注意,不推荐使用root用户上传文件
#chown_uploads=YES
#chown_username=whoever

# 是否以ASCII方式传输数据。启用ASCII方式传输数据,可能会导致由"SIZE /big/file"方式引起的DoS攻击
#ascii_upload_enable=YES
#ascii_download_enable=YES

idle_session_timeout=600  #设置空闲或者无操作时自动退出登陆的时间
data_connection_timeout=120  #设置数据连接超时时间
ls_recurse_enable=YES # 是否允许递归查询。
listen=NO   #是否开启监听(不支持ipv6),如开启,则由vsftpd自己监听ipv4端口
listen_ipv6=YES   #是否开启ipv6端口监听
listen_address=IP   #设置要监听的IP地址
listen_port=端口号  #设置FTP服务的监听端口
max_client=0    #设置FTP服务器所允许的最大客户端连接数,值为0时表示不限制
max_per_ip=0    #设置对于同一IP地址允许的最大客户端连接数,值为0时表示不限制

chroot_local_user=YES  # 是否将用户锁定在自身的FTP目录。当chroot_local_user=YES,表示锁定开启。
chroot_list_enable=YES  # 当chroot_list_enable=YES则表示/etc/vsftpd/chroot_list列表中的用户锁定在自身的FTP目录,否则,列表以外的用户锁定在自身的FTP目录。
chroot_list_file=/etc/vsftpd/chroot_list  # 控制用户是否可以访问自身FTP目录以外的目录,文件内为用户列表

deny_email_enable=YES # 邮件黑名单设置
banned_email_file=/etc/vsftpd/banned_emails  # 邮件黑名单存储路径

pam_service_name=vsftpd  # 设置PAM可插拔认证模块的配置文件名,即/etc/pam.d/vsftpd文件(一般将vsftpd文件作为本地用户认证模块名,而将vsftpd.vu作为虚拟用户认证模块名)
# 此文件中file=/etc/vsftpd/ftpusers字段,说明了PAM模块能抵挡的帐号内容来自文件/etc/vsftpd/ftpusers中

userlist_enable=YES   #是否使得userlist_deny有效以及user_list文件生效

Four, virtual user login configuration steps

1. Create a virtual user

#Create vsftpd virtual user list vuser.list, and add two vsftpd virtual users vuser1 and vuser2, the passwords are both redhat

[root@hollowman ~]# cd /etc/vsftpd/
[root@hollowman vsftpd]# vim vuser.list  
vuser1
redhat
vuser2
redhat

Convert the vuser.list data to form a database file vuser.dbthat can be used for ftp authentication . It is recommended to delete vuser.listand reduce the permissions of vuser.db (600 permissions).
Command syntax:db_load -T -t hash -f user user.db

-T 允许非Berkeley DB的应用程序使用文本格式转换的DB数据文件
-t hash   读取文件的基本方
-f 指定数据源文件也就是vuser.list
[root@hollowman vsftpd]# db_load -T -t hash -f vuser.list vuser.db

2. Create a local user, prohibit logging in to the system, specify a home directory and give 755 permissions. In order to use the home directory as the home directory after the virtual user logs in.

[root@hollowman vsftpd]# useradd -d /var/ftphome -s /sbin/nologin vsftpduser
[root@hollowman vsftpd]# chmod -Rf 755 /var/ftphome

3. Create a pam file vsftpd.vu that supports vuser.db

The pam file is in the /etc/pam.d/ directory, the file name should be the same as the parameter of pam_service_name in the main configuration file, that ispam_service_name=vsftpd.vu

[root@hollowman vsftpd]# vim /etc/pam.d/vsftpd.vu

auth       required     pam_userdb.so  db=/etc/vsftpd/vuser
account    required     pam_userdb.so  db=/etc/vsftpd/vuser
#这里的vuser指向的就是vuser.db,只是省略了文件的后缀。

4. Configure the main configuration file parameters

[root@hollowman vsftpd]# vim vsftpd.conf

anonymous_enable=NO

local_enable=YES
local_umask=022
write_enable=YES

guest_enable=YES
guest_username=vsftpduser
allow_writeable_chroot=YES

dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES

listen=NO
listen_ipv6=YES

pam_service_name=vsftpd.vu
userlist_enable=YES

5. Set virtual user permissions

At this time, you can log in with a virtual user name and password, but only the read permission requires further permission settings to achieve the desired effect.

1). Name and create a new virtual user directory vsftpduser, which is generally easier to remember in the working directory of vsftpd
[root@hollowman vsftpd]# mkdir vsftpduser
2) In the virtual user directory, create a new virtual user configuration file with the same name as the virtual user, namely vuser1 and vuser2
[root@hollowman vsftpd]# cd vsftpduser/
[root@hollowman vsftpduser]# touch vuser1
[root@hollowman vsftpduser]# touch vuser2
3) Configure the virtual user profile. Only vuser1 is configured here. The configuration of the virtual user profile uses anonymous user configuration parameters.
[root@hollowman vsftpduser]# vim vuser1

anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
4) Go back to the main configuration file and add the virtual user configuration directory information in the appropriate location
[root@hollowman vsftpduser]# cd ..
[root@hollowman vsftpd]# vim vsftpd.conf

user_config_dir=/etc/vsftpd/vsftpduser

6. Remember to modify selinux rules and set ftpd_full_access to on

[root@hollowman vsftpd]# setsebool -P ftpd_full_access on

7. Restart the vsftpd service

[root@hollowman vsftpd]# systemctl restart vsftpd

8. Use vuser1 virtual user to log in to the ftp server to access

[root@hollowman vsftpd]# ftp 192.168.100.100
Connected to 192.168.100.100 (192.168.100.100).
220 (vsFTPd 3.0.3)
Name (192.168.100.100:root): vuser1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir testdir   #创建目录
257 "/testdir" created
ftp> ls
227 Entering Passive Mode (192,168,100,100,236,230).
150 Here comes the directory listing.
drwx------    2 1001     1001            6 Jan 21 23:39 testdir
226 Directory send OK.
ftp> rmdir testdir     #删除目录
250 Remove directory operation successful.
ftp> ls
227 Entering Passive Mode (192,168,100,100,208,14).
150 Here comes the directory listing.
226 Directory send OK.
ftp> 

Try to log in with vuser2, you can find that you cannot create and delete directories.

5. Relevant instructions

1. Parameter description of user login permission

1) The users in the ftpusers file (permanent blacklist) are always prohibited from logging in to vsftp
2) Only when userlist_enable=YES, userlist_deny is valid, and the user_list file is valid.
3) The user in the user_list file can be a login-forbidden user, or a "only" allowed login user (at this time other users are forbidden to log-in users, of course, the user_list file must be in effect)
#允许所有用户(ftpusers用户除外)登陆vsftp
userlist_enable=NO

#禁止user_list用户(黑名单)登陆,也就是允许所有用户中除ftpusers用户和user_list用户以外的用户登陆。
userlist_enable=YES
userlist_deny=YES

#只允许user_list用户列表中用户登陆(ftpusers中用户除外)
userlist_enable=YES
userlist_deny=NO 

2. umask action

When creating a directory, the permissions given to the directory are: 777 - umaskvalue (when umask=022, the permission to create the directory is 755)
When creating a file, the permissions given to the file are: 666 - umaskvalue (when umask=022, create File permissions are 644)

[root@hollowman ~]# cd /var/ftp
[root@hollowman ftp]# mkdir dir1
[root@hollowman ftp]# touch file1
[root@hollowman ftp]# ls -l
total 0
drwxr-xr-x. 2 root root  6 Jan 13 07:16 dir1   #新创建的目录权限为755
-rw-r--r--. 1 root root  0 Jan 13 07:17 file1   #新创建的文件权限为644
drwxrwxrwx. 4 root root 26 Jan 13 07:07 pub

3. The meaning of the digital code in the FTP login interface

110 重新启动标记应答。
120 服务在多久时间内ready。
125 数据链路端口开启,准备传送。
150 文件状态正常,开启数据连接端口。
200 命令执行成功。
202 命令执行失败。
211 系统状态或是系统求助响应。
212 目录的状态。
213 文件的状态。
214 求助的讯息。
215 名称系统类型。
220 新的联机服务ready。
221 服务的控制连接端口关闭,可以注销。
225 数据连结开启,但无传输动作。
226 关闭数据连接端口,请求的文件操作成功。
227 进入passive mode。
230 使用者登入。
250 请求的文件操作完成。
257 显示目前的路径名称。
331 用户名称正确,需要密码。
332 登入时需要账号信息。
350 请求的操作需要进一部的命令。
421 无法提供服务,关闭控制连结。
425 无法开启数据链路。
426 关闭联机,终止传输。
450 请求的操作未执行。
451 命令终止:有本地的错误。
452 未执行命令:磁盘空间不足。
500 格式错误,无法识别命令。
501 参数语法错误。
502 命令执行失败。
503 命令顺序错误。
504 命令所接的参数不正确。
530 未登入。 
532 储存文件需要账户登入。
550 未执行请求的操作。
551 请求的命令终止,类型未知。
552 请求的文件终止,储存位溢出。 
553 未执行请求的的命令,名称不正确

Guess you like

Origin blog.csdn.net/ymz641/article/details/112975191