文件传输协议(File Transfer Protocol,FTP)是用于在网络上进行文件传输的一套标准协议,它工作在 OSI 模型的第七层 即应用层, 使用 TCP 传输而不是 UDP, 客户在和服务器建立连接前要经过一个“三次握手”的过程, 保证客户与服务器之间的连接是可靠的, 而且是面向连接, 为数据传输提供可靠保证。
Vsftpd是linux类操作系统上运行的ftp服务器软件。
vsftpd基本信息如下:
服务名称: vsftpd.service
配置目录: /etc/vsftpd
主配置文件: /etc/vsftpd/vsftpd.conf
默认发布目录: /var/ftp
报错信息:
550 : 程序本身拒绝
553 :文件系统权限限制
500 :权限过大
530 :认证失败
在虚拟机westosa上进行操作实验,安装的前提:
[root@ftp ~]# systemctl disable --now firewalld
[root@ftp ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor >
Active: inactive (dead)
Docs: man:firewalld(1)
直接在仓库安装vsftpd服务即可!安装后并开启服务
[root@ftp ~]# dnf install -y vsftpd
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
AppStream 1.4 MB/s | 3.2 kB 00:00
BaseOS 1.1 MB/s | 2.8 kB 00:00
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
vsftpd x86_64 3.0.3-31.el8 AppStream 180 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 180 k
Installed size: 343 k
Downloading Packages:
vsftpd-3.0.3-31.el8.x86_64.rpm 30 MB/s | 180 kB 00:00
--------------------------------------------------------------------------------
Total 18 MB/s | 180 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : vsftpd-3.0.3-31.el8.x86_64 1/1
Running scriptlet: vsftpd-3.0.3-31.el8.x86_64 1/1
Verifying : vsftpd-3.0.3-31.el8.x86_64 1/1
Installed products updated.
Installed:
vsftpd-3.0.3-31.el8.x86_64
Complete!
[root@ftp ~]# systemctl enable --now vsftpd
Created symlink /etc/systemd/system/multi-user.target.wants/vsftpd.service → /usr/lib/systemd/system/vsftpd.service.
此时访问需要登陆
终端也可以利用lftp测试,报错530
在终端也可进行登陆ftp,安装 lftp 即可!
[root@ftp ~]# dnf install -y lftp
Updating Subscription Management repositories.
Unable to read consumer identity
安装后在终端输入 ftp 172.25.254.100即可查看ftp界面!也是匿名登陆!
ps:只有ls出来才算登陆ftp界面!
注意:匿名登陆下,不能上传文件!
lftp 用户登陆:可以上传文件+下载文件
实验开始前保证
如果想匿名用户也可以访问ftp界面,需要修改ftp主配置文件,修改完成别忘了重启服务!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
修改完成后再次测试,匿名用户也可登陆ftp!
显示的是默认发布目录,/var/ftp下的!
在终端测试,显示匿名用户也可登陆!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxr-xr-x 2 0 0 6 Feb 17 2020 pub
lftp 172.25.254.100:/> quit
vsftpd默认家目录是/var/ftp,可以在主配置文件修改家目录
首先建立新目录,作为家目录
[root@ftp ~]# ll /var/ftp/
total 0
drwxr-xr-x 2 root root 6 Feb 17 2020 pub
[root@ftp ~]# mkdir /westos
[root@ftp ~]# touch /westos/westosfile{1..10}
在新目录中建立10个文件,如果访问vsftp可以看见10个文件,
说明该目录更改为了vsftp的家目录
在配置文件中添加:anon_root=/westos
[root@ftp ~]# cat /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=YES
anon_root=/westos
# Uncomment this to allow local users to log in.
然后重启服务,访问vsftp,发现10个文件存在!说明家目录已更改!
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile1
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile10
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile2
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile3
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile4
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile5
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile6
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile7
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile8
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile9
lftp 172.25.254.100:/>
注释掉即可还原!
当匿名用户上传文件时,默认不允许上传!
ps:只能在发布目录的子目录上传文件!不能在发布目录上传文件!
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxr-xr-x 2 0 0 6 Feb 17 2020 pub
lftp 172.25.254.100:/> cd /pub/
lftp 172.25.254.100:/pub> put /etc/passwd
put: /etc/passwd: Access failed: 550 Permission denied. (passwd)
lftp 172.25.254.100:/pub>
此时报错550,意思是程序本身不允许上传,修改主配置文件!然后重启服务!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
再次上传文件,报错553,意思是权限控制!
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxr-xr-x 2 0 0 6 Feb 17 2020 pub
lftp 172.25.254.100:/> cd /pub/
lftp 172.25.254.100:/pub> put /etc/passwd
put: /etc/passwd: Access failed: 553 Could not create file. (passwd)
lftp 172.25.254.100:/pub> quit
[root@ftp ~]#
查看默认发布目录的子目录权限,发现只有root可以写。
因此要修改该目录权限
[root@ftp ~]# ll -ld /var/ftp/pub/
drwxr-xr-x 2 root root 6 Feb 17 2020 /var/ftp/pub/
[root@ftp ~]# id ftp
uid=14(ftp) gid=50(ftp) groups=50(ftp)
[root@ftp ~]# chgrp ftp /var/ftp/pub/
[root@ftp ~]# chmod 775 /var/ftp/pub/
[root@ftp ~]# ll -ld /var/ftp/pub/
drwxrwxr-x 2 root ftp 6 Feb 17 2020 /var/ftp/pub/
再次测试匿名上传文件!成功上传!
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 2 0 50 6 Feb 17 2020 pub
lftp 172.25.254.100:/> cd /pub/
lftp 172.25.254.100:/pub> put /etc/passwd
2761 bytes transferred
lftp 172.25.254.100:/pub> quit
默认情况,匿名用户无法建立目录!
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 2 0 50 20 Aug 01 11:22 pub
lftp 172.25.254.100:/> cd /pub/
lftp 172.25.254.100:/pub> mkdir westos
mkdir: Access failed: 550 Permission denied. (westos)
lftp 172.25.254.100:/pub> quit
只需要修改主配置文件,重启服务!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 2 0 50 20 Aug 01 11:22 pub
lftp 172.25.254.100:/> cd /pub/
lftp 172.25.254.100:/pub> mkdir westos
mkdir ok, `westos' created
lftp 172.25.254.100:/pub> ls
-rw------- 1 14 50 2761 Aug 01 11:22 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> quit
就能建立目录!!!
默认情况下,匿名用户无法下载文件
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 3 0 50 34 Aug 01 11:30 pub
lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> ls
-rw------- 1 14 50 2761 Aug 01 11:22 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> get passwd
get: Access failed: 550 Failed to open file. (passwd)
lftp 172.25.254.100:/pub> quit
修改配置文件,添加下载控制即可!ps:该控制配置文件中没有
可以man 5 vsftpd.service 查找
找到后添加该控制!
添加成功,重启服务,即可下载文件成功!
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 3 0 50 34 Aug 01 11:30 pub
lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> ls
-rw------- 1 14 50 2761 Aug 01 11:22 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> get passwd
2761 bytes transferred
lftp 172.25.254.100:/pub> quit
默认情况下,匿名用户无法删除文件
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 3 0 50 34 Aug 01 11:30 pub
lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> ls
-rw------- 1 14 50 2761 Aug 01 11:22 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> rm passwd
rm: Access failed: 550 Permission denied. (passwd)
lftp 172.25.254.100:/pub> quit
同理查找删除控制的服务
添加删除控制在主配置文件!
配置成功重启服务,再次测试,可以删除文件!
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 3 0 50 34 Aug 01 11:30 pub
lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> ls
-rw------- 1 14 50 2761 Aug 01 11:22 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> rm passwd
rm ok, `passwd' removed
lftp 172.25.254.100:/pub> ls
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> quit
匿名用户上传文件时,默认权限是600
如果想更改权限,在配置文件中修改即可!
默认下,匿名用户上传文件的用户是ftp
如果想设定用户身份,可以修改配置文件!
注意:在设定用户身份后,想要修改权限,只能通过 chown_upload_mode=0644 (权限多少就写多少,不是预留权限)设定,不能通过 non_umask=022 设定!
设定完成后重启服务,再次上传文件测试,发现用户身份和权限都已经设定!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 3 0 50 34 Aug 02 05:01 pub
lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> ls
--w----r-- 1 1000 50 2761 Aug 02 05:01 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> rm passwd
rm ok, `passwd' removed
lftp 172.25.254.100:/pub> ls
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> put /etc/passwd
2761 bytes transferred
lftp 172.25.254.100:/pub> ls
-rw-r--r-- 1 1000 50 2761 Aug 02 05:02 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> quit
[root@ftp ~]# id westos
uid=1000(westos) gid=1000(westos) groups=1000(westos),10(wheel)
默认情况下,登陆ftp的终端没有限制
在配置文件中设定登陆数量为2
测试,当登陆第3个ftp时,无法登录。。。。
此时,退出一个,则第3个顺利登陆!
将登陆数量注释,则又可以无限制的登陆!
首先建立一个大文件,为了实验效果,上传该文件,默认情况下上传速度还是挺快的!
[root@ftp ~]# dd if=/dev/zero of=/mnt/bigfile bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 0.222185 s, 2.4 GB/s
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 3 0 50 34 Aug 02 05:02 pub
lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> ls
-rw-r--r-- 1 1000 50 2761 Aug 02 05:02 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> put /mnt/bigfile
524288000 bytes transferred
lftp 172.25.254.100:/pub> quit
[root@ftp ~]#
在配置文件中,设定文件上传速率为100k
再次上传该文件,发现速率很慢!
新建一个本地用户hgx,用户hgx和westos登陆ftp,默认登陆成功!
[root@ftp ~]# useradd hgx
[root@ftp ~]# echo westos | passwd --stdin hgx
Changing password for user hgx.
passwd: all authentication tokens updated successfully.
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:~> quit
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> quit
在配置文件中修改登陆控制,设定不允许本地用户登陆!
保存后重启服务,再次用本地用户登陆,发现无法登录!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
ls: Login failed: 530 This FTP server is anonymous only.
lftp [email protected]:~> quit
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
ls: Login failed: 530 This FTP server is anonymous only.
lftp [email protected]:~> quit
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:~> quit
在配置文件中设定本地用户的默认家目录
再次登陆本地用户,查看家目录,家目录已经变成/westos
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile1
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile10
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile2
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile3
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile4
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile5
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile6
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile7
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile8
-rw-r--r-- 1 0 0 0 Aug 01 10:59 westosfile9
lftp [email protected]:~> quit
默认情况下,本地用户的写权限是开启的,可以上传文件,可以删除文件
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> put /etc/passwd
2798 bytes transferred
lftp [email protected]:~> ls
-rw-r--r-- 1 1001 1001 2798 Aug 02 06:37 passwd
lftp [email protected]:~> rm passwd
rm ok, `passwd' removed
lftp [email protected]:~> quit
在配置文件中设定写权限为NO
测试,本地用户上传文件失败!!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> put /etc/passwd
put: /etc/passwd: Access failed: 550 Permission denied. (passwd)
lftp [email protected]:~> quit
注意:此时匿名用户也无法上传和删除文件!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> put /etc/passwd
put: /etc/passwd: Access failed: 550 Permission denied. (passwd)
lftp [email protected]:~> quit
[root@ftp ~]# lftp 172.25.254.100
lftp 172.25.254.100:~> ls
drwxrwxr-x 3 0 50 49 Aug 02 05:51 pub
lftp 172.25.254.100:/> cd pub/
lftp 172.25.254.100:/pub> ls
-rw-r--r-- 1 1000 50 16043547 Aug 02 05:53 bigfile
-rw-r--r-- 1 1000 50 2761 Aug 02 05:02 passwd
drwx------ 2 14 50 6 Aug 01 11:30 westos
lftp 172.25.254.100:/pub> rm passwd
rm: Access failed: 550 Permission denied. (passwd)
lftp 172.25.254.100:/pub> put /etc/passwd
put: /etc/passwd: Access failed: 550 Permission denied. (passwd)
lftp 172.25.254.100:/pub> quit
本地用户上传文件默认权限是644
在主配置文件中设定上传文件权限为600,则保留权限是077
测试,上传新文件,该文件权限为600
将权限修改回以前,再次上传新文件,发现权限又变回644
删除上传的文件,ps: 不能用rm -fr / rm * 。 只能rm + 名字~!
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
-rw------- 1 1001 1001 1032 Aug 02 06:56 group
-rw-r--r-- 1 1001 1001 490 Aug 02 06:57 inittab
-rw-r--r-- 1 1001 1001 2798 Aug 02 06:53 passwd
lftp [email protected]:~> rm -fr *
lftp [email protected]:~> ls
-rw------- 1 1001 1001 1032 Aug 02 06:56 group
-rw-r--r-- 1 1001 1001 490 Aug 02 06:57 inittab
-rw-r--r-- 1 1001 1001 2798 Aug 02 06:53 passwd
lftp [email protected]:~> rm *
rm: Access failed: 550 Delete operation failed. (*)
lftp [email protected]:~> ls
-rw------- 1 1001 1001 1032 Aug 02 06:56 group
-rw-r--r-- 1 1001 1001 490 Aug 02 06:57 inittab
-rw-r--r-- 1 1001 1001 2798 Aug 02 06:53 passwd
lftp [email protected]:~> rm group passwd inittab
rm ok, 3 files removed
lftp [email protected]:~> ls
lftp [email protected]:~> quit
默认情况下,hgx用户可以登陆,但是把该用户写入永久黑名单 /etc/vsftpd/ftpusers后,无法登录!
ps:黑白名单不用重启服务,可以立即生效!
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> quit
[root@ftp ~]# vim /etc/vsftpd/ftpusers
[root@ftp ~]# 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
hgx
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
ls: Login failed: 530 Login incorrect.
lftp [email protected]:~>
lftp [email protected]:~> quit
同时,westos用户可以正常登陆!
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:~> quit
把用户hgx从永久黑名单中删除后,该用户hgx又可以登陆!
默认情况,该文件也是 /etc/vsftpd/user_list 黑名单,名单内的用户无法登录!
如下图,westos用户无法登录,hgx用户正常登陆,因为westos用户在默认黑名单内!
[root@ftp ~]# 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
westos
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
ls: Login failed: 530 Permission denied.
lftp [email protected]:~> quit
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> quit
进入默认黑名单,发现 在配置文件添加 userlist_deny=NO 默认黑名单变成白名单,只有该名单内的用户可以登陆!
进入配置文件,添加参数,把默认黑名单改成白名单!
修改成功后重启服务,再次测试!发现westos用户可以登陆,反而hgx用户无法登录!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
ls: Login failed: 530 Permission denied.
lftp [email protected]:~> quit
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:~> wuit
Unknown command `wuit'.
lftp [email protected]:~> quit
想恢复默认设置,只需要在配置文件中删除userlist_deny=NO 即可!同时将westos用户从默认黑名单中删除!再次登陆,两个用户都可以登陆!
[root@ftp ~]# vim /etc/vsftpd/user_list
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:~> quit
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> quit
默认情况下,用户登陆可以进入根目录
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> cd /
cd ok, cwd=/
lftp [email protected]:/> ls
lrwxrwxrwx 1 0 0 7 Aug 12 2018 bin -> usr/bin
dr-xr-xr-x 5 0 0 4096 Aug 01 06:21 boot
drwxr-xr-x 21 0 0 3300 Aug 02 03:49 dev
drwxr-xr-x 140 0 0 8192 Aug 02 05:58 etc
drwxr-xr-x 4 0 0 31 Aug 02 05:58 home
lrwxrwxrwx 1 0 0 7 Aug 12 2018 lib -> usr/lib
lrwxrwxrwx 1 0 0 9 Aug 12 2018 lib64 -> usr/lib64
drwxr-xr-x 2 0 0 6 Aug 12 2018 media
drwxr-xr-x 2 0 0 21 Aug 02 05:48 mnt
drwxr-xr-x 2 0 0 6 Aug 12 2018 opt
dr-xr-xr-x 209 0 0 0 Aug 02 03:49 proc
dr-xr-x--- 15 0 0 4096 Aug 02 08:51 root
drwxr-xr-x 40 0 0 1220 Aug 02 03:49 run
lrwxrwxrwx 1 0 0 8 Aug 12 2018 sbin -> usr/sbin
drwxr-xr-x 2 0 0 6 Aug 12 2018 srv
dr-xr-xr-x 13 0 0 0 Aug 02 03:49 sys
drwxrwxrwt 17 0 0 4096 Aug 02 06:59 tmp
drwxr-xr-x 12 0 0 144 Jul 30 06:09 usr
drwxr-xr-x 22 0 0 4096 Jul 30 12:48 var
drwxr-xr-x 2 0 0 197 Aug 01 10:59 westos
比较危险,因此可以设定锁定用户到自己的家目录,则不能随便访问其他目录!
在主配置文件chroot_local_user=YES,同时去除主目录的写权限!
本地用户登陆成功,且被锁定在自己的家目录中!
上节实验,本地用户全部都锁定在自己的家目录中,可以设定白名单!
让westos用户被锁定,hgx用户不被锁定!
首先在配置文件中 设定
由于白名单不存在,所以要新建白名单!将用户hgx写进该白名单!
[root@ftp ~]# cat /etc/vsftpd/chroot_list
hgx
[root@ftp ~]#
重启服务后进行测试,发现hgx用户没有被锁定!westos用户被锁定!
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:/> cd /
lftp [email protected]:/> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:/> quit
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> cd /
cd ok, cwd=/
lftp [email protected]:/> ls
lrwxrwxrwx 1 0 0 7 Aug 12 2018 bin -> usr/bin
dr-xr-xr-x 5 0 0 4096 Aug 01 06:21 boot
drwxr-xr-x 21 0 0 3300 Aug 02 03:49 dev
drwxr-xr-x 140 0 0 8192 Aug 02 05:58 etc
drwxr-xr-x 4 0 0 31 Aug 02 05:58 home
lrwxrwxrwx 1 0 0 7 Aug 12 2018 lib -> usr/lib
lrwxrwxrwx 1 0 0 9 Aug 12 2018 lib64 -> usr/lib64
drwxr-xr-x 2 0 0 6 Aug 12 2018 media
drwxr-xr-x 2 0 0 21 Aug 02 05:48 mnt
drwxr-xr-x 2 0 0 6 Aug 12 2018 opt
dr-xr-xr-x 209 0 0 0 Aug 02 03:49 proc
dr-xr-x--- 15 0 0 4096 Aug 02 09:36 root
drwxr-xr-x 40 0 0 1220 Aug 02 03:49 run
lrwxrwxrwx 1 0 0 8 Aug 12 2018 sbin -> usr/sbin
drwxr-xr-x 2 0 0 6 Aug 12 2018 srv
dr-xr-xr-x 13 0 0 0 Aug 02 03:49 sys
drwxrwxrwt 17 0 0 4096 Aug 02 09:36 tmp
drwxr-xr-x 12 0 0 144 Jul 30 06:09 usr
drwxr-xr-x 22 0 0 4096 Jul 30 12:48 var
drwxr-xr-x 2 0 0 197 Aug 01 10:59 westos
lftp [email protected]:/> quit
设置主配置文件,设定黑名单!
重启服务,黑名单中只有hgx用户,测试hgx和westos用户,发现hgx用户被锁定!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# cat /etc/vsftpd/chroot_list
hgx
[root@ftp ~]# lftp 172.25.254.100 -u hgx
Password:
lftp [email protected]:~> ls
lftp [email protected]:/> cd /
lftp [email protected]:/> ls
lftp [email protected]:/> quit
[root@ftp ~]# lftp 172.25.254.100 -u westos
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2761 Aug 01 10:01 passwd
lftp [email protected]:~> cd /
cd ok, cwd=/
lftp [email protected]:/> ls
lrwxrwxrwx 1 0 0 7 Aug 12 2018 bin -> usr/bin
dr-xr-xr-x 5 0 0 4096 Aug 01 06:21 boot
drwxr-xr-x 21 0 0 3300 Aug 02 03:49 dev
drwxr-xr-x 140 0 0 8192 Aug 02 05:58 etc
drwxr-xr-x 4 0 0 31 Aug 02 05:58 home
lrwxrwxrwx 1 0 0 7 Aug 12 2018 lib -> usr/lib
lrwxrwxrwx 1 0 0 9 Aug 12 2018 lib64 -> usr/lib64
drwxr-xr-x 2 0 0 6 Aug 12 2018 media
drwxr-xr-x 2 0 0 21 Aug 02 05:48 mnt
drwxr-xr-x 2 0 0 6 Aug 12 2018 opt
dr-xr-xr-x 209 0 0 0 Aug 02 03:49 proc
dr-xr-x--- 15 0 0 4096 Aug 02 09:51 root
drwxr-xr-x 40 0 0 1220 Aug 02 03:49 run
lrwxrwxrwx 1 0 0 8 Aug 12 2018 sbin -> usr/sbin
drwxr-xr-x 2 0 0 6 Aug 12 2018 srv
dr-xr-xr-x 13 0 0 0 Aug 02 03:49 sys
drwxrwxrwt 17 0 0 4096 Aug 02 09:46 tmp
drwxr-xr-x 12 0 0 144 Jul 30 06:09 usr
drwxr-xr-x 22 0 0 4096 Jul 30 12:48 var
drwxr-xr-x 2 0 0 197 Aug 01 10:59 westos
lftp [email protected]:/> quit
以上实验用户都是真实存在的,这样服务器的安全性很差,因此可以建立虚拟用户访问ftp!
新建认证文件:/etc/vsftpd/westos :名字任意!
加密认文件
[root@ftp ~]# cat /etc/vsftpd/westos
user1
123
user2
123
[root@ftp ~]# db_load -T -t hash -f /etc/vsftpd/westos /etc/vsftpd/westos.db
[root@ftp ~]# cd /etc/vsftpd
[root@ftp vsftpd]# ls
chroot_list user_list vsftpd_conf_migrate.sh westos.db
ftpusers vsftpd.conf westos
建立认证策略文件
[root@ftp vsftpd]# vi /etc/pam.d/westos
[root@ftp vsftpd]# cat /etc/pam.d/westos
account required pam_userdb.so db=/etc/vsftpd/westos
auth required pam_userdb.so db=/etc/vsftpd/westos
编辑配置文件,开启虚拟功能!指定认证策略文件!
设定完成,重启服务!测试虚拟用户usera user2是否可以登陆ftp
虚拟用户成功登陆ftp,但是家目录与匿名用户的一样!可以建立虚拟用户自己的家目录!
首先建立虚拟用的家目录,名字与用户名一致!
[root@ftp ~]# ls -lR /ftphomedir/
/ftphomedir/:
total 0
drwxr-xr-x 3 root root 34 Aug 2 19:06 user1
drwxr-xr-x 3 root root 34 Aug 2 19:06 user2
/ftphomedir/user1:
total 0
drwxr-xr-x 2 root root 6 Aug 2 19:06 pub
-rw-r--r-- 1 root root 0 Aug 2 19:03 user1file
/ftphomedir/user1/pub:
total 0
/ftphomedir/user2:
total 0
drwxr-xr-x 2 root root 6 Aug 2 19:06 pub
-rw-r--r-- 1 root root 0 Aug 2 19:03 user2file
/ftphomedir/user2/pub:
total 0
在配置文件中设定虚拟用户的家目录!
设定成功后,重启服务,测试,登陆匿名用户!
登陆user1就在user1的家目录,user2就在user2的家目录!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u user1
Password:
lftp [email protected]:~> ls
drwxr-xr-x 2 0 0 6 Aug 02 11:06 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user1file
lftp [email protected]:/> quit
[root@ftp ~]# lftp 172.25.254.100 -u user2
Password:
lftp [email protected]:~> ls
drwxr-xr-x 2 0 0 6 Aug 02 11:06 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user2file
lftp [email protected]:/> quit
虚拟用户userq user2用的都是默认配置文件,则上传,删除文件功能是一致的!
首先修改目录的权限+所有组!!此时,所有的虚拟用户都可以上传文件+删除文件!
[root@ftp ~]# ls -ld /ftphomedir/user{1..2}
drwxr-xr-x 3 root root 34 Aug 2 19:06 /ftphomedir/user1
drwxr-xr-x 3 root root 34 Aug 2 19:06 /ftphomedir/user2
[root@ftp ~]# chmod 775 /ftphomedir/user{1..2}/pub
[root@ftp ~]# chgrp ftp /ftphomedir/user{1..2}/pub
[root@ftp ~]# ls -ld /ftphomedir/user{1..2}/pub
drwxrwxr-x 2 root ftp 6 Aug 2 19:06 /ftphomedir/user1/pub
drwxrwxr-x 2 root ftp 6 Aug 2 19:06 /ftphomedir/user2/pub
[root@ftp ~]# lftp 172.25.254.100 -u user1
Password:
lftp [email protected]:~> ls
drwxrwxr-x 2 0 50 6 Aug 02 11:06 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user1file
lftp [email protected]:/> cd pub/
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> put /etc/passwd
2798 bytes transferred
lftp [email protected]:/pub> ls
-rw-r--r-- 1 1000 50 2798 Aug 02 11:31 passwd
lftp [email protected]:/pub> rm passwd
rm ok, `passwd' removed
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> quit
[root@ftp ~]# lftp 172.25.254.100 -u user2
Password:
lftp [email protected]:~> ls
drwxrwxr-x 2 0 50 6 Aug 02 11:06 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user2file
lftp [email protected]:/> cd pub/
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> put /etc/passwd
2798 bytes transferred
lftp [email protected]:/pub> ls
-rw-r--r-- 1 1000 50 2798 Aug 02 11:31 passwd
lftp [email protected]:/pub> rm passwd
rm ok, `passwd' removed
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> quit
修改默认配置文件,关闭上传+删除文件设定!此时虚拟用户都无法上传+删除文件!
重启服务后,设定生效!
[root@ftp ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftp ~]# systemctl restart vsftpd.service
[root@ftp ~]# lftp 172.25.254.100 -u user1
Password:
lftp [email protected]:~> ls
drwxrwxr-x 2 0 50 6 Aug 02 11:31 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user1file
lftp [email protected]:/> cd pub/
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> put /etc/passwd
put: /etc/passwd: Access failed: 550 Permission denied. (passwd)
lftp [email protected]:/pub> quit
[root@ftp ~]# lftp 172.25.254.100 -u user2
Password:
lftp [email protected]:~> ls
drwxrwxr-x 2 0 50 6 Aug 02 11:31 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user2file
lftp [email protected]:/> cd pub/
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> put /etc/passwd
put: /etc/passwd: Access failed: 550 Permission denied. (passwd)
lftp [email protected]:/pub> quit
如果想要实现user1 可以上传+删除文件,而user2不可以吗?需要建立独立配置文件!
首先在默认配置文件中添加独立目录---名字任意
新建该目录,重启服务!
[root@ftp ~]# mkdir /etc/vsftpd/user_conf
[root@ftp ~]# systemctl restart vsftpd.service
在该目录下新建与用户名同名的文件!并且写入允许上传的配置!
[root@ftp ~]# touch /etc/vsftpd/user_conf/user1
[root@ftp ~]# vim /etc/vsftpd/user_conf/user1
[root@ftp ~]# cat /etc/vsftpd/user_conf/user1
anon_upload_enable=YES
[root@ftp ~]#
测试!!user1可以上传文件,user2不可以!因为user1使用的是独立配置,user2使用的是默认配置,而默认配置不允许上传文件!
[root@ftp ~]# lftp 172.25.254.100 -u user1
Password:
lftp [email protected]:~> ls
drwxrwxr-x 2 0 50 6 Aug 02 11:31 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user1file
lftp [email protected]:/> cd pub/
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> put /etc/passwd
2798 bytes transferred
lftp [email protected]:/pub> ls
-rw-r--r-- 1 1000 50 2798 Aug 02 11:52 passwd
lftp [email protected]:/pub> quit
[root@ftp ~]# lftp 172.25.254.100 -u user2
Password:
lftp [email protected]:~> ls
drwxrwxr-x 2 0 50 6 Aug 02 11:31 pub
-rw-r--r-- 1 0 0 0 Aug 02 11:03 user2file
lftp [email protected]:/> cd pub/
lftp [email protected]:/pub> ls
lftp [email protected]:/pub> put /etc/passwd
put: /etc/passwd: Access failed: 550 Permission denied. (passwd)
lftp [email protected]:/pub> quit