在Unix/Linux中传输文件的SFTP命令
SFTP代表类似于ftp的安全文件传输程序。
唯一的区别是Unix或者Linux SFTP命令通过加密的SSH传输执行所有操作。
它还可能使用ssh的许多功能,例如公钥身份验证和压缩。
我已经写了另一篇文章,介绍了在Unix和Linux中使用authorized_keys设置无密码登录的SFTP服务器的步骤。
因此,我将使用同一台SFTP服务器来演示单行SFTP命令,以将文件和目录从本地传输到远程,并将远程传输到本地服务器。
服务器
SFTP服务器:主机名:sftp服务器IP地址:192.168.43.83
SFTP客户端:主机名:sftp客户端IP地址:192.168.43.82
1.将文件从远程服务器复制到本地计算机窗口
我们可以使用Windows服务器,使用任何SFTP客户端,例如WinSCP,FileZilla等,来连接到我们的SFTP服务器。
目标的SFTP语法可以指定为[user @] host [:path]或者URI表格
sftp://[user@]host[:port][/path]
我将在Windows笔记本电脑上使用WinSCP连接到Linux SFTP服务器。
提供IP地址和其他用户凭据以使用WinSCP登录到SFTP服务器
连接后,我们现在可以将文件从远程服务器复制到本地计算机窗口,反之亦然。
假设用户" hynman"具有足够的特权,则我们应该能够将文件从远程服务器复制到本地计算机窗口,反之亦然。
2.单行SFTP get命令将文件从远程下载到本地服务器
使用以下SFTP语法,使用SFTP get命令将文件"从远程文件下载到本地服务器"。
sftp [user]@host[:port][/dest_path/file] [/local_path]
我在sftp-server上有一个dest_dir文件夹,在该文件夹上我将创建一个新文件(sftp-server_file),然后我们将使用SFTP get命令将该文件下载到sftp-client上。
[root@sftp-server dest_dir]# pwd /opt/sftp-jails/hynman/dest_dir [root@sftp-server dest_dir]# touch sftp-server_file
使用" sftp-client"连接到" sftp-server",并使用SFTP get命令将" sftp-server_file"下载到" sftp-client"上的"/tmp"。
[root@sftp-client ~]# sftp hynman@sftp-server:dest_dir/sftp-server_file /tmp/ Connected to sftp-server. Fetching /dest_dir/sftp-server_file to /tmp/sftp-server_file
由于我配置了无密码的sftp,因此没有任何SFTP命令行密码提示
3.单行SFTP put命令可将文件从本地上传到远程服务器
使用以下SFTP语法,可以使用SFTP put命令将文件从本地服务器上传到远程服务器。
sftp [user]@host[:port][/dest_path] <<< $'put /local_path/file'
我将在sftp-client
的/tmp
下创建文件sftp-client_file
。
[root@sftp-client ~]# touch /tmp/sftp-client_file
使用单行SFTP put命令将/tmp/sftp-client_file
上传到sftp-server
[root@sftp-client ~]# sftp hynman@sftp-server:dest_dir/<<< $'put /tmp/sftp-client_file' Connected to sftp-server. Changing to: /dest_dir/ sftp> put /tmp/sftp-client_file Uploading /tmp/sftp-client_file to /dest_dir/sftp-client_file /tmp/sftp-client_file
如我们所见,单行SFTP命令行文件上传成功,并且由于我们使用的是SFTP Authorized_keys,因此没有任何SFTP命令行密码提示
4.单行SFTP命令将目录从远程下载到本地服务器
我们将使用与SFTP get命令所使用的SFTP语法相同的语法,并使用添加的SFTP参数将文件从远程下载到本地服务器
sftp -r [user]@host[:port][/dest_path] [/local_path]
我在sftp-server上创建了sftp-server_dir
[root@sftp-server dest_dir]# pwd /opt/sftp-jails/hynman/dest_dir [root@sftp-server dest_dir]# mkdir sftp-server_dir
接下来,我们将使用单行SFTP get命令行来将目录从远程下载到本地服务器到/tmp下。
[root@sftp-client ~]# sftp -r hynman@sftp-server:dest_dir/sftp-server_dir /tmp/ Connected to sftp-server. Fetching /dest_dir/sftp-server_dir/to /tmp/sftp-server_dir Retrieving /dest_dir/sftp-server_dir
如我们所见,单行SFTP命令行从远程目录下载到本地服务器成功,并且由于我们是SFTP Authorized_keys,因此没有任何SFTP命令行密码提示。
使用SFTP命令验证我们刚刚在/tmp
下下载的sftp-client
上的目录:
[root@sftp-client ~]# ls -ld /tmp/sftp-server_dir/ drwx------ 2 root root 4096 Apr 2 17:42 /tmp/sftp-server_dir/
SFTP下载到其中?
在上面的SFTP使用示例中,我们明确提供了下载文件和目录的路径。
默认情况下,执行SFTP的本地目录将是本地路径,默认情况下,如果没有提供任何源目录,SFTP会将文件下载到该本地路径。
SFTP使用示例:
下面的SFTP使用示例将清除我们对" sftp下载到其中"的疑问
[root@sftp-client ~]# sftp hynman@sftp-server Connected to hynman@sftp-server. <-- Connected to sftp-server sftp> cd exchange/<-- Change destination directory on sftp-server to exchange sftp> get file1 <-- SFTP get command to download sftp-server:exchange/file1 to sftp-client Fetching /exchange/file1 to file1 <-- This will get sftp-server:exchange/file1 under /root/on sftp-client as we executed sftp from /root/ sftp> get file1 /tmp/<-- SFTP get command to download exchange/file1 from sftp-server to /tmp/on sftp-client Fetching /exchange/file1 to /tmp/file1 sftp> lcd /home/rahul/<-- SFTP lcd command to change local directory path to /home/rahul on sftp-client sftp> get file1 <-- SFTP get command. This will download sftp-server:exchange/file1 under /home/rahul on sftp-client Fetching /exchange/file1 to file1 sftp> lmkdir test_dir <-- SFTP lmkdir command to create directory on local server sftp> mkdir sftp-server_dir <-- SFTP mkdir command to create directory on sftp-server sftp> ls -l <-- List the files and directories on sftp-server -rw------- 1 hynman 1003 800 Mar 31 17:20 checksum_datababse.out -rw-r--r-- 1 hynman 1003 0 Apr 2 16:10 file1 -rw-r--r-- 1 hynman 1003 0 Apr 2 16:10 file2 -rw-r--r-- 1 hynman 1003 0 Apr 2 16:08 file6 -rw-r--r-- 1 hynman 1003 0 Apr 2 16:08 file7 -rw------- 1 hynman 1003 24 Mar 31 17:20 hello.txt drwx------ 2 hynman 1003 4096 Apr 3 17:27 sftp-server_dir -rw------- 1 hynman 1003 59 Mar 31 17:20 upload_dir.ba sftp> lpwd <-- SFTP lpwd command to print present working directory on local server Local working directory: /home sftp> pwd <-- SFTP pwd command to print present working directory on remote server Remote working directory: /exchange sftp> df -h <-- SFTP df command to check disk usage for shared path on remote server Size Used Avail (root) %Capacity 13.3GB 6.3GB 6.3GB 7.0GB 47% sftp> exit
在此SFTP使用示例中,我们使用了以下命令:
lcd -> To change directory on local seever i.e. SFTP Client lpwd -> List present working directory on local server i.e. SFTP Client lmkdir -> Create directory on local server i.e. SFTP Client cd -> Change directory on remote server i.e. SFTP Server get -> Download file from remote to local server i.e. SFTP Server ls -> List files and directories on remote server i.e. SFTP Server pwd -> Print present working directory on remote server i.e. SFTP Server df -> Print disk usage for destination directory on remote server i.e. SFTP Server
5. SFTP命令将目录从本地上传到远程服务器
我找不到单行SFTP put命令将目录从本地上传到远程服务器,如果我们遇到任何这样的划线机SFTP put命令,请通过注释部分通知我
我使用了一种替代方法,通过使用EOF
标签来组合所有SFTP命令。
重要的提示:
在尝试将目录从本地服务器上传到远程服务器时,计划上传的目录名称很重要,SFTP服务器上还必须存在一个具有相同名称的目录。
例如,我在sftp-client
上有mydir
,在尝试传输之前,我还必须在sftp-server
上创建mydir,然后再尝试从本地服务器上传到远程服务器。
[root@sftp-client ~]# sftp hynman@sftp-server <<EOF > cd dest_dir > mkdir sftp-client_dir > put -r /tmp/sftp-client_dir <-- SFTP put command to upload /tmp/sftp-client_dir to sftp-server > quit > EOF Connected to hynman@sftp-server. sftp> cd dest_dir sftp> mkdir sftp-client_dir sftp> put -r /tmp/sftp-client_dir Uploading /tmp/sftp-client_dir/to /dest_dir/sftp-client_dir Entering /tmp/sftp-client_dir/ sftp> quit
说明:
我们可能会无法规范化:没有这样的文件或者目录;无法规范化路径XXX。
这意味着我们要上载的SFTP服务器上不存在该目录。
使用SFTP CLI,这是在上载目录之前已经具有相同名称的目录的准备工作。
sftp> put -r /tmp/sftp-client_dir sftp-client_dir Uploading /tmp/sftp-client_dir/to /sftp-client_dir Couldn't canonicalize: No such file or directory Unable to canonicalize path "/sftp-client_dir"
6. SFTP命令使用批处理文件传输文件
我们可以使用批处理文件在Unix和Linux中自动进行SFTP文件传输。
我已经在另一篇文章中解释了有关批处理文件的更多信息,因此我将不再赘述。
使用下面的SFTP语法来使用批处理文件并自动进行文件传输
sftp -b <batch_file> [user]@server:[port]
我在下面的SFTP批处理文件中创建了从本地到远程服务器的目录(sftp-client_dir`)。
[root@sftp-client ~]# cat upload_dir.ba cd dest_dir mkdir sftp-client_dir put -r /tmp/sftp-client_dir quit
这个批处理文件将在" sftp-server"上创建" sftp-client_dir",然后将目录" sftp-client_sir"从" sftp-client"上传到SFTP服务器。
[root@sftp-client ~]# sftp -b upload_dir.ba hynman@sftp-server sftp> cd dest_dir sftp> mkdir sftp-client_dir sftp> put -r /tmp/sftp-client_dir Entering /tmp/sftp-client_dir/ sftp> quit
验证sftp-client_dir是否成功上传到sftp-server。
由于我们已配置SFTPauthorized_keys来执行无密码的sftp,因此未收到任何SFTP命令行密码提示
[root@sftp-server dest_dir]# ls -l total 4 drwxr-xr-x 2 hynman hynman 4096 Mar 31 20:14 sftp-client_dir
7.单行SFTP命令删除远程服务器上的目录
使用一个划线SFTP命令行删除" sftp-server"上的目录的SFTP语法与我们用于从本地服务器向远程服务器上载文件的语法相同
使用下面的一个SFTP rmdir命令,我们将在sftp-server上删除sftp-client_dir。
由于该目录位于sftp-server的exchange文件夹下,因此我在连接到sftp-server时提供了此路径。
[root@sftp-client ~]# sftp hynman@sftp-server:exchange/<<< $'rmdir sftp-client_dir' Connected to sftp-server. Changing to: /exchange/ sftp> rmdir sftp-client_dir
我们已成功从" exchange"文件夹中的" sftp-server"中删除了" sftp-client_dir"。
由于我们已配置SFTPauthorized_keys来执行无密码的sftp,因此未收到任何SFTP命令行密码提示
8.使用ssh_config通过SFTP命令传输文件
我们可以使用SSH客户端配置值创建配置文件,SFTP命令将使用该配置文件来传输文件。
使用以下SFTP语法来使用ssh_config
sftp -F <config_file> [user]@server:[port]
我在下面的ssh配置文件中创建了一些值,这些值将由我们的sftp-client
使用。
[root@sftp-client ~]# cat /tmp/ssh_config AddressFamily inet ConnectionAttempts 10 ForwardAgent no ForwardX11 no ForwardX11Trusted no GatewayPorts no HostBasedAuthentication no PubkeyAuthentication yes HostKeyAlias sftp-alias PasswordAuthentication no Compression yes ServerAliveCountMax 3 ServerAliveInterval 15 TCPKeepAlive no User hynman
我们将通过SFTP命令行结合使用ssh_config
文件和-F
参数,如下所示
[root@sftp-client ~]# sftp -F /tmp/ssh_config sftp-server Connected to sftp-server. sftp> exit
我们可以将其与我在上面的SFTP使用示例中使用的批处理文件结合使用,以自动化本地和远程服务器之间的SFTP传输文件。
由于我们已配置SFTPauthorized_keys来执行无密码的sftp,因此未收到任何SFTP命令行密码提示。
9.通过SFTP单个命令使用SSH选项
如果我们不想创建其他SSH配置文件,则还可以通过SFTP命令使用-o参数来传递这些参数。
使用以下SFTP语法:
sftp [-o <ssh_option_1> -o <ssh_option_2> -o <ssh_option_3> ..] [user]@server:[port]
我在下面的SFTP使用示例中使用了一些SSH客户端选项,以对我们的SFTP使用"公钥身份验证"。
[root@sftp-client ~]# sftp -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -o "PreferredAuthentications=publickey" -o "StrictHostKeyChecking=no" -o User=hynman sftp-server Connected to sftp-server. sftp> exit
由于我们已经配置了SFTPauthorized_keys来执行无密码的sftp,因此我们没有任何SFTP命令行密码提示
10.将不同的端口与SFTP命令一起使用来传输文件
默认情况下,SFTP使用与SSH相同的端口,即22. 但是我们也可以通过使用SFTP服务器上/etc/ssh/sshd_config
中的Port = <value>
,将SSH和SFTP配置为使用不同的端口。
在此SFTP使用示例中,我设置了" SFTP以使用端口2200而不是端口22"。
我们可以使用-o Port = <value>`使用自定义SFTP端口进行连接,如下所示
[root@sftp-client ~]# sftp -o Port=2200 -o User=hynman sftp-server Connected to sftp-server. sftp> exit
或者,我们也可以使用SFTP参数-P <value>
来定义SFTP连接的自定义端口。
[root@sftp-client ~]# sftp -P 2200 -o User=hynman sftp-server Connected to sftp-server. sftp> exit