引言

本地计算机与远程服务器常常需要进行文件传输,如将本地计算机中的文件上传到远程服务器,或从远程服务器下载文件到本地计算机。本文主要介绍包含 FTPSFTPSCP 在内的三种常用的文件传输方式,直接通过命令行操作,无需安装任何软件。

简介

  • FTP(File Transfer Protocol):文件传输协议,基于互联网实现文件传输。1
  • SFTP(SSH File Transfer Protocol):SSH 文件传输协议,也称安全文件传输协议,通过 SSH 建立的安全连接来实现加密文件传输。2
  • SCP(Secure Copy):通过 SSH 建立的安全连接实现本地机与远程机或两台远程机之间的文件传输。3

用法

SFTP 与 FTP 有着几乎一样的语法和功能,故这里只介绍 SFTP 的用法。

SFTP

SFTP 比 FTP 更加安全可靠,因此更推荐在实际应用场景中使用。

SFTP 命令详解

sftp> help
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp [-h] grp path                Change group of file 'path' to 'grp'
chmod [-h] mode path               Change permissions of file 'path' to 'mode'
chown [-h] own path                Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                   filesystem containing 'path'
exit                               Quit sftp
get [-afpR] remote [local]         Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-afpR] local [remote]         Upload file
pwd                                Display remote working directory
quit                               Quit sftp
reget [-fpR] remote [local]        Resume download file
rename oldpath newpath             Rename remote file
reput [-fpR] local [remote]        Resume upload file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help
建立连接

在本地计算机输入以下命令即可连接到远程服务器:

sftp -P 22 user@host

-P:指定 SSH 远程连接的端口号。默认是 22。

执行以上命令后,会要求你输入密码,验证通过后即可进行 SFTP 命令行交互界面。

文件传输
  • 本地计算机上传文件到远程服务器:put filename
  • 远程服务器下载文件到本地计算机:get filename
目录传输
  • 本地计算机上传文件到远程服务器:put -r LocalDirectory RemoteDirectory
  • 远程服务器下载文件到本地计算机:get -r RemoteDirectory LocalDirectory

SCP

SCP 程序的语法类似 Linux 系统中 cp(copy),一般使用默认端口 22,这和 SSH 实现的默认端口一致。

文件传输
  • 本地计算机上传文件到远程服务器:scp SourceFile user@host:directory/TargetFile
  • 远程服务器下载文件到本地计算机:scp user@host:directory/SourceFile TargetFile
目录传输
  • 本地计算机上传文件夹到远程服务器:scp -r SourceFolder user@host:directory/TargetFolder
  • 远程服务器下载文件夹到本地计算机:scp -r user@host:directory/SourceFolder TargetFolder
指定端口

若远程服务器使用非默认端口 22,可通过 -P 参数指定端口号。例如,从远程服务器下载文件到本地计算机:

scp -P 5678 user@host:directory/SourceFile TargetFile
温馨提示

SCP 也支持两台远程服务器之间的文件传输。例如,将远程服务器 A 的文件发送到远程服务器 B:

scp userA@hostA:directory/SourceFile userB@hostB:directory/TargetFile

插画

【画师】藤原 【P站ID】63529251

参考

Linux常用传输工具(上)-FTP和TFTP

Linux常用传输工具(下)-SFTP SCP


  1. FTP-百度百科 ↩︎

  2. SFTP-百度百科 ↩︎

  3. SCP-维基百科 ↩︎

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐