详解SFTP命令

 更新时间:2022年09月29日 14:05:03   作者:哈士奇说骗你是小狗  
SFTP(Secure File Transfer Protocol,安全文件传输协议)是一种基于可靠数据流(data stream),提供文件存取和管理的网络传输协议,这篇文章主要介绍了SFTP命令,需要的朋友可以参考下

SFTP命令

一、介绍

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

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

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

二、使用SFTP进行连接

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

sftp user_name@remote_server_address[:path]

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

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 命令来查看帮助文档。

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)显示当前的工作目录:

sftp> pwd
Remote working directory: /

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

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

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

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)切换目录:

sftp> cd testDirectory

5)建立文件夹:

sftp> mkdir anotherDirectory

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

sftp> lls
localFiles

使用 ! 可以直接(本地)运行 Shell 中的指令:

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 命令可以从远程服务器拉取文件到本地:

sftp> get remoteFile [newName]

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

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

sftp> get -r remoteDirectory

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

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

sftp> put localFile

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

sftp> mkdir folderName
sftp> put -r folderName

六、最佳实践

1)连接远程服务器

sftp remote_user@remote_host

2)使用端口进行连接

sftp -P remote_port remote_user@remote_host

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

get /path/remote_file

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

put local_file

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

ls

6)查看本地目录内容

lls

7)执行本地 Shell 命令

![command]

到此这篇关于SFTP命令的文章就介绍到这了,更多相关SFTP命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SED单行脚本快速参考中文版(Unix 流编辑器)

    SED单行脚本快速参考中文版(Unix 流编辑器)

    这篇文章主要介绍了SED单行脚本快速参考中文版,需要的朋友可以参考下
    2016-02-02
  • Linux 通过 autojump 命令减少 cd 命令的使用的实现方法

    Linux 通过 autojump 命令减少 cd 命令的使用的实现方法

    这篇文章主要介绍了Linux 通过 autojump 命令减少 cd 命令的使用的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Linux Shell中判断进程是否存在的代码

    Linux Shell中判断进程是否存在的代码

    有时候我们需要在linux中判断进程是否存在,然后再执行相应的操作,这里简单的分享下,方便需要的朋友
    2013-01-01
  • Linux中Git集中操作命令汇总

    Linux中Git集中操作命令汇总

    小编给大家汇总了在Linux中Git集中操作命令,值得大家参考一下。
    2017-11-11
  • Linux systemd 定时任务原理解析

    Linux systemd 定时任务原理解析

    说到 Linux 定时任务,大家用得最多的就是 crond 服务,但其实 systemd 也有类似的功能,我们不但可以通过 systemd 来管理服务,还能设置定时任务,那就是 systemd timer,接下来通过本文介绍Linux systemd 定时任务的相关知识,需要的朋友可以参考下
    2024-04-04
  • SHELL脚本监控JAVA进程的代码

    SHELL脚本监控JAVA进程的代码

    这篇文章主要介绍了SHELL脚本监控JAVA进程,需要的朋友可以参考下
    2016-03-03
  • Linux中的Syslog命令

    Linux中的Syslog命令

    syslog是Linux系统默认的日志守护进程,默认的syslog配置文件是/etc/syslog.conf文件。接下来通过本文给大家分享Linux中的Syslog命令,感兴趣的朋友一起看看吧
    2017-09-09
  • Shell脚本中if条件判断的写法实例

    Shell脚本中if条件判断的写法实例

    在写shell脚本的过程中,用到了if else的写法,突然有多个参数需要判断,那么就想到了if else if的用法,这篇文章主要给大家介绍了关于Shell脚本中if条件判断写法的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-01-01
  • 浅谈shell 遍历数组的几种方法

    浅谈shell 遍历数组的几种方法

    这篇文章主要介绍了浅谈shell 遍历数组的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • cpu时钟预取实例代码分享

    cpu时钟预取实例代码分享

    cpu时钟预取实例代码分享,大家参考使用吧
    2013-12-12

最新评论