详解SFTP命令

SFTP命令

一、介绍

SFTP(Secure File Transfer Protocol,安全文件传输协议)是一种基于可靠数据流(data stream),提供文件存取和管理的网络传输协议。

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

windows下可以使用此命令于Linux进行文件交互。

二、使用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)查看远程服务器目录内容

1

ls

6)查看本地目录内容

1

lls

7)执行本地 Shell 命令

1

![command]

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux中的sftp命令是一种用于在不安全网络上传输文件的工具。在传统的FTP协议中,传输的文件以明文方式传输,容易被攻击者窃取,因此sftp命令采用安全的ssh协议来传输文件,保证数据的安全性。下面就是sftp命令详解: 1.基本语法:sftp [options] [user@]host[/path] 2.参数说明: -a:使用ASCII模式进行传输,支持文本文件和邮件附件等 -b:使用batch文件模式,允许使用sftp命令批量传输文件 -c:使用某种特定的加密算法,包括3des、blowfish等 -f:指定ssh配置文件的位置,通常为/etc/ssh/ssh_config -g:允许使用sftp命令传输文件夹 -i:指定本地私钥的位置,用于身份验证 -p:指定ssh端口号,默认为22 -r:递归传输整个目录 -Q:禁用动态交互功能,只允许使用批处理文件操作 3.sftp命令的使用: 登陆远程主机:sftp user@host 上传本地文件:put local_file remote_dir 下载远程文件:get remote_file local_dir 列出远程目录:ls remote_dir 列出本地目录:ll local_dir 创建远程目录:mkdir remote_dir 删除远程文件:rm remote_file 4.常见的sftp服务器: OpenSSH:最常用的sftp服务器,支持Linux和其他Unix系统 FileZilla:支持Windows和MacOS系统,也支持sftp协议 WinSCP:只支持Windows系统,提供图形化的FTP客户端界面 总之,sftp命令是一种能够保证数据传输安全的工具,可以在不安全的网络环境中安全地传输文件。同时,sftp命令使用简单,容易学习和掌握,值得使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jh035512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值