FTP(File Transfer Protocol)是互联网上最早使用的文件传输协议之一,设计用于在两台计算机之间可靠且有效地传输文件。它建立在TCP/IP协议之上,确保数据传输的稳定性。FTP是一种客户端-服务器架构的服务

FTP有两种工作模式:主动模式和被动模式。


FTP使用TCP(传输控制协议)来建立连接。通常,控制连接使用端口21,而数据连接的端口号会动态分配。
在被动模式下,数据连接端口号由服务器在响应客户端请求时指定。
通常情况下,FTP需要用户进行身份验证,以便访问服务器上的文件系统。身份验证可以使用用户名和密码,或者匿名访问(允许用户以“anonymous”身份登录)。
FTP本身并不加密数据传输,因此在安全性方面较弱。为增加安全性,可以使用FTPS(FTP Secure)或SFTP(SSH文件传输协议),它们提供加密传输的能力。
FTP支持使用命令和响应来进行通信。
FTP客户端发送命令(如上传、下载、列出目录内容等),服务器接收并处理这些命令,然后发送相应的响应。
常见的命令包括ls(列出文件列表)、get(下载文件)、put(上传文件)、delete(删除文件)等。
在Ubuntu上搭建FTP服务器可以通过安装软件来完成,常用的FTP服务器软件包括vsftpd、ProFTPD和Pure-FTPd。如下范例我们采用vsftpd来搭建FTP服务器。
sudo apt-get update
sudo apt-get install vsftpd

sudo vi /etc/vsftpd.conf

在配置文件中,您可以设置多种参数。以下是一些基本的设置:
anonymous_enable=YES:允许匿名访问
local_enable=YES:允许本地用户访问
write_enable=YES:允许写权限(上传文件)
chroot_local_user=YES:限制用户访问到home目录
通常,anonymous_enable= 参数不会设置为YES,FTP目录权限,应该遵循最小权限基本原则,建议在正式环境中,要设置为NO。因为我们是测试环境,没有配置认证用户,所以先设置参数“YES”。
sudo systemctl restart vsftpd


特别提醒,FTP协议本身不加密数据传输,因此建议在生产环境中考虑使用加密协议,如SFTP(通过SSH加密)或FTPS(使用SSL/TLS加密)来提高安全性。特别是发布到公网的FTP服务器。
本机登录ftp服务器,因为配置anonymous_enable=NO,所以这里需要输入账号密码才可以。
ftp localhost

列出当前文件目录文件列表,可以使用dir或者ls

退出可以使用bye或者quit

disconnect 作用是断开ftp连接,但是本地不会退出ftp。这个和quit这些命令有所区别。

help 命令用于获取ftp客户端支持的本地命令列表的帮助信息。

rhelp命令用于获取远程ftp服务器的命令帮助信息。在Windows dos环境下,远程帮助指令是remotehelp

get命令用于从远程服务器下载文件到本地。
get remotefile.txt localfile.txt
如果要指定下载到本地目录:
get remotefile.txt C:\Downloads\localfile.txt
批量下载txt文件,则使用:get *.txt
put命令用于将本地的文件上传到远程FTP服务器。
put localfile.txt remotefile.txt
若是上传到指定的FTP path:
put localfile.txt /path/to/remote/directory/remotefile.txt
在FTP客户端连接到服务器后,输入hash命令即可启用或禁用文件传输时的进度显示。
启用 hash 显示:hash
关闭 hash显示:hash off
hash命令通常对大文件的传输非常有用,它让用户可以清楚地看到文件传输的进度,知道文件是否在持续传输中。
以下是FTP的一些常用命令:
| Command | Information |
|---|---|
| ! | The exclamation point command switches temporarily from FTP to operating system. When using the operating system, typing exit takes you back to the FTP command line. |
| ? | Access the help screen. |
| append | Append text to a local file. |
| ascii | Switch to ASCII (American Standard Code for Information Interchange) transfer mode. |
| bell | Turns bell mode on or off. |
| binary | Switches to binary transfer mode. |
| bye | Exits from FTP. |
| cd | Changes directory. |
| close | Exits from FTP. |
| delete | Deletes a file. |
| debug | Sets debugging on or off. |
| dir | Lists files if connected. |
| dir -C | lists the files in wide format. |
| dir -1 | lists the files in bare format in alphabetic order |
| dir -r | lists directory in reverse alphabetic order. |
| dir -R | lists all files in current directory and subdirectories. |
| dir -S | lists files in bare format in alphabetic order. |
| disconnect | Exits from FTP. |
| get | Grabs file from the connected computer. |
| glob | Sets globbing on or off. When turned off the file name in the put and get commands is taken literally |
| hash | Sets hash mark printing on or off. When turned on, for each 1024 bytes of data received, a hash |
| help | Access the help screen and displays information about command if command typed after help. |
| lcd | Displays local directory if typed alone, or if path typed after lcd, changes local directory. |
| literal | Sends a literal command to the connected computer with an expected one line response. |
| ls | Lists files of the remotely connected computer. |
| mdelete | Multiple delete. |
| mdir | Lists contents of multiple remote directories. |
| mget | Get multiple files. |
| mkdir | Make directory. |
| mls | Lists contents of multiple remote directories. |
| mput | Sent multiple files |
| open | Opens address. |
| prompt | Enables or disables the prompt. |
| put | Send one file. |
| pwd | Print working directory. |
| quit | Exits from FTP. |
| quote | Same as the literal command. |
| recv | Receive file. |
| remotehelp | Get help from remote server. |
| rename | Renames a file. |
| rmdir | Removes a directory on the remote computer. |
| send | Send single file. |
| status | Shows status of currently enabled and disabled options. |
| trace | Toggles packet tracing. |
| Type | Set file transfer type. |
| user | Send new user information. |
| verbose | Sets verbose on or off. |
在dos 或者Ubuntu,又或者不同的FTP软件,命令会有所小小差异,但是整体上差异不大。以上就是部分ftp命令的使用方法。希望对大家有所帮助。

