## 在客户端安装ftp命令,以匿名用户登录FTP,匿名账号为anonymous,密码直接确认 ##
[root@client ~]# yum -y install ftp
## 匿名用户默认开启,若没有开启则将NO改为YES ##
anonymous_enable=YES
## 登录172.16.12.130 ftp服务器,账号为anonymous,密码直接确认 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
## 登录之后 ,ls 可以看到,匿名用户访问的是pub目录 ##
ftp> ls
227 Entering Passive Mode (172,16,12,131,46,39).
150 Here comes the directory listing.
drwxr-xr-x 2 0 0 6 Aug 03 2017 pub
226 Directory send OK.
ftp>
## 进入pub目录,在ftp目录中创建目录,目录没有权限创建 ##
ftp> mkdir aaa
550 Permission denied.
ftp>
## 要想在ftp目录中写,就必须修改配置, ##
[root@client ~]# vim /etc/vsftpd/vstpd.conf
找到这两行取消这注释,或者重新再写
anon_upload_enable=YES #允许匿名用户上传
anon_mkdir_write_enable=YES #允许匿名用户创建目录
添加一行
anon_other_write_enable=YES #允许匿名用户除创建和上传之外的权限,如删除、重命名
:wq #保存退出
## 因为匿名用户登录ftp所用的账户是ftp用户,所以将/var/ftp/pub的属主和属组改为ftp ##
chown ftp.ftp /var/ftp/pub
## 重启vsftpd服务 ##
systemctl restart vsftpd
## 再在/var/ftp/pub目录下创建目录,这时可以写 ##
ftp> mkdir test
257 "/pub/test" created
ftp> ls
227 Entering Passive Mode (172,16,12,131,53,244).
150 Here comes the directory listing.
drwx------ 3 14 50 16 Sep 13 00:03 aaa
drwx------ 2 14 50 6 Sep 13 00:09 test
226 Directory send OK.
ftp>
系统用户
配置系统用户,利用tom用户进行访问
## 还原匿名用户配置,注销添加的配置文件 ##
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
#anon_upload_enable=YES
## 创建tom用户,并设置密码 ##
[root@localhost ~]# useradd tom
[root@localhost ~]# echo 123456 | passwd --stdin tom
Changing password for user tom.
passwd: all authentication tokens updated successfully.
## 修改/etc/vsftpd/vsftpd.conf配置文件,开启如下参数 ##
local_user=YES
write_eanble=YES
local_umask=022
## 利用tom登录FTP服务器,账号为tom,密码为123456 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
## 查看登录到ftp服务器的家目录 ##
ftp> ls
227 Entering Passive Mode (172,16,12,131,211,38).
150 Here comes the directory listing.
drwxr-xr-x 2 1000 1000 6 Sep 13 00:59 123
226 Directory send OK.
ftp> cd ..
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (172,16,12,131,81,233).
150 Here comes the directory listing.
drwx------ 3 1000 1000 69 Sep 13 00:59 tom
226 Directory send OK.
ftp> pwd
257 "/home" #可以看出利用tom账户登录ftp,ftp家目录就是tom用户的家目录
ftp>
## 为了系统安全,将用户锁定在家目录里面,修改配置文件,取消注释或则添加一条 ##
chroot_local_user=YES #禁锢所有FTP本地用户在其家目录中
## 重新启动vsftpd服务后,再次登录,tom用户无法登录 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed. #登录失败
421 Service not available, remote server has closed connection
ftp>
## 修改配置文件,添加一条 ##
allow_writeable_chroot=YES #允许被禁锢
## 重启vsftpd服务,再次登录tom用户 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
331 Please specify the password.
Password:
230 Login successful. #登录成功
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,12,131,165,161).
150 Here comes the directory listing.
drwxr-xr-x 2 1000 1000 6 Sep 13 00:59 123
226 Directory send OK.
## 利用tom用户上传文件,利用windows上传文件(linux上传文件有问题)##
ftp> put C:\Users\dell\Desktop\新建文本文档.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 463 字节,用时 0.07秒 6.61千字节/秒。
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
123
456.txt
新建文本文档.txt #上传的文件
226 Directory send OK.
ftp: 收到 41 字节,用时 0.00秒 41000.00千字节/秒。
ftp>
## 利用tom用户进行下载,利用windows将文件下载到本地(linux上传下载有问题) ##
ftp> get 456.txt
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for 456.txt (463 bytes).
226 Transfer complete.
ftp: 收到 463 字节,用时 0.00秒 463000.00千字节/秒。
ftp>
虚拟用户
配置虚拟用户,创建一个本地用户vuser,两个虚拟用户分别为tom,list。
## 注销如下两行,恢复上面改动的配置,删除用户 ##
allow_writeable_chroot=YES
chroot_local_user=YES
[root@localhost ~]# userdel -r tom
## 配置yum仓库,安装依赖的程序(本地源没有需要安装的依赖程序,利用网络源安装)并修改yum源 ##
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2523 100 2523 0 0 8088 0 --:--:-- --:--:-- --:--:-- 8138
[root@localhost ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo #修改yum源
[root@localhost ~]# yum -y install epel-release #安装依赖关系
## 创建文本格式的用户名、密码列表(创建的列表规则为奇数用户名,偶数为用户密码) ##
[root@localhost ~]# echo 'tom' >> /etc/vsftpd/user.list
[root@localhost ~]# echo '123456' >> /etc/vsftpd/user.list
[root@localhost ~]# echo 'list' >> /etc/vsftpd/user.list
[root@localhost ~]# echo '123456' >> /etc/vsftpd/user.list
[root@localhost ~]# cat /etc/vsftpd/user.list
tom
123456
list
123456
## 安装db4工具(依赖前面的epel_release包),将创建/etc/vsftpd/user.list文件使用db4工具进行转换 ##
[root@localhost ~]# yum -y install db4*
[root@localhost ~]# db_load -T -t hash -f /etc/vsftpd/user.list /etc/vsftpd/user.db
## 为了提高虚拟用户账号的安全性,将文件权限设置为600 ##
[root@localhost ~]# chmod 600 /etc/vsftpd/user.*
##添加虚拟用户的映射账号、创建ftp根目录,并将权限改为755 ##
[root@localhost ~]# useradd -d /var/vftproot -s /sbin/nologin vuser
[root@localhost ~]# ll -d /var/vftproot/
drwx------ 2 vuser vuser 59 Sep 12 23:57 /var/vftproot/
[root@localhost ~]# chmod 755 /var/vftproot/
[root@localhost ~]# ll -d /var/vftproot/
drwxr-xr-x 2 vuser vuser 59 Sep 12 23:57 /var/vftproot/
## 为虚拟用户建立PAM认证,修改/etc/pam.d/vsftpd文件,将文件里面的内容删除或者注销(留下第一行),并添加两行内容(防止出现问题,先备份一份) ##
[root@localhost ~]# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
[root@localhost ~]# vim /etc/pam.d/vsftpd
#%PAM-1.0
auth required pam_userdb.so db=/etc/vsftpd/user
account required pam_userdb.so db=/etc/vsftpd/user
## 修改/etc/vsftpd/vsftpd.conf配置文件,添加虚拟用户支持 ##
guest_enable=YES
guest_username=vuser
## 修改/etc/vsftpd/vsftpd.conf配置文件,建立虚拟用户配置文件 ##
user_config_dir=/etc/vsftpd/vuser_dir
allow_writeable_chroot=YES
## 创建虚拟用户目录(/etc/vsftpd/vuser_dir),为两个虚拟用户创建不同的配置文件(创建两个用户配置文件) ##
[root@localhost ~]# mkdir /etc/vsftpd/vuser_dir
[root@localhost ~]# cd /etc/vsftpd/vuser_dir/
[root@localhost vuser_dir]# touch tom list
## 设置tom用户可上传文件、创建目录;设置list用户只有默认的下载权限(/etc/vsftpd/vuser_dir/list文件为空)
[root@localhost ~]# vim /etc/vsftpd/vuser_dir/tom
anon_upload_enable=YES
anon_mkdir_write_enable=YES
:wq
注:虚拟用户是通过匿名访问的,所以要开启匿名访问功能。
##重启服务 ##
[root@localhost ~]# systemctl restart vsftpd
## 用tom虚拟用户登录ftp服务器,验证是否可以上传文件,创建目录但不能删除目录 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
331 Please specify the password.
Password: #密码为123456
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir test
257 "/test" created
ftp> ls
227 Entering Passive Mode (172,16,12,131,48,140).
150 Here comes the directory listing.
drwx------ 2 1000 1000 6 Sep 13 04:25 test
226 Directory send OK.
ftp> rmdir test
550 Permission denied. #权限拒绝
ftp>
## 利用list用户登录到ftp服务器,验证是否只能查看 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): list
331 Please specify the password.
Password: #密码123456
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,12,131,249,198).
150 Here comes the directory listing.
drwx------ 2 1000 1000 6 Sep 13 04:25 test
226 Directory send OK.
ftp> mkdir list
550 Permission denied. #权限拒绝
ftp>