ftp简介
FTP(File Transfer Protocol)是一个非常古老并且应用十分广泛的文件传输协议
FTP是C/S架构的服务,拥有一个服务器端和一个客户端,FTP底层通过TCP协议来作为传输协议,所以FTP协议是一种可靠的文件传输方式,FTP提供了两个端口号,20和21号端口,20号是数据接口,提供数据之间的传输,21号是命令接口,提供命令之间的传输FTP服务端与客户端连接一般有两种模式:主动模式(Active Mode)和被动模式(Passive Mode)
主动模式原理图
主动模式下,客户端首先会向服务器端的21号端口发出一个连接命令,请求与服务器端建立连接,此时服务器端响应回去给客户端,并要求客户端发送一个用于传送数据的端口,该端口号要 > 1023 ,此时服务器端的20号端口就会与该数据端口主动建立连接,客户端与服务器端进行数据的传送
被动模式的原理如下图所示
与主动模式不同的是,在被动模式下,客户端也是首先与服务器端的21端口建立连接,此时服务器端会开启一个 > 1023 号的数据传送端口,并返回给客户端,这个时候客户端也会开启一个 > 1023 的端口,然后客户端会主动的去跟服务器端的数据传输端口建立连接,两者之间来进行数据的传送输
所以说,主动模式与被动模式的区别就在于究竟是服务器端的20端口主动发起于客户端建立连接,还是服务器端开放一个随机端口,等待客户端与其主动建立连接。在我们的生产环境中,通常还是使用的是被动连接的模式,因为我们的服务器端都有配置防火墙,而防火墙对于内网连接外网的端口一般是放行的,而外网来连接内网的端口则一般是有限制的,所以我们这时如果使用主动模式连接的话,端口可能被防火墙拦截,从而不能提供我们的FTP服务red
1.ftp服务的安装与启用
开启一台虚拟机做为ftp服务器,真机为测试主机
[root@westos_ftp ~]# dnf search ftp 搜索安装包
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.
Last metadata expiration check: 1:59:58 ago on Sat 23 Oct 2021 09:03:55 AM CST.
========================= Name & Summary Matched: ftp ==========================
ftp.x86_64 : The standard UNIX FTP (File Transfer Protocol) client
lftp-scripts.noarch : Scripts for lftp
vsftpd.x86_64 : Very Secure Ftp Daemon ftp服务安装包
tftp.x86_64 : The client for the Trivial File Transfer Protocol (TFTP)
python3-requests-ftp.noarch : FTP transport adapter for python3-requests
tftp-server.x86_64 : The server for the Trivial File Transfer Protocol (TFTP)
syslinux-tftpboot.noarch : SYSLINUX modules in /tftpboot, available for network
: booting
============================== Name Matched: ftp ===============================
lftp.i686 : A sophisticated file transfer program
lftp.x86_64 : A sophisticated file transfer program ftp客户端
============================= Summary Matched: ftp =============================
wget.x86_64 : A utility for retrieving files using the HTTP or FTP protocols
curl.x86_64 : A utility for getting files from remote servers (FTP, HTTP, and
: others)
[root@westos_ftp ~]# dnf install vsftpd.x86_64 lftp.x86_64 -y 安装
[root@westos_ftp ~]# systemctl enable --now vsftpd 启动服务
Created symlink /etc/systemd/system/multi-user.target.wants/vsftpd.service → /usr/lib/systemd/system/vsftpd.service.
[root@westos_ftp ~]# netstat -antlupe | grep vsftpd 查看访问端口
tcp6 0 0 :::21 ::: LISTEN 0 65913 30453/vsftpd 端口为21,传输端口为20
[root@foundation50 Desktop]# lftp 172.25.254.100 访问
lftp 172.25.254.100:~> ls 匿名用户不能访问,没有开启匿名用户访问权限
`ls' at 0 [Sending commands...]
[root@foundation50 Desktop]# lftp 172.25.254.100 -u westos 指定用户访问,可以访问
Password:
lftp westos@172.25.254.100:~> ls
drwxr-xr-x 2 1000 1000 6 Oct 08 07:58 Desktop
drwxr-xr-x 2 1000 1000 6 Aug 28 02:17 Documents
drwxr-xr-x 2 1000 1000 6 Aug 28 02:17 Downloads
drwxr-xr-x 2 1000 1000 6 Aug 28 02:17 Music
drwxr-xr-x 2 1000 1000 6 Aug 28 02:17 Pictures
drwxr-xr-x 2 1000 1000 6 Aug 28 02:17 Public
drwxr-xr-x 2 1000 1000 6 Aug 28 02:17 Templates
drwxr-xr-x 2 1000 1000 6 Aug 28 02:17 Videos
[root@westos_ftp ~]# vim /etc/vsftpd/vsftpd.conf 编辑ftp主配置文件
anonymous_enable=yes 第12行将no改称yes,匿名用户访问就开启了
[root@westos_ftp ~]# systemctl restart vsftpd 改完配置后需要重启
[root@foundation50 Desktop]# lftp 172.25.254.100 匿名用户就可以访问了
lftp 172.25.254.100:~> ls
drwxr-xr-x 2 0 0 6 Feb 17 2020 pub
[root@westos_ftp ftp]# cd /var/ftp/ ftp默认发布目录
[root@westos_ftp ftp]# touch westosfile 发布目录里建立文件
lftp 172.25.254.100:~> ls 登陆就可以访问到
drwxr-xr-x 2 0 0 6 Feb 17 2020 pub
-rw-r--r-- 1 0 0 0 Oct 23 14:13 westosfile
若别人也能访问需要进行火墙设置
[root@westos_ftp ftp]# firewall-cmd --permanent --add-service=ftp 火墙允许ftp服务
success
[root@westos_ftp ftp]# firewall-cmd --reload 刷新火墙
success
[root@westos_ftp ftp]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens3
sources:
services: cockpit dhcpv6-client ftp ssh ftp服务被火墙允许
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
2、ftp匿名用户访问控制
更改默认发布目录
[root@westos_ftp ftp]# mkdir /westosdir
[root