Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SCP command to transfer files safely in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux how to use the SCP command to transfer files safely, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

SCP (secure copy Secure Copy) is a command-line tool in systems such as Linux and Unix for securely transferring files and directories across systems over a network. When we use the scp command to copy files and directories from the local system to the remote system, we establish an ssh connection to the remote system at the back end. In other words, we can say that scp uses the same SSH security mechanism on the back end, which requires a password or key for authentication.

In this tutorial, we will discuss 14 examples of useful Linux scp commands.

Scp command syntax:

# scp user name @ target host: / # scp user name @ target host: / file

The first syntax of the scp command demonstrates how to copy a file or directory from the local system to the target host under a specific folder.

The second syntax of the scp command demonstrates how to copy files from the target host to the local system.

Here are some of the most widely used options in the scp command

-C enable compression

-I specify the identification file or private key

-l limit bandwidth when replicating

-P specifies the ssh port number of the target host

-p preserve the permissions, mode, and access time of the file when copying

-Q disable SSH warning messages

-r Recursively copy files and directories

-v detailed output

Now let's jump to the example!

Example: 1) use scp to copy files from the local system to the remote system

Suppose we want to use the scp command to copy jdk's rpm package from the local Linux system to the remote system (172.20.10.8), use the following command

[root@linuxtechi ~] $scp jdk-linux-x64_bin.rpm root@linuxtechi:/optroot@linuxtechi's password:jdk-linux-x64_bin.rpm 100% 10MB 27.1MB/s 00:00 [root@linuxtechi ~] $

The above command copies the rpm package file for jdk to the remote system under the / opt folder.

Example: 2) use scp to copy files from the remote system to the local system

Suppose we want to copy files from the remote system to the / tmp folder on the local system, execute the following scp command

[root@linuxtechi ~] $scp root@linuxtechi:/root/Technical-Doc-RHS.odt / tmproot@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) detailed output when transferring files using scp (- v)

In the scp command, we can use the-v option to enable verbose output. Using detailed output, we can easily find out exactly what is going on in the background. This is useful for debugging issues such as connection, authentication, and configuration.

Root@linuxtechi ~] $scp-v jdk-linux-x64_bin.rpm root@linuxtechi:/optExecuting: program / usr/bin/ssh host 172.20.10.8, user root, command scp-v-t / optOpenSSH_7.8p1 OpenSSL 1.1.1 FIPS 11 Sep 2018debug1: Reading configuration data / etc/ssh/ssh_configdebug1: Reading configuration data / etc/ssh/ssh_config.d/05-redhat.confdebug1: Reading configuration data / etc/crypto-policies/back-ends/openssh.configdebug1: / 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: passwordroot@linuxtechi's password: example: 4) transfer multiple files to a remote system

You can use the scp command to copy / transfer multiple files to a remote system at once, specify multiple files in the scp command, and separate them with spaces, as shown in the following example

[root@linuxtechi ~] $scp install.txt index.html jdk-linux-x64_bin.rpm root@linuxtechi:/mntroot@linuxtechi's password:install.txt 100% 0.0KB/s 00:00index.html 100% 85KB 7.2MB/s 00:00jdk-linux-x64_bin.rpm 100% 10MB 25.3MB/s 00:00 [root@linuxtechi ~] $example: 5) transfer files between two remote hosts

Using the scp command, we can copy files and directories between two remote hosts, assuming that we have a local Linux system that can connect to two remote Linux systems, so from my local Linux system, I can use the scp command to copy files between the two systems

Command syntax:

# scp username @ remote host 1 / username @ remote host 2Rose /

Examples are as follows:

# 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) Recursively copy files and directories (- r)

Use the-r option in the scp command to recursively copy the entire directory from one system to another, as shown in the following example:

[root@linuxtechi] $scp-r Downloads root@linuxtechi:/opt

Verify that the Downloads folder has been copied to the remote system using the following command

[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 transmission speed by enabling compression (- C)

In the scp command, we can increase the transfer speed by enabling compression using the-C option, which automatically enables compression on the source host and decompresses it on the target 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 when replicating (- l)

Use the-l option in the scp command to set the limit on bandwidth usage during replication. Bandwidth is specified in Kbit/s, as shown in the following example:

[root@linuxtechi ~] $scp-l 500 jdk-linux-x64_bin.rpm root@linuxtechi:/var example: 9) specify a different ssh port when scp (- P)

In some cases, the ssh port on the target host changes, so when using the scp command, we can use the-P option to specify the ssh port number.

[root@linuxtechi] $scp-P 2022 jdk-linux-x64_bin.rpm root@linuxtechi:/var

In the above example, the ssh port of the remote host is "2022".

Example: 10) retain the permissions, mode, and access time of the file when copying (- p)

"when copying from source to destination, use the-p option in the scp command to retain permissions, access time, and mode."

[root@linuxtechi ~] $scp-pjdk-linux-x64_bin.rpm root@linuxtechi:/var/tmpjdk-linux-x64_bin.rpm 100% 10MB 13.5MB/s 00:00 [root@linuxtechi ~] $example: 11) transfer files in quiet mode in scp (- Q)

Use the-Q option in the scp command to suppress the display of transfer progress, warnings, and diagnostic messages for ssh. The example is as follows:

[root@linuxtechi ~] $scp-Q-r Downloads root@linuxtechi:/var/tmp [root@linuxtechi ~] $example: 12) use the identification file in scp when transferring (- I)

In most Linux environments, key-based authentication is preferred. In the scp command, we use the-I option to specify the identification file (private key file), as shown in the following example:

[root@linuxtechi] $scp-I my_key.pem-r Downloads root@linuxtechi:/root

In the above example, my_key.pem is an identification file or a private key file.

Example: 13) use other ssh_config files in scp (- F)

In some cases, you use different networks to connect to the Linux system, and some networks may be behind the proxy server, so in this case, we must have different ssh_config files.

Different ssh_config files are specified in the scp command with the-F option, as shown in the following example:

[root@linuxtechi] $scp-F / home/pkumar/new_ssh_config-r Downloads root@linuxtechi:/rootroot@linuxtechi's password:jdk-linux-x64_bin.rpm 100% 10MB 16.6MB/s 00:00backup-Oct.zip 100% 713MB 41.9MB/s 00:17index.html 100% 85KB 6 .6MB / s 00:00 [root@linuxtechi ~] $example: 14) use other encryption methods in the scp command (- c)

By default, scp uses AES-128 encryption to encrypt files. If you want to use other encryption methods in the scp command, use the-c option, followed by the encryption method name.

Suppose we want to use 3des-cbc encryption when transferring files with the scp command, run the following scp command:

[root@linuxtechi] # scp-c 3des-cbc-r Downloads root@linuxtechi:/root

Use the following command to list the encryption methods supported by ssh and scp:

[root@linuxtechi ~] # ssh-Q cipher localhost | paste-d,-s-3desmurc Cbctech Aes128Michel Cbctechi Aes256lb cbctechiAes128CtruxtechiAes128MictrtechiAes256 CtruxtechiAes256CtruxtechiDrootfuglinuxtechi [root@linuxtechi ~] # does it help you to read the above contents? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report