Merge pull request #16617 from wxy/20191020-14-SCP-Command-Examples-to-Securely-Transfer-Files-in-Linux

TSL:20191020 14 SCP Command Examples to Securely Transfer Files in Linux
This commit is contained in:
Xingyu.Wang 2019-12-10 16:52:44 +08:00 committed by GitHub
commit 19646d87c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 243 additions and 241 deletions

View File

@ -1,241 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (14 SCP Command Examples to Securely Transfer Files in Linux)
[#]: via: (https://www.linuxtechi.com/scp-command-examples-in-linux/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
14 SCP Command Examples to Securely Transfer Files in Linux
======
**SCP** (Secure Copy) is command line tool in Linux and Unix like systems which is used to transfer files and directories across the systems securely over the network. When we use scp command to copy files and directories from our local system to remote system then in the backend it makes **ssh connection** to remote system. In other words, we can say scp uses the same **SSH security mechanism** in the backend, it needs either password or keys for authentication.
[![scp-command-examples-linux][1]][2]
In this tutorial we will discuss 14 useful Linux scp command examples.
**Syntax of scp command:**
### scp <options> <files_or_directories> [root@linuxtechi][3]_host:/<folder>
### scp <options> [root@linuxtechi][3]_host:/files   <folder_local_system>
First syntax of scp command demonstrate how to copy files or directories from local system to target host under the specific folder.
Second syntax of scp command demonstrate how files from target host is copied into local system.
Some of the most widely used options in scp command are listed below,
*  -C         Enable Compression
*  -i           identity File or private key
*  -l           limit the bandwidth while copying
*  -P          ssh port number of target host
*  -p          Preserves permissions, modes and access time of files while copying
*  -q          Suppress warning message of SSH
*   -r          Copy files and directories recursively
*   -v          verbose output
Lets jump into the examples now!!!!
###### Example:1) Copy a file from local system to remote system using scp
Lets assume we want to copy jdk rpm package from our local Linux system to remote system (172.20.10.8) using scp command, use the following command,
```
[root@linuxtechi ~]$ scp jdk-linux-x64_bin.rpm root@linuxtechi:/opt
root@linuxtechi's password:
jdk-linux-x64_bin.rpm 100% 10MB 27.1MB/s 00:00
[root@linuxtechi ~]$
```
Above command will copy jdk rpm package file to remote system under /opt folder.
###### Example:2) Copy a file from remote System to local system using scp
Lets suppose we want to copy a file from remote system to our local system under the /tmp folder, execute the following scp command,
```
[root@linuxtechi ~]$ scp root@linuxtechi:/root/Technical-Doc-RHS.odt /tmp
root@linuxtechi's password:
Technical-Doc-RHS.odt 100% 1109KB 31.8MB/s 00:00
[root@linuxtechi ~]$ ls -l /tmp/Technical-Doc-RHS.odt
-rwx------. 1 pkumar pkumar 1135521 Oct 19 11:12 /tmp/Technical-Doc-RHS.odt
[root@linuxtechi ~]$
```
######  Example:3) Verbose Output while transferring files using scp (-v)
In scp command, we can enable the verbose output using -v option, using verbose output we can easily find what exactly is happening in the background. This becomes very useful in **debugging connection**, **authentication** and **configuration problems**.
```
root@linuxtechi ~]$ scp -v jdk-linux-x64_bin.rpm root@linuxtechi:/opt
Executing: program /usr/bin/ssh host 172.20.10.8, user root, command scp -v -t /opt
OpenSSH_7.8p1, OpenSSL 1.1.1 FIPS 11 Sep 2018
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: /etc/ssh/ssh_config.d/05-redhat.conf line 8: Applying options for *
debug1: Connecting to 172.20.10.8 [172.20.10.8] port 22.
debug1: Connection established.
…………
debug1: Next authentication method: password
root@linuxtechi's password:
```
###### Example:4) Transfer multiple files to remote system
Multiple files can be copied / transferred to remote system using scp command in one go, in scp command specify the multiple files separated by space, example is shown below
```
[root@linuxtechi ~]$ scp install.txt index.html jdk-linux-x64_bin.rpm root@linuxtechi:/mnt
root@linuxtechi's password:
install.txt 100% 0 0.0KB/s 00:00
index.html 100% 85KB 7.2MB/s 00:00
jdk-linux-x64_bin.rpm 100% 10MB 25.3MB/s 00:00
[root@linuxtechi ~]$
```
###### Example:5) Transfer files across two remote hosts
Using scp command we can copy files and directories between two remote hosts, lets suppose we have a local Linux system which can connect to two remote Linux systems, so from my local linux system I can use scp command to copy files across these two systems,
Syntax:
### scp [root@linuxtechi][3]_hosts1:/<files_to_transfer>  [root@linuxtechi][3]_host2:/<folder>
Example is shown below,
```
# scp root@linuxtechi:~/backup-Oct.zip root@linuxtechi:/tmp
# ssh root@linuxtechi "ls -l /tmp/backup-Oct.zip"
-rwx------. 1 root root 747438080 Oct 19 12:02 /tmp/backup-Oct.zip
```
###### Example:6) Copy files and directories recursively (-r)
Use -r option in scp command to recursively copy the entire directory from one system to another, example is shown below,
```
[root@linuxtechi ~]$ scp -r Downloads root@linuxtechi:/opt
```
Use below command to verify whether Download folder is copied to remote system or not,
```
[root@linuxtechi ~]$ ssh root@linuxtechi "ls -ld /opt/Downloads"
drwxr-xr-x. 2 root root 75 Oct 19 12:10 /opt/Downloads
[root@linuxtechi ~]$
```
###### Example:7) Increase transfer speed by enabling compression (-C)
In scp command, we can increase the transfer speed by enabling the compression using -C option, it will automatically enable compression at source and decompression at destination host.
```
root@linuxtechi ~]$ scp -r -C Downloads root@linuxtechi:/mnt
```
In the above example we are transferring the Download directory with compression enabled.
###### Example:8) Limit bandwidth while copying ( -l )
Use -l option in scp command to put limit on bandwidth usage while copying. Bandwidth is specified in Kbit/s, example is shown below,
```
[root@linuxtechi ~]$ scp -l 500 jdk-linux-x64_bin.rpm root@linuxtechi:/var
```
###### Example:9) Specify different ssh port while scp ( -P)
There can be some scenario where ssh port is changed on destination host, so while using scp command we can specify the ssh port number using -P option.
```
[root@linuxtechi ~]$ scp -P 2022 jdk-linux-x64_bin.rpm root@linuxtechi:/var
```
In above example, ssh port for remote host is “2022”
###### Example:10) Preserves permissions, modes and access time of files while copying (-p)
Use “-p” option in scp command to preserve permissions, access time and modes while copying from source to destination
```
[root@linuxtechi ~]$ scp -p jdk-linux-x64_bin.rpm root@linuxtechi:/var/tmp
jdk-linux-x64_bin.rpm 100% 10MB 13.5MB/s 00:00
[root@linuxtechi ~]$
```
###### Example:11) Transferring files in quiet mode ( -q) in scp
Use -q option in scp command to suppress transfer progress, warning and diagnostic messages of ssh. Example is shown below,
```
[root@linuxtechi ~]$ scp -q -r Downloads root@linuxtechi:/var/tmp
[root@linuxtechi ~]$
```
###### Example:12) Use Identify file in scp while transferring ( -i )
In most of the Linux environments, keys-based authentication is preferred. In scp command we specify the identify file or private key file using -i option, example is shown below,
```
[root@linuxtechi ~]$ scp -i my_key.pem -r Downloads root@linuxtechi:/root
```
In above example, “my_key.pem” is the identity file or private key file.
###### Example:13) Use different ssh_config file in scp ( -F)
There are some scenarios where you use different networks to connect to Linux systems, may be some network is behind proxy servers, so in that case we must have different **ssh_config** file.
Different ssh_config file in scp command is specified via -F option, example is shown below
```
[root@linuxtechi ~]$ scp -F /home/pkumar/new_ssh_config -r Downloads root@linuxtechi:/root
root@linuxtechi's password:
jdk-linux-x64_bin.rpm 100% 10MB 16.6MB/s 00:00
backup-Oct.zip 100% 713MB 41.9MB/s 00:17
index.html 100% 85KB 6.6MB/s 00:00
[root@linuxtechi ~]$
```
###### Example:14) Use Different Cipher in scp command (-c)
By default, scp uses AES-128 cipher to encrypt the files. If you want to use another cipher in scp command then use -c option followed by cipher name,
Lets suppose we want to use 3des-cbc cipher in scp command while transferring the files, run the following scp command
```
[root@linuxtechi ~]# scp -c 3des-cbc -r Downloads root@linuxtechi:/root
```
Use the below command to list ssh and scp ciphers,
```
[root@linuxtechi ~]# ssh -Q cipher localhost | paste -d , -s -
3des-cbc,aes128-cbc,aes192-cbc,aes256-cbc,root@linuxtechi,aes128-ctr,aes192-ctr,aes256-ctr,root@linuxtechi,root@linuxtechi,root@linuxtechi
[root@linuxtechi ~]#
```
Thats all from this tutorial, to get more details about scp command, kindly refer its man page. Please do share your feedback and comments in comments section below.
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/scp-command-examples-in-linux/
作者:[Pradeep Kumar][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lujun9972
[1]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]: https://www.linuxtechi.com/wp-content/uploads/2019/10/scp-command-examples-linux.jpg
[3]: https://www.linuxtechi.com/cdn-cgi/l/email-protection

View File

@ -0,0 +1,243 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (14 SCP Command Examples to Securely Transfer Files in Linux)
[#]: via: (https://www.linuxtechi.com/scp-command-examples-in-linux/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
在 Linux 上安全传输文件的 14 SCP 命令示例
======
SCPSecure Copy是 Linux 和 Unix 之类的系统中的命令行工具,用于通过网络安全地跨系统传输文件和目录。当我们使用 `scp` 命令将文件和目录从本地系统复制到远程系统时,则在后端与远程系统建立 ssh 连接。换句话说,我们可以说 `scp` 在后端使用了相同的 SSH 安全机制,它需要密码或密钥进行身份验证。
![scp-command-examples-linux][2]
在本教程中,我们将讨论 14 个有用的 Linux `scp` 命令示例。
`scp` 命令语法:
```
# scp <选项> <文件或目录> 用户名@目标主机:/<文件夹>
# scp <选项> 用户名@目标主机:/文件 <本地文件夹>
```
`scp` 命令的第一个语法演示了如何将文件或目录从本地系统复制到特定文件夹下的目标主机。
`scp` 命令的第二种语法演示了如何将目标主机中的文件复制到本地系统中。
下面列出了 `scp` 命令中使用最广泛的一些选项,
* `-C` 启用压缩
* `-i` 指定识别文件或私钥
* `-l` 复制时限制带宽
* `-P` 目标主机的 ssh 端口号
* `-p` 复制时保留文件的权限、模式和访问时间
* `-q` 禁止 SSH 警告消息
* `-r` 递归复制文件和目录
* `-v` 详细输出
现在让我们跳入示例!!!!
### 示例1使用 scp 将文件从本地系统复制到远程系统
假设我们要使用 `scp` 命令将 jdk 的 rpm 软件包从本地 Linux 系统复制到远程系统172.20.10.8),请使用以下命令,
```
[root@linuxtechi ~]$ scp jdk-linux-x64_bin.rpm root@linuxtechi:/opt
root@linuxtechi's password:
jdk-linux-x64_bin.rpm 100% 10MB 27.1MB/s 00:00
[root@linuxtechi ~]$
```
上面的命令会将 jdk 的 rpm 软件包文件复制到 `/opt` 文件夹下的远程系统。
### 示例2使用 scp 将文件从远程系统复制到本地系统
假设我们想将文件从远程系统复制到 `/tmp` 文件夹下的本地系统,执行以下 `scp` 命令,
```
[root@linuxtechi ~]$ scp root@linuxtechi:/root/Technical-Doc-RHS.odt /tmp
root@linuxtechi's password:
Technical-Doc-RHS.odt 100% 1109KB 31.8MB/s 00:00
[root@linuxtechi ~]$ ls -l /tmp/Technical-Doc-RHS.odt
-rwx------. 1 pkumar pkumar 1135521 Oct 19 11:12 /tmp/Technical-Doc-RHS.odt
[root@linuxtechi ~]$
```
### 示例3使用 scp 传输文件时的详细输出(-v
`scp` 命令中,我们可以使用 `-v` 选项启用详细输出,使用详细输出,我们可以轻松地发现后台确切发生了什么。这对于调试连接、认证和配置问题非常有用。
```
root@linuxtechi ~]$ scp -v jdk-linux-x64_bin.rpm root@linuxtechi:/opt
Executing: program /usr/bin/ssh host 172.20.10.8, user root, command scp -v -t /opt
OpenSSH_7.8p1, OpenSSL 1.1.1 FIPS 11 Sep 2018
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: /etc/ssh/ssh_config.d/05-redhat.conf line 8: Applying options for *
debug1: Connecting to 172.20.10.8 [172.20.10.8] port 22.
debug1: Connection established.
…………
debug1: Next authentication method: password
root@linuxtechi's password:
```
### 示例4将多个文件传输到远程系统
可以使用 `scp` 命令一次性将多个文件复制/传输到远程系统,在 `scp` 命令中指定多个文件,并用空格隔开,示例如下所示
```
[root@linuxtechi ~]$ scp install.txt index.html jdk-linux-x64_bin.rpm root@linuxtechi:/mnt
root@linuxtechi's password:
install.txt 100% 0 0.0KB/s 00:00
index.html 100% 85KB 7.2MB/s 00:00
jdk-linux-x64_bin.rpm 100% 10MB 25.3MB/s 00:00
[root@linuxtechi ~]$
```
### 示例5在两个远程主机之间传输文件
使用 `scp` 命令,我们可以在两个远程主机之间复制文件和目录,假设我们有一个可以连接到两个远程 Linux 系统的本地 Linux 系统,因此从我的本地 Linux 系统中,我可以使用 `scp` 命令在这两个系统之间复制文件,
命令语法:
```
# scp 用户名@远程主机1:/<要传输的文件> 用户名@远程主机2:/<文件夹>
```
示例如下:
```
# scp root@linuxtechi:~/backup-Oct.zip root@linuxtechi:/tmp
# ssh root@linuxtechi "ls -l /tmp/backup-Oct.zip"
-rwx------. 1 root root 747438080 Oct 19 12:02 /tmp/backup-Oct.zip
```
### 示例6递归复制文件和目录-r
`scp` 命令中使用 `-r` 选项将整个目录从一个系统递归复制到另一个系统,示例如下所示:
```
[root@linuxtechi ~]$ scp -r Downloads root@linuxtechi:/opt
```
使用以下命令验证 `Downloads` 文件夹是否已复制到远程系统,
```
[root@linuxtechi ~]$ ssh root@linuxtechi "ls -ld /opt/Downloads"
drwxr-xr-x. 2 root root 75 Oct 19 12:10 /opt/Downloads
[root@linuxtechi ~]$
```
### 示例7通过启用压缩来提高传输速度-C
`scp` 命令中,我们可以通过使用 `-C` 选项启用压缩来提高传输速度,它将自动在源上启用压缩并在目标主机上启用解压缩。
```
root@linuxtechi ~]$ scp -r -C Downloads root@linuxtechi:/mnt
```
在以上示例中,我们正在启用压缩的情况下传输下载目录。
### 示例8复制时限制带宽-l
`scp` 命令中使用 `-l` 选项设置复制时对带宽使用的限制。带宽以 Kbit/s 为单位指定,示例如下所示,
```
[root@linuxtechi ~]$ scp -l 500 jdk-linux-x64_bin.rpm root@linuxtechi:/var
```
### 示例9在 scp 时指定其他 ssh 端口(-P
在某些情况下,目标主机上的 ssh 端口会更改,因此在使用 `scp` 命令时,我们可以使用 `-P` 选项指定 ssh 端口号。
```
[root@linuxtechi ~]$ scp -P 2022 jdk-linux-x64_bin.rpm root@linuxtechi:/var
```
在上面的示例中,远程主机的 ssh 端口为 “2022”
### 示例10复制时保留文件的权限、模式和访问时间-p
从源复制到目标时,在 `scp` 命令中使用 `-p` 选项保留权限、访问时间和模式。
```
[root@linuxtechi ~]$ scp -p jdk-linux-x64_bin.rpm root@linuxtechi:/var/tmp
jdk-linux-x64_bin.rpm 100% 10MB 13.5MB/s 00:00
[root@linuxtechi ~]$
```
### 示例11在 scp 中以安静模式传输文件(-q
`scp` 命令中使用 `-q` 选项可禁止显示 ssh 的传输进度、警告和诊断消息。示例如下所示:
```
[root@linuxtechi ~]$ scp -q -r Downloads root@linuxtechi:/var/tmp
[root@linuxtechi ~]$
```
### 示例12在传输时使用 scp 中的识别文件(-i
在大多数 Linux 环境中,首选基于密钥的身份验证。在 scp 命令中,我们使用 `-i` 选项指定识别文件(私钥文件),示例如下所示:
```
[root@linuxtechi ~]$ scp -i my_key.pem -r Downloads root@linuxtechi:/root
```
在上面的示例中,`my_key.pem` 是识别文件或私钥文件。
### 示例13在 scp 中使用其他 ssh_config 文件(-F
在某些情况下,你使用不同的网络连接到 Linux 系统,可能某些网络位于代理服务器后面,因此在这种情况下,我们必须具有不同的 `ssh_config` 文件。
通过 `-F` 选项在 `scp` 命令中指定了不同的 `ssh_config` 文件,示例如下所示:
```
[root@linuxtechi ~]$ scp -F /home/pkumar/new_ssh_config -r Downloads root@linuxtechi:/root
root@linuxtechi's password:
jdk-linux-x64_bin.rpm 100% 10MB 16.6MB/s 00:00
backup-Oct.zip 100% 713MB 41.9MB/s 00:17
index.html 100% 85KB 6.6MB/s 00:00
[root@linuxtechi ~]$
```
### 示例14在 scp 命令中使用其他加密方式(-c
默认情况下,`scp` 使用 AES-128 加密方式来加密文件。如果你想在 `scp` 命令中使用其他加密方式,请使用 `-c` 选项,后接加密方式名称,
假设我们要在用 `scp`命令传输文件时使用 3des-cbc 加密方式,请运行以下 `scp` 命令:
```
[root@linuxtechi ~]# scp -c 3des-cbc -r Downloads root@linuxtechi:/root
```
使用以下命令列出 `ssh``scp` 加密方式:
```
[root@linuxtechi ~]# ssh -Q cipher localhost | paste -d , -s -
3des-cbc,aes128-cbc,aes192-cbc,aes256-cbc,root@linuxtechi,aes128-ctr,aes192-ctr,aes256-ctr,root@linuxtechi,root@linuxtechi,root@linuxtechi
[root@linuxtechi ~]#
```
以上就是本教程的全部内容,要获取有关 `scp` 命令的更多详细信息,请参考其手册页。请在下面的评论部分中分享你的反馈和评论。
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/scp-command-examples-in-linux/
作者:[Pradeep Kumar][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lujun9972
[1]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]: https://www.linuxtechi.com/wp-content/uploads/2019/10/scp-command-examples-linux.jpg
[3]: https://www.linuxtechi.com/cdn-cgi/l/email-protection