一、FTP简介
FTP(File Transfer Protocol) 早期的三个应用级协议之一,是基于C/S架构。
FTP的主要特性主要有以下几点:
双通道协议:数据通道和命令连接通道,数据通道和命令通道在各自连接时,都会分别建立三次握手。
数据传输格式:二进制(默认)和文本
FTP默认是不加密传输数据的(也可以设置加密),一般用于内网服务
FTP的数据传输有两种模式(从服务器角度来看)
主动(PORT style):服务器主动连接
命令(控制):客户端:随机port —>服务器:tcp21
数据:客户端:随机port+1<—服务器:tcp20
被动(PASV style):客户端主动连接
命令(控制):客户端:随机port —>服务器:tcp21
数据:客户端:随机port+1 —>服务器:随机port
CentOS默认FTP服务器的vsftpd(Very Secure FTP Daemon)高速,稳定。
二、FTP的用户认证
FTP的用户登录可以分为三类用户:匿名用户、系统用户、虚拟用户。在FTP用户登录连接服务器的过程中会用到一些服务及pam模块,如:nsswitch:network service switch名称解析框架、pam:pluggable authentication module 用户认证相关的pam模块,其中pam模块的的存放路径在/lib64/security目录下,用户认证配置文件是/etc/pam.d/vsftpd。接下来详细解析FTP的三类用户。
2.1、匿名用户
匿名用户的名称为ftp,anonymous,其对应Linux用户ftp(实际上是匿名用户映射成了ftp用户),ftp的家目录是/var/ftp,因此匿名用户登录时/var/ftp会被映射成"/",由于登录的用户对ftp服务的“/”不能具有写权限,所以一般会在/var/ftp/目录下建一个pub文件夹,用于读写操作。
[root@centos7 mysql]# getent passwd ftp ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
讯享网
讯享网[root@centos7 ~]# cat /etc/vsftpd/vsftpd.conf | grep "anonymous_enable" anonymous_enable=YES
no_anon_password=YES(默认NO) 匿名用户略过口令检查,需要在配置文件中手动添加这项。更改之后匿名用户就可以直接登录了。
[root@centos7 vsftpd]# ftp 192.168.239.132 Connected to 192.168.239.132 (192.168.239.132). 220 (vsFTPd 3.0.2) Name (192.168.239.132:root): ftp 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> pwd 257 "/"
anon_world_readable_only (默认YES)只能下载全部读的文件
讯享网验证:在服务器上建立两个文件,并把其中一个文件的other 用户权限去掉 [root@centos7 pub]# chmod o-r ftpfile [root@centos7 pub]# ll 总用量 8 -rw-r--r--. 1 root root 10 8月 17 2019 f1 -rw-r----- 1 root root 20 3月 12 19:40 ftpfile
然后客户端使用匿名用户登录,分别下载这两个文件,f1可以下载,ftpfile不能下载 ftp> get ftpfile local: ftpfile remote: ftpfile 227 Entering Passive Mode (192,168,239,132,202,62). 550 Failed to open file. ftp> get f1 local: f1 remote: f1 227 Entering Passive Mode (192,168,239,132,170,62). 150 Opening BINARY mode data connection for f1 (10 bytes). 226 Transfer complete. 10 bytes received in 4.6e-05 secs (217.39 Kbytes/sec)
anon_upload_enable=YES 匿名上传,anon_mkdir_write_enable=YES 允许匿名创建文件,这两项要同时开启才有效,同时也要注文件系统权限,要确保ftp用户对/var/ftp/pub具有写权限(为何不对/var/ftp目录具有写权限???因为登录的用户不能对/
具有写权限,匿名用户登录时会映射成ftp用户,而ftp用户的家目录/var/ftp会映射成/,若想在该目录下操作,可以先在服务器端建一个子文件夹)
讯享网设置ftp用户对/var/ftp/pub具有写权限 setfacl -m u:ftp:rwx /var/ftp/pub/
把注释取消掉即可 [root@centos7 vsftpd]# cat /etc/vsftpd/vsftpd.conf | grep "anon_upload_enable" #anon_upload_enable=YES [root@centos7 vsftpd]# cat /etc/vsftpd/vsftpd.conf | grep "anon_mkdir_write_enable" #anon_mkdir_write_enable=YES
讯享网这时可以上传文件了 ftp> put ftpfile2 local: ftpfile2 remote: ftpfile2 227 Entering Passive Mode (192,168,239,132,99,194). 150 Ok to send data. 226 Transfer complete. 10 bytes sent in 8.6e-05 secs (116.28 Kbytes/sec)
anon_other_write_enable=YES 可删除和修改上传的文件,默认是不允许删除的。
ftp> ls 227 Entering Passive Mode (192,168,239,132,194,120). 150 Here comes the directory listing. -rw-r--r-- 1 0 0 10 Aug 17 2019 f1 -rw-r----- 1 0 0 20 Mar 12 11:40 ftpfile -rw------- 1 14 50 10 Mar 12 13:15 ftpfile2 226 Directory send OK. ftp> delete f1 550 Permission denied.
anon_umask=077 指定匿名上传umask,默认匿名用户的umask是077
讯享网 [root@centos7 pub]# ll 总用量 12 -rw------- 1 ftp ftp 10 3月 12 21:15 ftpfile2
指定上传文件的默认的所有者和权限
chown_uploads=YES(默认NO)
chown_username=cwj
chown_upload_mode=0644
注释去掉并修改 [root@centos7 vsftpd]# cat /etc/vsftpd/vsftpd.conf | grep "chown_" #chown_uploads=YES #chown_username=whoever 上传文件 ftp> put ftpfile3 local: ftpfile3 remote: ftpfile3 227 Entering Passive Mode (192,168,239,132,167,92). 150 Ok to send data. 226 Transfer complete. 10 bytes sent in 0.000115 secs (86.96 Kbytes/sec) 服务端查看文件的属主及权限 [root@centos7 pub]# ll 总用量 16 -rw-r--r-- 1 cwj ftp 10 3月 12 22:45 ftpfile3
2.2、Linux的系统用户
Linux用户也可以登录FTP服务器,其对应于服务器端的系统用户,用户资源存放在/etc/passwd,密码存在/etc/shadow中。系统用户登录后会进入用户的家目录中。
讯享网[root@centos7 data]# ftp 192.168.239.132 Connected to 192.168.239.132 (192.168.239.132). 220 (vsFTPd 3.0.2) Name (192.168.239.132:root): cwj 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> pwd 257 "/home/cwj" 使用cwj用户登录后会进入/home/cwj目录下,注意,此时的根还是操作系统的根,而不是/home/cwj ftp> mkdir ftp 257 "/home/cwj/ftp" created
服务器端可以看到创建的目录 [root@centos7 pub]# ll /home/cwj 总用量 4 drwxr-xr-x 2 cwj cwj 6 3月 14 06:55 ftp
讯享网[root@centos7 data]# ftp 192.168.239.132 Connected to 192.168.239.132 (192.168.239.132). 220 (vsFTPd 3.0.2) Name (192.168.239.132:root): cwj 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> pwd 257 "/" ftp> ls 227 Entering Passive Mode (192,168,239,132,166,114). 150 Here comes the directory listing. drwxrwxr-x 2 0 0 63 Mar 12 14:45 pub
local_enable=YES 是否允许linux用户登录,默认允许
write_enable=YES 允许linux用户上传文件,默认允许
local_umask=022 指定系统用户上传文件的默认权限,默认权限是022
[root@centos7 ~]# cat /etc/vsftpd/vsftpd.conf | grep "write_enable" write_enable=YES anon_mkdir_write_enable=YES [root@centos7 ~]# [root@centos7 ~]# cat /etc/vsftpd/vsftpd.conf | grep "local_enable" local_enable=YES [root@centos7 ~]# cat /etc/vsftpd/vsftpd.conf | grep "local_umask" local_umask=022
local_root=/data/ftptest 非匿名用户登录所在目录
讯享网Name (192.168.239.132:root): cwj 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> pwd 257 "/" ftp> ls 227 Entering Passive Mode (192,168,239,132,91,60). 150 Here comes the directory listing. -rw-r--r-- 1 0 0 20 Mar 14 14:49 ftpfile5 226 Directory send OK. 服务器端的情况 [root@centos7 ftptest]# pwd /data/ftptest [root@centos7 ftptest]# ll 总用量 4 -rw-r--r-- 1 root root 20 3月 14 22:49 ftpfile5
例如:使用cwj用户登录,由于cwj用于对自己的家目录具有写权限,因此会拒绝登录。 [root@centos7 data]# ftp 192.168.239.132 Connected to 192.168.239.132 (192.168.239.132). 220 (vsFTPd 3.0.2) Name (192.168.239.132:root): cwj 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 在去掉/home/cwj的写权限之后,就可以正常登录了 [root@centos7 ftptest]# chmod u-w /home/cwj [root@centos7 ftptest]# ll /home/cwj/ -d dr-x------. 15 cwj cwj 4096 3月 14 06:55 /home/cwj/ 重新登录,成功 [root@centos7 data]# ftp 192.168.239.132 Connected to 192.168.239.132 (192.168.239.132). 220 (vsFTPd 3.0.2) Name (192.168.239.132:root): cwj 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files.
禁锢或不禁锢特定的系统用户在家目录中,与上面设置功能相反
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
当chroot_local_user=YES时,则chroot_list_file中用户不禁锢
当chroot_local_user=NO时,则chroot_list_file中用户禁锢
讯享网cwj在文件中 [root@centos7 ftptest]# vim /etc/vsftpd/chroot_list cwj 添加新用户 [root@centos7 ftptest]# useradd -d /home/ftptest ftptest [root@centos7 ftptest]# echo |passwd --stdin ftptest 更改用户 ftptest 的密码 。 passwd:所有的身份验证令牌已经成功更新。 此时cwj重新登录,反而不禁锢了 [root@centos7 data]# ftp 192.168.239.132 Connected to 192.168.239.132 (192.168.239.132). 220 (vsFTPd 3.0.2) Name (192.168.239.132:root): cwj 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> pwd 257 "/home/cwj" ftp> cd /etc 250 Directory successfully changed. 使用ftptest登录,反而禁锢了(登录之前记得去掉ftptest用户对家目录的写权限) [root@centos7 data]# ftp 192.168.239.132 Connected to 192.168.239.132 (192.168.239.132). 220 (vsFTPd 3.0.2) Name (192.168.239.132:root): ftptest 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> pwd 257 "/"
关于ftp的匿名用户和系统用户先介绍到这里,接下来将介绍ftp虚拟用户。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/63106.html