文件传输协议:File Transfer Protocol 早期的三个应用级协议之一,基于C/S结构
数据传输格式:二进制(默认)和文本
双通道协议:命令和数据连接
两种模式:从服务器角度
主动(PORT style):服务器主动连接
命令(控制):客户端:随机port ---> 服务器:21/tcp
数据:客户端:随机port <---服务器:20/tcp
在服务器端主动连接客户端时,在命令连接阶段就已经协商好客户端的随机端口,服务器端主动去连接的时候使用协商时的随机端口,
被动(PASV style):客户端主动连接
命令(控制):客户端:随机port ---> 服务器:21/tcp
数据:客户端:随机port ---> 服务器:随机port /tcp
在服务器端被动被客户端连接时,在命令连接阶段就协商好服务器端的随机端口,客户端连接的时候使用协商时的随机端口,
#开启vsftpd
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 32 *:21 # 在linux客户端进行连接显示为 passive Mode 被动模式
[root@ftp-client ~]# ftp 10.1.1.41
Connected to 10.1.1.41 (10.1.1.41).
220 (vsFTPd 3.0.3)
Name (10.1.1.41:root): he
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
ftp> ls
227 Entering Passive Mode (10,1,1,41,78,66).
150 Here comes the directory listing.
226 Directory send OK.
#传输数据时服务器端会打开随机端口进行传输
[root@ftp-server ~]# ss -tn
State Local Address:Port Peer Address:Port
ESTAB 10.1.1.41:22 10.1.1.1:49927
ESTAB [::ffff:10.1.1.41]:21 [::ffff:10.1.1.42]:35062
#客户端进行文件下载
ftp> ls
227 Entering Passive Mode (10,1,1,41,45,0).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 1073741824 Jun 15 09:17 f1.img
226 Directory send OK.
ftp> get f1.img
local: f1.img remote: f1.img
227 Entering Passive Mode (10,1,1,41,131,97).
150 Opening BINARY mode data connection for f1.img (1073741824 bytes).
226 Transfer complete.
1073741824 bytes received in 11.8 secs (91000.90 Kbytes/sec)
ftp>
#服务器端查看端口都是随机端口
[root@ftp-server ~]# ss -tn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 52 10.1.1.41:22 10.1.1.1:49927
ESTAB 0 3776080 [::ffff:10.1.1.41]:33633 [::ffff:10.1.1.42]:46213
[root@ftp-server ~]#
#通过该数字进行服务器端用的端口(10,1,1,41,131,97). 131*156+97=33633
[root@ftp-server ~]# echo 131*256+97 | bc
33633
#在Windows上进行连接 PORT command为主动模式
C:\Users\36933>ftp 10.1.1.41
连接到 10.1.1.41。
220 (vsFTPd 3.0.3)
200 Always in UTF8 mode.
用户(10.1.1.41:(none)): he
331 Please specify the password.
密码:
230 Login successful.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
226 Directory send OK.
#服务器端查看端口 服务器使用的是20
[root@ftp-server ~]# ss -tn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 52 10.1.1.41:22 10.1.1.1:49927
ESTAB 0 0 [::ffff:10.1.1.41]:21 [::ffff:10.1.1.1]:58082
ESTAB 0 464280 [::ffff:10.1.1.41]:20 [::ffff:10.1.1.1]:54207
原文:https://www.cnblogs.com/catastrophe/p/14886469.html