Warning icon

New location: Zurich, Switzerland

SCP Linux Command for Remote File Copy

SCP Linux Command for Remote File Copy
213

What is SCP? 

If you would like to transfer files between Unix or Linux computers securely without having to worry about any unauthorized viewings, you’d need to do that with the SCP commands. SCP means secure copy. It is a command-line tool used to copy or transfer files and directories across Unix and Linux systems securely.

For example, if you want to copy files to Linux, and you’re worried about prying eyes on the network having access to your files, you can easily use the SCP commands which password authentication.  SCP commands SSH to transfer data and that files and passwords are encrypted to avoid unauthorized viewings.

SCP COMMAND SYNTAX  

To understand how to use SCP commands, it’s important to have a basic understanding of SCP syntax, as knowledge of SCP syntax helps you to understand the systemic orderly arrangement of SCP commands and helps you understand the rules for forming them.

SCP syntax assumes this form:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

  • SCP – Secure Copy
  • OPTION- This refers to SCP options like ssh port, recursive copy, ssh configuration, cipher, etc.
  • [user@]SRC_HOST:]file1 – This is the source file. In simple terms, a source file is a file where original data originates or a file that contains program instructions. It’s also referred to as the source code.
  • [user@]DEST_HOST:]file2 – This is the destination file, where data from the source file is stored in.

There are also some designated SCP options that regulate SCP commands behavior:

  • -r: This option is used to copy directories recursively.  
  • -P: This option stipulates the remote host SSH port.
  • -p:  This is the option to protect access times and any modification to any file.
  • -C: To compress data while it transfers to the destination machine in a Linux system, you’d need to use the -C SCP option.
  • -q: This option is used to get the progress meter and non-error messages suppressed.
  • -v: This is for verbose output
  • -I: This is to identify a file or private key

Some important points:

Because SCP commands use SSH, to transfer data or copy files to Linux would require the use of authentication passwords for security purposes. To copy the file to Linux systems, you’d need to have read and write permissions on the source file and on the target system respectively.

Adequate care and attention should be given to files that have the same name and location on the source system and the destination system because copying such files from one Linux system to another using the SCP commands will overwrite them without notifying the operator.

SCP commands use the “: ” (colon) to differentiate local locations from remote ones.

14 SCP examples to securely transfer Files 

Now that you have a basic understanding of SCP syntax, let’s get on with 14 examples of how to use SCP commands to securely transfer or copy files to Linux.

1. Using SCP commands to copy files from a remote system to a local system: If you’d like to copy the file to Linux system from another remote Linux system, you can use the following SCP command while obeying the rules of SCP syntax:

[user@linux ~]$ scp [email protected]:/root/Technical-Doc.odt /tmp

[email protected]’s password:

Technical-Doc.odt                         100% 109KB  3.1MB/s   00:00

[user@linux ~]$ ls -l /tmp/Technical-Doc.odt

-rwx——. 1 user user 1135521 Oct 19 11:12 /tmp/Technical-Doc.odt

[user@linux ~]$

This example of how to copy files to Linux from a remote system under the /tmp folder.

2. Using SCP commands to copy a file from a local Linux system to a remote Linux system: To copy the file to Linux from a local Linux system, execute the following command following the exact scp syntax as follows:

[user@linux ~]$ scp jdk-linux-x64_bin.rpm [email protected]:/opt

[email protected]’s password:

jdk-linux-x64_bin.rpm                          100%   10MB  27.1MB/s   00:00

[user@linux ~]$

3. Copy file to Linux recursively: As earlier stated while discussing basic SCP syntax, the -r option is used to copy files and directory recursively. Applying this to execute the SCP commands, follow this example to copy the file to Linux from another Linux:

[user@linux ~]$ scp -r Downloads [email protected]:/opt

To confirm if download folder has been copied to a remote system, use this command:

[user@linux ~]$ ssh [email protected] “ls -ld /opt/Downloads”

drwxr-xr-x. 2 root root 75 Oct 19 12:10 /opt/Downloads

[user@linux ~]$

4. Using SCP commands to transfer files across two remote Linux systems:  To copy a file from one local Linux system across two remote Linux systems, follow this scp syntax:

# scp user@remote_hosts1:/<files_to_transfer>  user@remote_host2:/<folder>

For example:

# scp [email protected]:~/backup-Oct.zip [email protected]:/tmp

# ssh [email protected] “ls -l /tmp/backup-Oct.zip”

-rwx——. 1 root root 747438080 Oct 19 12:02 /tmp/backup-Oct.zip

5. Using SCP commands to transfer numerous files to a remote Linux system: You can take advantage of SCP commands to copy multiple files from a local Linux to a remote Linux by specifying the files and separating them with space as shown below:

[user@linux ~]$ scp install.txt index.html jdk-linux-x64_bin.rpm [email protected]:/mnt

[email protected]’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
[user@linux ~]$

6.Using SCP commands to enable verbose output while copying files: In scp syntax, (-v) is the option required for verbose output. You can use this option to solve certain configuration, connection or authentication problems in the background:

user@linux ~]$ scp -v jdk-linux-x64_bin.rpm [email protected]:/opt

Executing: program /usr/bin/ssh host 10.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 10.20.10.8 [10.20.10.8] port 22.

debug1: Connection established.

…………

debug1: Next authentication method: password

[email protected]’s password:

7. Using SCP commands to limit bandwidth usage while copying: The scp syntax prescribes the ‘-I’ option for limiting bandwidths:

[user@linux ~]$ scp -l 500 jdk-linux-x64_bin.rpm  [email protected]:/var

8. Increasing transfer speed using SCP commands: You can increase transfer speed when copying files from one Linux to another by enabling compression using the ‘-C’ option as already studied in the spc syntax basics above. An example is:

user@linux ~]$ scp -r -C Downloads [email protected]:/mnt

9. Using a different cipher in SCP commands: SCP normally uses AES-128 cipher for file encryption. To use another cipher, use the -c option just before the cipher name. An example is:

[root@linux ~]# scp -c 3des-cbc -r Downloads [email protected]:/root

In the example above, 3des-cbc is the cipher name you want to use

10.Using the -p option to specify different ssh port using scp command:

[user@linux ~]$ scp -P 2022 jdk-linux-x64_bin.rpm  [email protected]:/var

11. Use SCP commands to enable use of different “ssh_config” file:  when you use different networks to connect to your Linux, its possible that some of them may be behind proxy servers, in which case you may need a different ssh_config file. To specify this file, use the -F option in scp commands. An example is shown:

[user@linux ~]$ scp -F /home/user/new_ssh_config -r Downloads [email protected]:/root

[email protected]’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

[user@linux ~]$

12. How to Use the -p option to preserve modes and access times:

[user@linux ~]$ scp -p jdk-linux-x64_bin.rpm  [email protected]:/var/tmp

jdk-linux-x64_bin.rpm                            100%   10MB  13.5MB/s   00:00

[user@linux ~]$

13.Specify identify file or private key file with the -i option:

[user@linux ~]$ scp -i my_key.pem -r Downloads [email protected]:/root

14.Copy files in quiet mode with the -q option:

[user@linux ~]$ scp -q -r Downloads [email protected]:/var/tmp

[user@linux ~]$

Conclusion 

An SCP means a secure copy. It’s a convenient way of ensuring the security of files while they’re being transferring from one Linux system to another using the SSH that allows for it to encrypt both files and passwords.

The SCP command has a variety of options that helps regulate SCP command behavior and undertake some tasks like copying directories and files recursively, modifying files, suppressing non-error messages, etc.

To make use of the commands, a basic understanding of the SCP syntax, and how they are applied is necessary, as has been shown in this article.