linux scp命令用法_Linux SCP命令用法示例

linux scp命令用法

linux scp命令用法

Scp is an acronym for Secure Copy. It is used primarily for copying files over an ssh connection. The copied files are encrypted and assumed secure as ssh. If the files contain sensitive data copying them with SCP or similar encrypted protocol is the best solution. In this tutorial, we will look at how to use SCP in a detailed manner.

Scp是安全复制的缩写。 它主要用于通过ssh连接复制文件。 复制的文件已加密,并假定安全为ssh。 如果文件包含敏感数据,则最好使用SCP或类似的加密协议进行复制。 在本教程中,我们将详细研究如何使用SCP。

使用ssh软件包安装scp命令 (Install scp Command with ssh Package)

Scp is provided by ssh packages in Linux distributions. So to install scp we will install ssh but ssh is generally installed by default.

Scp由Linux发行版中的ssh软件包提供。 因此,要安装scp我们将安装ssh,但默认情况下通常会安装ssh。

Ubuntu,Debian,Mint,Kali (Ubuntu, Debian, Mint, Kali)

scp is provided with the ssh package. We will install the ssh package with the following apt install command.

ssh软件包提供了scp 。 我们将使用以下apt install命令安装ssh软件包。

$ sudo apt install ssh -y
Ubuntu, Debian, Mint, Kali
Ubuntu, Debian, Mint, Kali
Ubuntu,Debian,Mint,Kali

Fedora,CentOS,RHEL (Fedora,CentOS, RHEL)

We will use dnf or yum package manager.

我们将使用dnfyum软件包管理器。

$ sudo dnf install ssh -y

OR

要么

$ sudo yum install ssh -y

Scp命令语法 (Scp Command Syntax)

To get a simple and fast help run SCP without any parameter like below. As we can see we use scp command and provide options related to the connection. Then we will provide a username and remote hostname with @ delimiter.

要获得简单快速的帮助,请运行不带以下任何参数的SCP。 如我们所见,我们使用scp命令并提供与连接有关的选项。 然后,我们将使用@分隔符提供用户名和远程主机名。

scp OPTIONS[email protected]_HOST:/FILES_DIRECTORIES [email protected]_HOST:/FILES_DIRECTORIES
  • OPTIONS will define the behavior of the scp command like cipher type, recursive, speed, ssh port, and limit.

    选项将定义scp命令的行为,例如密码类型,递归,速度,ssh端口和限制。

  • USERNAME is the user of the remote system

    USERNAME是远程系统的用户
  • SOURCE_HOST is the host we want to copy files and folders which will be the source.

    SOURCE_HOST是我们要复制将作为源的文件和文件夹的主机。
  • DESTINATION_HOST is the destination host where source files and directories will be copied.

    DESTINATION_HOST是将在其中复制源文件和目录的目标主机。
  • FILES_DIRECTORIES are copied files and directories remote system paths and names.

    FILES_DIRECTORIES是复制的文件和目录,以及远程系统的路径和名称。

Scp命令帮助信息 (Scp Command Help Information)

To get more detailed help man scp command can be used like below. Scp Unix usage is not different from Linux Scp usage.

要获得更详细的帮助,可以像下面这样使用man scp命令。 Scp Unix使用与Linux Scp使用没有什么不同。

$ man scp
Scp Command Help Information
Scp Command Help Information
Scp命令帮助信息

使用Scp将远程文件复制到本地系统(Copy Remote Files To Local System With Scp)

We will copy files with scp command like below. While copying files scp will ask for credentials.

我们将使用scp命令复制文件,如下所示。 在复制文件时,scp将要求提供凭据。

$ scp[email protected]:/home/ismail/Downloads/backup.tar.gz /home/ali

We have copied backup.tar.gz by providing ismail user from local system to the /home/ali. It is not asked for a password because I have already copied the required credentials.

我们通过将ismail用户从本地系统提供到/home/ali复制了backup.tar.gz。 不需要输入密码,因为我已经复制了所需的凭据。

LEARN MORE  Expect Scripting Tutorial With Examples
了解更多信息期望脚本教程与示例

使用证书身份验证运行不带密码的Scp命令(Run Scp Command without Password with Certificate Authentication)

If we want to use scp in a passwordless and more secure manner, we need to setup SSH Key-based authentication. In order to set up key-based authentication, we need to have keys that can be created with the ssh-keygen command like below.

如果要以无密码且更安全的方式使用scp ,则需要设置基于SSH密钥的身份验证。 为了设置基于密钥的身份验证,我们需要具有可以使用ssh-keygen命令创建的密钥,如下所示。

$ ssh-keygen
Passwordless scp Command
Passwordless scp Command
无密码的scp命令

After creating a key pair we will deploy the public key with the following command to the remote system by providing the user. It will ask a password for the user. After deployment, we can use ssh without a password.

创建密钥对之后,我们将通过提供用户,将带有以下命令的公钥部署到远程系统。 它将询问用户密码。 部署后,我们可以使用不带密码的ssh。

$ ssh-copy-id[email protected]

使用Scp将本地文件复制到远程 (Copy Local File To The Remote With Scp)

We will copy the local file to the remote. As you will see there is no difference from copying from remote to local.

我们将本地文件复制到远程文件。 如您所见,从远程复制到本地没有什么区别。

$ scp backup.tar.gz[email protected]:/home/ismail/Downloads
  • backup.tar.gz is the local file name we want to copy to the remote.

    backup.tar.gz是我们要复制到远程的本地文件名。

  • ismail is the username we will authenticate on the remote server.

    ismail是我们将在远程服务器上进行身份验证的用户名。

  • 192.168.122.233 is the IP address of the remote system.

    192.168.122.233是远程系统的IP地址。

  • /home/ismail/Downlaods is the remote system path we want to copy.

    /home/ismail/Downlaods是我们要复制的远程系统路径。

使用Scp将远程文件复制到本地 (Copy Remote File To The Local With Scp)

Alternatively, we can copy remote files and directories to the local system. We will specify the remote system as a source host. While specifying source host we may need to provide a user name explicitly. Then we will specify the destination which will be our local system path. In this example, we will copy the file named test.tar.gz file into the local system path /home/ismail. We will use ali as a remote system user name.

或者,我们可以将远程文件和目录复制到本地系统。 我们将远程系统指定为源主机。 在指定源主机时,我们可能需要显式提供用户名。 然后,我们将指定目标位置,这将是我们的本地系统路径。 在此示例中,我们将名为test.tar.gz文件复制到本地系统路径/home/ismail 。 我们将使用ali作为远程系统用户名。

$ scp[email protected]:/mnt/test.tar.gz /home/ismail/

使用Scp递归复制文件夹 (Copy Folders Recursively With Scp)

Up to now, we have copied single files from remote. To copy folders with its sub-folders can be done with a recursive parameter. Recursive parameter makes to copy entire directories recursively. We will use the -r option.

到目前为止,我们已经从远程复制了单个文件。 要复制带有子文件夹的文件夹,可以使用递归参数。 递归参数使您可以递归复制整个目录。 我们将使用-r选项。

$ scp -r Downloads 192.168.122.233:/home/ismail/Documents

将多个本地文件复制到远程系统 (Copy Multiple Local Files To The Remote System)

We can copy multiple local files to the remote system with SCP command. We will just provide the names of the local files for the source and in the last, we will provide the destination or remote system we want to copy. In this example, we will copy local files named file1.txt, file2.txt and file3.tar.gz to the remote system.

我们可以使用SCP命令将多个本地文件复制到远程系统。 我们将仅提供源的本地文件名,最后,我们将提供要复制的目标或远程系统。 在此示例中,我们将名为file1.txtfile2.txtfile3.tar.gz本地文件复制到远程系统。

$ scp file1.txt file2.txt file3.tar.gz[email protected]:/home/ismail/

复制特定文件类型或扩展名的所有文件 (Copy All Files For Specific File Type or Extension)

In some cases, we may need to copy only specific file types or extensions. We can use the bash glob operator in order to generalize the file name and set the extension. In this example, we will copy all text files with the *.txt extension into the remote system.

在某些情况下,我们可能只需要复制特定的文件类型或扩展名。 我们可以使用bash glob运算符来概括文件名并设置扩展名。 在此示例中,我们将所有带有*.txt扩展名的文本文件复制到远程系统中。

$ scp /home/ismail/*.txt[email protected]:/home/ismail

使用Scp复制时压缩文件 (Compress Files While Copying With Scp)

Scp encrypts files while copying and this makes some performance loss for the transmission. We can eliminate this performance loss by compressing files. Keep in mind that files are compressed for transfer not entirely. We will use -C option for compression.

Scp在复制时会加密文件,这会导致传输性能下降。 我们可以通过压缩文件来消除这种性能损失。 请记住,文件不完全压缩以进行传输。 我们将使用-C选项进行压缩。

$ scp -C original-ks.cfg[email protected]:/root/original-ks.cfg

By providing -C parameter we have enabled compression for the transfer.

通过提供-C参数,我们启用了传输压缩。

对Scp使用不同的SSH端口 (Use Different SSH Port For Scp)

Ssh runs by default tcp/22 but sometimes it may be different from that. Scp assumes the port of ssh 22 too. We can change port like below. We will provide the port name with the -P option and the port number.

SSH默认运行tcp / 22,但有时可能与此不同。 Scp也假定为ssh 22的端口。 我们可以像下面那样更改端口。 我们将为端口名提供-P选项和端口号。

$ scp -P 2222 original-ks.cfg[email protected]:/root/original-ks.cfg

We have provided port number with -P parameter and the value </b>2222.

我们提供了带有-P参数和值</b>2222端口号

设置Scp文件传输的带宽限制 (Set Bandwidth Limit For Scp File Transfer)

If our network capacity or bandwidth is limited or there is some heavy load which is important copying files with Scp will create extra load. We may need to limit the bandwidth usage of the Scp command. We can use the -l option and provide the bandwidth we want to set for Scp command. In this example, we will set the bandwidth usage to the 100 kbps.

如果我们的网络容量或带宽有限,或者负载过重,那么使用Scp复制文件将产生额外的负载,这一点很重要。 我们可能需要限制Scp命令的带宽使用。 我们可以使用-l选项,并提供我们要为Scp命令设置的带宽。 在此示例中,我们将带宽使用率设置为100 kbps。

$ scp -l 100 Backup.tar.gz  192.168.122.233:/home/ismail/Documents

Scp的详细或调试模式 (Verbose or Debug Mode For Scp)

If there is a problem with the Scp file transfer or connection we have to troubleshoot the problem. So we need more information about the Scp where verbose or debug mode can be enabled with the -v.

如果Scp文件传输或连接有问题,我们必须解决该问题。 因此,我们需要有关Scp的更多信息,其中可以使用-v启用详细或调试模式。

$ scp -r -v kafka_2.12-2.2.0[email protected]:/home/ismail/backup/
Verbose and Debug Mode For Scp
Verbose and Debug Mode For Scp
Scp的详细和调试模式

将密码传递给Scp命令(Passing Password To Scp Command)

We have already set up passwordless key-based authentication but we may need to use SCP with the password. In this case, we can provide a password with a simple app named sshpass to the SCP like below.

我们已经设置了无密码的基于密钥的身份验证,但是我们可能需要将SCP与密码一起使用。 在这种情况下,我们可以使用一个名为sshpass的简单应用程序向SCP提供密码,如下所示。

$ dnf install sshpass

We have installed it. Because it is separate from ssh tools.

我们已经安装好了。 因为它与ssh工具分开。

$ sshpass -p "123456" scp original-ks.cfg[email protected]:/root/original-ks.cfg

Here -p for the password and “123456” is our password. Note that our password is not strong and it is easily hackable. Then we provide regular scp command.

在此, -p为密码, “ 123456”为我们的密码。 请注意,我们的密码不强,很容易被黑客入侵。 然后我们提供常规的scp命令。

LEARN MORE  TCP and UDP Port List, Names and Usage Frequency
了解更多TCP和UDP端口列表,名称和使用频率

如何使用Scp逃逸路径中的空间(How to Escape Spaces In The Path With Scp)

If there are spaces in the local or remote path it may become a problem. Say we have a folder named  “test 1”. There are two solutions for this

如果本地或远程路径中有空格,则可能会成为问题。 假设我们有一个名为“ test 1”的文件夹。 有两种解决方案

$ scp  original-ks.cfg[email protected]:/root/Downloads/test\\\ 1/

We have simple add \\\ before space in the folder name.

我们在文件夹名称中的空格前简单添加了\\\

$ scp  original-ks.cfg[email protected]:"'/root/Downloads/test 1/'"

We have added a single and double quote around path like above.

我们在上述路径周围添加了单引号和双引号。

SCP表现 (SCP Performance)

As we know SCP uses some encryption algorithms. Scp by default uses Triple-DES to encrypt network traffic. Triple-DES can cause some performance bottlenecks so faster Blowfish or aes128-ctr encryption algorithm usage will make transfers faster.

众所周知,SCP使用了一些加密算法。 默认情况下,Scp使用Triple-DES加密网络流量。 三重DES可能会导致一些性能瓶颈,因此更快的Blowfish或aes128-ctr加密算法使用将使传输速度更快。

$ scp -c aes128-ctr tmux.tar ubu1:/home/ismail/backup/
Linux SCP Command Usage With Examples Infografic
Linux SCP Command Usage With Examples Infografic
Linux SCP命令用法与示例示例

翻译自: https://www.poftut.com/linux-scp-command-usage-with-examples/

linux scp命令用法

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值