文件服务-FTP
介绍
概述
- FTP协议:文件传输协议(File Transfer Protocol)
- 主要用于互联网中文件的双向传输,文件共享
- 跨平台 :Linux,Windows
- C/S架构
- 基于TCP协议,提供可靠的数据传输
运行模式
-
主动模式:FTP服务端主动连接客户端
-
客户端开启随机**(命令+数据)端口,通过发送数据端口向服务端21端口**发起请求
-
服务端的21端口响应客户端的随机命令端口
-
服务端的20端口主动请求连接客户端的随机数据端口
-
客户端的随机数据端口确认连接
-
-
被动模式:客户端主动连接FTP服务端(默认该模式)
- 客户端开启随机**(命令+数据)端口,通过发送PASV命令向服务端21端口**发起请求
- 服务端的21端口响应客户端的随机命令端口
- 客户端主动连接服务端打开随机数据端口
- 服务端确认连接
客户端工具
- Windows:FlashFXP,FileZilla,IE,Chrome,Firefox
- Linux:ftp,lftp
- lftp:默认是以匿名用户访问,支持批量下载
- ftp:默认是以用户名/密码方式访问
VSFTPD服务介绍
-
服务包:vsftpd(very secure ftp daemon)
-
服务类型:由Systemd启动的守护进程
-
守护进程: /usr/sbin/vsftpd
-
端口: 21(ftp) , 20(ftp-data)
-
启动脚本: /usr/lib/systemd/system/vsftpd.service
-
用户访问控制配置文件:
- /etc/vsftpd/ftpusers 黑名单
- /etc/vsftpd/user_list
-
日志文件: /etc/logrotate.d/vsftpd
-
匿名用户访问目录
- /var/ftp
- /var/ftp/pub
-
主配置文件: /etc/vsftpd/vsftpd.conf
[root@server1 ~]# grep -Ev '^#|^$' /etc/vsftpd/vsftpd.conf anonymous_enable=YES 是否允许匿名用户访问(匿名账号:ftp,anonymous) local_enable=YES 是否允许本地用户登录,默认进入本地用户家目录 write_enable=YES 写总开关 local_umask=022 本地用户上传文件的umask值 dirmessage_enable=YES 是否启用消息功能 xferlog_enable=YES 是否开启xferlog日志 connect_from_port_20=YES 支持主动模式(默认被动模式) xferlog_std_format=YES xferlog日志格式 listen=NO 是否以独立运行的方式监听服务 listen_ipv6=YES 是否支持ipv6 pam_service_name=vsftpd 指定认证文件 userlist_enable=YES 设置用户列表为"允许"(黑名单) tcp_wrappers=YES 支持tcp_wrappers功能(FTP限速操作)
常见修改参数 作用 listen=NO 是否以独立运行的方式监听服务 listen_address=ip地址 设置要监听的IP地址 listen_port=21 设置FTP服务的监听端口 download_enable=YES 是否允许下载文件 userlist_enable=YES 设置用户列表为"允许" userlist_deny=YES 设置用户列表为"禁止" max_clients=0 最大客户端连接数,0为不限制 max_per_ip=0 同一IP地址的最大连接数,0为不限制 anonymous_enable=YES 是否允许匿名用户访问(匿名账号:ftp,anonymous) anon_upload_enable=YES 是否允许匿名用户上传文件 anon_umask 匿名用户上传文件的umask anon_root=/var/ftp 匿名用户的ftp根目录 anon_mkdir_write_enable=YES 是否允许匿名用户创建目录 anon_other_write_enable=YES 是否开放匿名用户的其他写入权限(重命名、删除等) anon_max_rate=0 匿名用户的最大传输速率,0为不限制 local_enable=yes 是否允许本地用户登录 local_umask=022 本地用户上传文件的umask值 local_root=/vat/ftp 本地用户的ftp根目录 chroot_local_user=YES 是否将用户权限禁锢在ftp目录,以确保安全 local_max_rate=0 本地用户的最大传输速率,0为不限制
基础配置
安装
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
setenforce 0
sed -i 's/enforced/disabled/' /etc/selinux/config
getenforce
yum install -y vsftpd
systemctl start vsftpd
systemctl enable vsftpd
基于浏览器访问(只能下载)
ftp://192.168.226.10
基于DOS窗口(可以上传,亦可以下载)
ftp://192.168.226.10
基于软件FlashFXP
基于ftp命令访问(需要输入用户名和密码,登录成功与否会提示)
[root@server2 ~]# yum install -y ftp
[root@server2 ~]# ftp 192.168.226.10
Connected to 192.168.226.10 (192.168.226.10).
220 (vsFTPd 3.0.2)
Name (192.168.226.10:root): ftp
331 Please specify the password.
Password:回车
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
基于lftp命令访问
[root@server2 ~]# yum install -y lftp
[root@server2 ~]# lftp 192.168.226.10
lftp 192.168.226.10:~>
案例:搭建作业提交系统
要求
- 不允许匿名用户访问
- 学生作业统一保存在/data/jobs中
- 学生登录后只能在/data/jobs中操作
安装
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
setenforce 0
sed -i 's/enforced/disabled/' /etc/selinux/config
getenforce
yum install -y vsftpd
systemctl start vsftpd
systemctl enable vsftpd
设置本地账户
[root@server1 ~]# useradd teacher
[root@server1 ~]# echo 985211|passwd --stdin teacher
指定访问目录
[root@server1 ~]# mkdir -p /data/jobs
为账号teacher设置权限(防止不能上传)
[root@server1 ~]# setfacl -R -m u:teacher:rwx /data/jobs
修改配置文件
[root@server1 ~]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO #禁止匿名用户登录
local_root=/data/jobs #默认登录进入该目录
chroot_local_user=YES #禁锢学生只能在/data/jobs目录下
allow_writeable_chroot=YES #允许上传
[root@server1 ~]# systemctl restart vsftpd
访问控制
对象访问控制
黑名单
/etc/vsftpd/ftpusers
[root@server1 ~]# 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
如果使用上述文件中的用户登录,输入密码后会提示
530 Login incorrect.
/etc/vsftpd/user_list
[root@server1 ~]# 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
如何使用上述文件中用户登录,输入用户名后,会提示
530 Permission denied.
Login failed.
-
该黑名单通过修改配置文件/etc/vsftpd/vsftpd.conf 也可变成白名单。但是变成白名单后,ftp服务只允许白名单中的用户登录
userlist_deny=NO
网络访问控制
限制IP
-
FTP必须支持tcp_wrappers(tcp_wrappers=YES)
如果服务支持tcp_wrappers,就可以进行限制IP,限流等操作
查询服务是否支持tcp_wrappers
[root@server1 ~]# ldd /usr/sbin/vsftpd |grep libwrap* libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f7d0ff6d000) #有该模块,说明支持tcp_wrappers
-
/etc/hosts.allow 允许访问的ip地址
-
/etc/hosts.deny 拒绝访问的ip地址(一般只设置这个)
修改/etc/hosts.deny的格式
格式 | 说明 |
---|---|
vsftpd:all | 全部拒绝 |
vsftpd:all EXCEPT 192.168.123.20 | 拒绝所有除了192.168.123.20 |
vsftpd:192.168.123.20 | 拒绝单个IP地址 |
vsftpd:192.168.123.20:allow | 允许192.168.123.20访问 |
vsftpd:192.168.0.0/255.255.255.0 | 拒绝某个网段 |
vsftpd:192.168.0.0/255.255.255.0 EXCEPT 192.168.123.20 | 拒绝某个网段,但是除了某个ip地址 |
[root@server1 ~]# echo 'vsftpd:all' >> /etc/hosts.deny
[root@server1 ~]# systemctl restart vsftpd
[root@server2 ~]# ftp 192.168.226.10
Connected to 192.168.226.10 (192.168.226.10).
421 Service not available.
#提示421
限流
主配置文件
anon_max_rate=0 匿名用户的最大传输速率,0为不限制
local_max_rate=0 本地用户的最大传输速率,0为不限制
#anon_max_rate=1024 修改单位默认为字节
ftp&lftp使用
ftp
[root@server2 ~]# ftp 192.168.226.10
Connected to 192.168.226.10 (192.168.226.10).
220 (vsFTPd 3.0.2)
Name (192.168.226.10:root): teacher#账号
331 Please specify the password.
Password:#密码
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> help ls 查看命令的帮助信息
ls list contents of remote directory
ftp> ls 查看当前目录中的文件
227 Entering Passive Mode (192,168,226,10,21,84).
150 Here comes the directory listing.
-rw-r--r-- 1 1001 1001 132231 May 22 02:34 01.计划任务.pdf
-rw-r--r-- 1 1001 1001 11253 Aug 25 2021 Linux常用命令.md
226 Directory send OK.
ftp> put test.txt 上传
local: test.txt remote: test.txt
227 Entering Passive Mode (192,168,226,10,235,201).
150 Ok to send data.
226 Transfer complete.
7 bytes sent in 4.4e-05 secs (159.09 Kbytes/sec)
ftp> get 01.计划任务.pdf 下载
local: 01.计划任务.pdf remote: 01.计划任务.pdf
227 Entering Passive Mode (192,168,226,10,191,18).
150 Opening BINARY mode data connection for 01.计划任务.pdf (132231 bytes).
226 Transfer complete.
132231 bytes received in 0.000336 secs (393544.64 Kbytes/sec)
ftp> mput file1 file2 file3 批量上传
mput file1? #回车
227 Entering Passive Mode (192,168,226,10,135,83).
150 Ok to send data.
226 Transfer complete.
mput file2? #回车
227 Entering Passive Mode (192,168,226,10,46,39).
150 Ok to send data.
226 Transfer complete.
mput file3? #回车
227 Entering Passive Mode (192,168,226,10,222,76).
150 Ok to send data.
226 Transfer complete.
ftp> prompt 屏蔽提示消息(如果想关闭该功能,在键入prompt即可)
Interactive mode off.
ftp> mput file4 file5
local: file4 remote: file4
227 Entering Passive Mode (192,168,226,10,46,95).
150 Ok to send data.
226 Transfer complete.
local: file5 remote: file5
227 Entering Passive Mode (192,168,226,10,50,93).
150 Ok to send data.
226 Transfer complete.
ftp> mget file1 file2 file3 批量下载
ftp> exit 退出
221 Goodbye.
lftp(适用于批量操作)
[root@server2 ~]# lftp teacher@192.168.226.10
口令:#密码
lftp teacher@192.168.226.10:~> ls
-rw-r--r-- 1 1001 1001 132231 May 22 02:34 01.计划任务.pdf
-rw-r--r-- 1 1001 1001 11253 Aug 25 2021 Linux常用命令.md
-rw-r--r-- 1 1001 1001 0 Aug 21 10:09 file1
-rw-r--r-- 1 1001 1001 0 Aug 21 10:09 file2
-rw-r--r-- 1 1001 1001 0 Aug 21 10:09 file3
-rw-r--r-- 1 1001 1001 0 Aug 21 10:16 file4
-rw-r--r-- 1 1001 1001 0 Aug 21 10:16 file5
lftp teacher@192.168.226.10:/> mirror -R dir1 上传文件夹(批量上传)
Total: 1 directory, 0 files, 0 symlinks
lftp teacher@192.168.226.10:/> mirror ./ 批量下载当前远程目录里的所有文件
Total: 2 directories, 8 files, 0 symlinks
New: 1 file, 0 symlinks
Modified: 7 files, 0 symlinks
143491 bytes transferred
To be removed: 4 directories, 8 files, 0 symlinks
-r–r-- 1 1001 1001 0 Aug 21 10:16 file5
lftp teacher@192.168.226.10:/> mirror -R dir1 上传文件夹(批量上传)
Total: 1 directory, 0 files, 0 symlinks
lftp teacher@192.168.226.10:/> mirror ./ 批量下载当前远程目录里的所有文件
Total: 2 directories, 8 files, 0 symlinks
New: 1 file, 0 symlinks
Modified: 7 files, 0 symlinks
143491 bytes transferred
To be removed: 4 directories, 8 files, 0 symlinks