Linux 命令详解:SFTP

4,553 阅读5分钟
原文链接: jiayuanzhang.com

「Linux 命令详解」这个系列的文章将会介绍 Linux/Unix/macOS 系统中使用到的常用命令,包括该命令的简单介绍、常用参数解释以及最佳实践,如果需要快速使用这个命令,直接跳到最佳实践部分即可。

「Linux 命令详解」系列文章:

一、介绍

SFTP(Secure File Transfer Protocol,安全文件传输协议)是一种基于可靠数据流(data stream),提供文件存取和管理的网络传输协议,它在网络协议层的结构如下图1所示:

sftp-layer

与 FTP 协议相比,SFTP 在客户端与服务器间提供了一种更为安全的文件传输方式,如果你还在使用 FTP 来进行文件传输,强烈建议切换到更为安全的 SFTP 上来。

本篇文章将会介绍 SFTP 的链接,以及在交互式命令行中的一些常用命令,并对一些参数进行解释,最后给出实际使用中的最佳实践。目前已经有很多 GUI 客户端支持 SFTP 协议,但是不在本篇文章的讨论范围之内。

二、使用 SFTP 进行连接

因为 SFTP 是基于 SSH 协议的,所以默认的身份认证方法与 SSH 协议保持一致。通常我们使用 SSH Key 来进行连接,如果你已经可以使用 SSH 连接到远程服务器上,那么可以使用以下命令来连接 SFTP:

1
sftp user_name@remote_server_address[:path]

如果远程服务器自定义了连接的端口,可以使用 -P 参数:

1
sftp -P remote_port user_name@remote_server_address[:path]

连接成功后将进入一个 SFTP 的解释器,可以发现命令行提示符变成了 sftp>,使用 exit 命令可以退出连接。

如果连接地址存在 path 并且 path 不是一个目录,那么 SFTP 会直接从服务器端取回这个文件。

三、连接参数详解

  • -B: buffer_size,制定传输 buffer 的大小,更大的 buffer 会消耗更多的内存,默认为 32768 bytes;
  • -P: port,制定连接的端口号;
  • -R: num_requests,制定一次连接的请求数,可以略微提升传输速度,但是会增加内存的使用量。

四、目录管理

在 SFTP 解释器中可以使用 help 命令来查看帮助文档。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
sftp> help
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp grp path                     Change group of file 'path' to 'grp'
chmod mode path                    Change permissions of file 'path' to 'mode'
chown 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 [-afPpRr] remote [local]       Download file
reget [-fPpRr] remote [local]      Resume download file
reput [-fPpRr] [local] remote      Resume upload 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 [-afPpRr] local [remote]       Upload file
pwd                                Display remote working directory
quit                               Quit sftp
rename oldpath newpath             Rename remote 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 解释器中预置了常用的命令,但是没有自带的 Bash 来得丰富。

1)显示当前的工作目录:

1
2
sftp> pwd
Remote working directory: /

2)查看当前目录的内容:

1
2
sftp> ls
Summary.txt     info.html       temp.txt        testDirectory

3)使用 -la 参数可以以列表形式查看,并显示隐藏文件:

1
2
3
4
5
6
7
8
9
sftp> ls -la
drwxr-xr-x    5 demouser   demouser       4096 Aug 13 15:11 .
drwxr-xr-x    3 root       root           4096 Aug 13 15:02 ..
-rw-------    1 demouser   demouser          5 Aug 13 15:04 .bash_history
-rw-r--r--    1 demouser   demouser        220 Aug 13 15:02 .bash_logout
-rw-r--r--    1 demouser   demouser       3486 Aug 13 15:02 .bashrc
drwx------    2 demouser   demouser       4096 Aug 13 15:04 .cache
-rw-r--r--    1 demouser   demouser        675 Aug 13 15:02 .profile
. . .

4)切换目录:

1
sftp> cd testDirectory

5)建立文件夹:

1
sftp> mkdir anotherDirectory

以上的命令都是用来操作远程服务器的,如果想要操作本地目录呢?只需要在每个命令前添加 l 即可,例如显示本地操作目录下的文件:

1
2
sftp> lls
localFiles

使用 ! 可以直接运行 Shell 中的指令:

1
2
3
4
5
6
7
sftp> !df -h
Filesystem      Size   Used  Avail Capacity iused               ifree %iused  Mounted on
/dev/disk1s1   466Gi  360Gi  101Gi    79% 3642919 9223372036851132888    0%   /
devfs          336Ki  336Ki    0Bi   100%    1162                   0  100%   /dev
/dev/disk1s4   466Gi  4.0Gi  101Gi     4%       5 9223372036854775802    0%   /private/var/vm
map -hosts       0Bi    0Bi    0Bi   100%       0                   0  100%   /net
map auto_home    0Bi    0Bi    0Bi   100%       0                   0  100%   /home

五、传输文件

5.1 从远程服务器拉取文件

使用 get 命令可以从远程服务器拉取文件到本地:

1
sftp> get remoteFile [newName]

如果不指定 newName,将使用和远程服务器相同的文件名。

使用 -r 参数可以拉取整个目录:

1
sftp> get -r remoteDirectory

5.2 从本地上传文件到服务器

使用 put 命令可以从本地上传文件到服务器:

1
sftp> put localFile

同样的,可以使用 -r 参数来上传整个目录,但是有一点要注意,如果服务器上不存在这个目录需要首先新建

1
2
sftp> mkdir folderName
sftp> put -r folderName

六、最佳实践

1)连接远程服务器

1
sftp remote_user@remote_host

2)使用端口进行连接

1
sftp -P remote_port remote_user@remote_host

3)从远程服务器拉取文件

1
get /path/remote_file

4)上传本地文件到服务器

1
put local_file

5)查看远程服务器目录内容



ls

6)查看本地目录内容



lls

7)执行本地 Shell 命令



![command]

参考资料


  1. 图片使用 Sketch 绘制,灵感来源于面向信仰编程 [return]