linux-运维进阶-16 vsftpd服务
文件传输协议
FTP协议有下面两种工作模式。
- 主动模式:FTP服务器主动向客户端发起连接请求。
- 被动模式:FTP服务器等待客户端发起连接请求(FTP的默认工作模式)
安装并启动vsftpd
[root@localhost ~]# yum install vsftps -y
[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@localhost ~]# firewall-cmd --add-service=ftp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]#
备份配置文件
[root@localhost ~]# mv /etc/vsftpd/vsftpd.conf{,.bak}
[root@localhost ~]# ls /etc/vsftpd/
ftpusers user_list vsftpd.conf.bak vsftpd_conf_migrate.sh
[root@localhost ~]#
创建新的没有注释的配置文件
[root@localhost ~]# grep -v "#" /etc/vsftpd/vsftpd.conf.bak > /etc/vsftpd/vsftpd.conf
[root@localhost ~]# ls /etc/vsftpd/
ftpusers user_list vsftpd.conf vsftpd.conf.bak vsftpd_conf_migrate.sh
[root@localhost ~]#
查看默认配置文件中的选项
[root@localhost ~]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@localhost ~]#
Vsftpd服务程序
vsftpd作为更加安全的文件传输的服务程序,允许用户以三种认证模式登录到FTP服务器上。
vsftpd使用三大模式:
-
匿名用户模式
是一种最不安全的认证模式,任何人都可以无需密码验证而直接登录到FTP服务器。
-
本地用户模式
是通过Linu系统本地的账户密码信息进行认证的模式,相较于匿名开放模式更安全,而且配置起来也很简单。但是如果被黑客破解了账户的信息,就可以畅通无阻地登录FTP服务器,从而完全控制整台服务器。
-
虚拟用户模式
是这三种模式中最安全的一种认证模式,它需要为FTP服务单独建立用户数据库文件,虚拟出用来进行口令验证的账户信息,而这些账户信息在服务器系统中实际上是不存在的,仅供FTP服务程序进行认证使用。这样,即使黑客破解了账户信息也无法登录服务器,从而有效降低了破坏范围和影响。
匿名开放模式
修改配置文件
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_umask=022
anonymous_enable=YES
anon_umask=022
anon_upload_enable=Yes
anon_mkdir_write_enable=Yes
anon_other_write_enable=Yes
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
保存退出
[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# chown -Rf ftp /var/ftp/pub
[root@localhost ~]# setsebool -P ftpd_full_access=on
[root@localhost ~]#
接下来就可以用windows资源管理器登录上去上传下载文件
本地用户模式
修改配置文件
[root@localhost ~]# 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_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
保存退出
[root@localhost ~]# systemctl restart vsftpd
再次用windows资源管理器登录服务器:
按理来讲,现在已经完全可以本地用户的身份登录FTP服务器了。但是在使用root管理员登录后,发现登陆失败:
可见,我们已经被系统拒绝访问了。这是因为vsftpd服务程序所在的目录中默认存放着两个名为“用户名单”的文件(ftpusers和user_list)。vsftpd服务程序目录中的这两个文件只要里面写有某位用户的名字,就不再允许这位用户登录到FTP服务器上。分别打开 ftpusers 和 user_list 这两个文件,删除里面的root,然后保存并退出
[root@localhost ~]# cat /etc/vsftpd/ftpusers
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@localhost ~]# vim /etc/vsftpd/ftpusers
删除root,保存退出。
[root@localhost ~]# cat /etc/vsftpd/ftpusers
# Users that are not allowed to login via ftp
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@localhost ~]#
[root@localhost ~]# cat /etc/vsftpd/user_list
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@localhost ~]#
[root@localhost ~]# vim /etc/vsftpd/user_list
删除root,保存退出。
[root@localhost ~]# cat /etc/vsftpd/user_list
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@localhost ~]#
现在就可以用本地用户root登录了,本地用户登录上去之后,登录的目录是本地用户的家目录,如下图
虚拟用户模式
创建一个本地用户,用于映射虚拟用户在本地的代理,为了安全起见,禁止这个用户登录linux服务器
[root@localhost ~]# useradd -d /var/ftproot -s /sbin/nologin virtual
创建用于进行FTP认证的用户数据库文件,其中奇数行为账户名,偶数行为密码。
[root@localhost ~]# cd /etc/vsftpd/
[root@localhost vsftpd]# vim vuser.list
aaa
123456
bbb
123456
ccc
123456
保存退出
使用db_load命令用哈希(hash)算法将原始的明文信息文件转换成数据库文件
降低数据库文件的权限(避免其他人看到数据库文件的内容)
把原始的明文信息文件删除。
[root@localhost vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@localhost vsftpd]# file vuser.db
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@localhost vsftpd]# chmod 600 vuser.db
[root@localhost vsftpd]# rm -f vuser.list
[root@localhost vsftpd]#
建立用于支持虚拟用户的PAM文件
[root@localhost 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
保存退出
修改vsftpd配置文件
[root@localhost vsftpd]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
local_enable=YES
guest_enable=YES
guest_username=virtual
allow_writeable_chroot=YES
write_enable=YES
local_umask=022
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
tcp_wrappers=YES
保存退出,重启vsftpd服务
[root@localhost vusers_dir]# systemctl restart vsftpd
再次用windows资源管理器登录服务器,这次我们用bbb用户登录:
登录成功,到了bbb用户的家目录,不过这里什么文件也木有
tftp服务
安装tftp服务
[root@localhost ~]# yum install xinetd tftp-server.x86_64 tftp -y
[root@localhost ~]# firewall-cmd --add-port=69/udp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]#
这里tftp的对应端口号69
修改配置文件
[root@localhost ~]# vim /etc/xinetd.d/tftp
将
disable = yes
改为
disable = no
保存退出
重启服务
[root@localhost ~]# systemctl restart xinetd.service
[root@localhost ~]# systemctl enable xinetd
[root@localhost ~]#
命令 | 作用 |
---|---|
? | 帮助信息 |
put | 上传文件 |
get | 下载文件 |
verbose | 显示详细的处理信息 |
status | 显示当前的状态信息 |
binary | 使用二进制进行传输 |
ascii | 使用ASCII码进行传输 |
timeout | 设置重传的超时时间 |
quit | 退出 |
测试
[root@localhost ~]# echo "hello,tftp" > /var/lib/tftpboot/readme.txt
[root@localhost ~]# tftp 192.168.141.12
tftp> get readme.txt
tftp> quit
[root@localhost ~]# cat readme.txt
hello,tftp
[root@localhost ~]#
本篇到此结束