使用FTP命令在shell命令行下从FTP服务器上传/下载文件
Active mode:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@192.168.150.57] # cd /home/admin/ [root@192.168.150.57] # ls ftpupload [root@192.168.150.57] # ls -l total 4.0K -rw-r--r-- 1 root root 7 Mar 29 14:02 ftpupload [root@192.168.150.57] # ftp ftp> open 192.168.150.60 cmds.c: at line 171 Connected to 192.168.150.60. 220-FileZilla Server 0.9.54 beta 220-written by Tim Kosse (tim.kosse@filezilla-project.org) 220 Please visit https://filezilla-project.org/ cmds.c: at line 173 Name (192.168.150.60:root): ftpuser1 331 Password required for ftpuser1 Password: 230 Logged on Remote system type is UNIX. ftp> pwd 257 "/" is current directory. ftp> ls 200 Port command successful 150 Opening data channel for directory listing of "/" drwxr-xr-x 1 ftp ftp 0 Mar 29 13:51 testdire -rw-r--r-- 1 ftp ftp 16 Mar 29 13:50 testfile.txt 226 Successfully transferred "/" ftp> cd testdire 250 CWD successful. "/testdire" is current directory. ftp> pwd 257 "/testdire" is current directory. ftp> ls 200 Port command successful 150 Opening data channel for directory listing of "/testdire" drwxr-xr-x 1 ftp ftp 0 Mar 29 13:50 testdire2 -rw-r--r-- 1 ftp ftp 4544 Mar 29 13:50 testfile2.jnt -rw-r--r-- 1 ftp ftp 0 Mar 29 13:51 testfile3.txt 226 Successfully transferred "/testdire" ftp> get testfile2.jnt local: testfile2.jnt remote: testfile2.jnt 200 Port command successful 150 Opening data channel for file download from server of "/testdire/testfile2.jnt" WARNING! 4 bare linefeeds received in ASCII mode File may not have transferred correctly. 226 Successfully transferred "/testdire/testfile2.jnt" 4544 bytes received in 0.00042 seconds (1e+04 Kbytes/s) ftp> pwd 257 "/testdire" is current directory. ftp> cd / 250 CWD successful. "/" is current directory. ftp> pwd 257 "/" is current directory. ftp> mkdir clientmade 257 "/clientmade" created successfully ftp> ls 200 Port command successful 150 Opening data channel for directory listing of "/" drwxr-xr-x 1 ftp ftp 0 Mar 29 14:48 clientmade drwxr-xr-x 1 ftp ftp 0 Mar 29 13:51 testdire -rw-r--r-- 1 ftp ftp 16 Mar 29 13:50 testfile.txt 226 Successfully transferred "/" ftp> cd clientmade 250 CWD successful. "/clientmade" is current directory. ftp> put ftpupload local: ftpupload remote: ftpupload 200 Port command successful 150 Opening data channel for file upload to server of "/clientmade/ftpupload" 226 Successfully transferred "/clientmade/ftpupload" 8 bytes sent in 1.8e-05 seconds (4.3e+02 Kbytes/s) ftp> ls 200 Port command successful 150 Opening data channel for directory listing of "/clientmade" -rw-r--r-- 1 ftp ftp 8 Mar 29 14:48 ftpupload 226 Successfully transferred "/clientmade" ftp> bye 221 Goodbye
Passive Mode:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@192.168.150.57] # ls -l total 12K -rw-r--r-- 1 root root 7 Mar 29 14:02 ftpupload -rw-r--r-- 1 root root 4.5K Mar 29 14:46 testfile2.jnt [root@192.168.150.57] # ftp ftp> open 192.168.150.60 cmds.c: at line 171 Connected to 192.168.150.60. 220-FileZilla Server 0.9.54 beta 220-written by Tim Kosse (tim.kosse@filezilla-project.org) 220 Please visit https://filezilla-project.org/ Name (192.168.150.60:root): ftpuser1 331 Password required for ftpuser1 Password: 230 Logged on Remote system type is UNIX. ftp> passive Passive mode on. ftp> ls 227 Entering Passive Mode (192,168,150,60,195,136) 150 Opening data channel for directory listing of "/" drwxr-xr-x 1 ftp ftp 0 Mar 29 14:48 clientmade drwxr-xr-x 1 ftp ftp 0 Mar 29 13:51 testdire -rw-r--r-- 1 ftp ftp 16 Mar 29 13:50 testfile.txt 226 Successfully transferred "/" ftp> get testfile.txt local: testfile.txt remote: testfile.txt 227 Entering Passive Mode (192,168,150,60,195,166) 150 Opening data channel for file download from server of "/testfile.txt" 226 Successfully transferred "/testfile.txt" 16 bytes received in 0.00064 seconds (24 Kbytes/s) ftp> cd testdire 250 CWD successful. "/testdire" is current directory. ftp> ls 227 Entering Passive Mode (192,168,150,60,195,139) 150 Opening data channel for directory listing of "/testdire" drwxr-xr-x 1 ftp ftp 0 Mar 29 13:50 testdire2 -rw-r--r-- 1 ftp ftp 4544 Mar 29 13:50 testfile2.jnt -rw-r--r-- 1 ftp ftp 0 Mar 29 13:51 testfile3.txt 226 Successfully transferred "/testdire" ftp> pwd 257 "/testdire" is current directory. ftp> put ftpupload local: ftpupload remote: ftpupload 227 Entering Passive Mode (192,168,150,60,195,99) 150 Opening data channel for file upload to server of "/testdire/ftpupload" 226 Successfully transferred "/testdire/ftpupload" 8 bytes sent in 3.3e-05 seconds (2.4e+02 Kbytes/s) ftp> ls 227 Entering Passive Mode (192,168,150,60,195,147) 150 Opening data channel for directory listing of "/testdire" -rw-r--r-- 1 ftp ftp 8 Mar 29 14:53 ftpupload drwxr-xr-x 1 ftp ftp 0 Mar 29 13:50 testdire2 -rw-r--r-- 1 ftp ftp 4544 Mar 29 13:50 testfile2.jnt -rw-r--r-- 1 ftp ftp 0 Mar 29 13:51 testfile3.txt 226 Successfully transferred "/testdire" ftp> bye 221 Goodbye [root@192.168.150.57] # ls -l total 16K -rw-r--r-- 1 root root 7 Mar 29 14:02 ftpupload -rw-r--r-- 1 root root 16 Mar 29 14:53 testfile.txt -rw-r--r-- 1 root root 4.5K Mar 29 14:46 testfile2.jnt
Active mode VS. Passive Mode
A typical FTP session operates using two channels: a command (or control) channel and a data channel. As their names imply, the command channel is used for transmitting commands as well as replies to those commands, while the data channel is used for transferring data.
Unless you configure your FTP server differently, you will normally set your command channel to use port 21. The port you'll use for the data channel, on the other hand, can differ depending on which data transfer mode you choose. If you choose active mode, then the data channel will normally be port 20. But if you choose passive mode, then the port that will be used will be a random port.
Active mode FTP
Among the two modes, Active mode is the older one. It was the mode introduced in the early days of computing when mainframes were more common and attacks to information security were not as prevalent.
Here's a simplified explanation on how an active mode connection is carried out, summarized in two steps. Some relevant steps (e.g. ACK replies) have been omitted to simplify things.
1. A user connects from a random port on a file transfer client to port 21 on the server. It sends the PORT command, specifying what client-side port the server should connect to. This port will be used later on for the data channel and is different from the port used in this step for the command channel.
2. The server connects from port 20 to the client port designated for the data channel. Once connection is established, file transfers are then made through these client and server ports.
Passive mode FTP
In passive mode, the client still initiates a command channel connection to the server. However, instead of sending the PORT command, it sends the PASV command, which is basically a request for a server port to connect to for data transmission. When the FTP server replies, it indicates what port number it has opened for the ensuing data transfer.
Here's how passive mode works in a nutshell:
1. The client connects from a random port to port 21 on the server and issues the PASV command. The server replies, indicating which (random) port it has opened for data transfer.
2. The client connects from another random port to the random port specified in the server's response. Once connection is established, data transfers are made through these client and server ports.
英文描述摘自: http://www.jscape.com/blog/bid/80512/Active-v-s-Passive-FTP-Simplified