Cyberithub

12 Best Examples of SCP Command in Linux

In this article, I will take you through 12 Best Examples of SCP Command in Linux. SCP is known as secure copy which basically is used to securely copy files and directories to remote server. It uses AES-128 encryption algorithm to encrypt and transfer files and directories securely. SCP command in Linux uses same mechanism as ssh to connect to the remote Server. You can also change the encryption algorithm to transfer files and directories.

12 Best Examples of SCP Command in Linux 1

SCP Command in Linux

Also Read: 10 Best Examples of cp command in Linux

1. Copy files with Verbose Option

Sometimes you get into some issue while trying to copy file to the remote location. To track the issue you might want to use -v verbose option to troubleshoot the issue as shown in below output.

[root@localhost ~]# scp -v /root/file2.txt root@192.168.0.110:/tmp
Executing: program /usr/bin/ssh host 192.168.0.110, user root, command scp -v -t /tmp
OpenSSH_8.0p1, OpenSSL 1.1.1c FIPS 28 May 2019
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: configuration requests final Match pass
debug1: re-parsing configuration
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: Connecting to 192.168.0.110 [192.168.0.110] port 22.
debug1: Connection established.

debug1: Authenticating to 192.168.0.110:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256 need=32 dh_need=32
debug1: kex: curve25519-sha256 need=32 dh_need=32
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:V8Y4WCCEJKcQiM52XJz4OdKaYl+jAAb7Eo1ZbzUFU2E
debug1: Host '192.168.0.110' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:2

debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KCM:)

debug1: Next authentication method: password
root@192.168.0.110's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.0.110 ([192.168.0.110]:22).

debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t /tmp
Sending file modes: C0644 3893 file2.txt
Sink: C0644 3893 file2.txt
file2.txt 100% 3893 894.3KB/s 00:00
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
Transferred: sent 5912, received 2500 bytes, in 0.1 seconds
Bytes per second: sent 55438.6, received 23443.3
debug1: Exit status 0

NOTE:

Please note that here I am using root user to run all the commands. You can use any user with required permissions to run these commands.

2. Limit File Transfer Bandwidth

If you want to limit the transfer of file bandwidth then you need to use -l option as shown in below example. In this example, we are trying to limit the file transfer bandwidth to 100 kbit/s.

[root@localhost ~]# scp -l 100 /root/file2.txt root@192.168.0.110:/root
root@192.168.0.110's password:
file2.txt 100% 48KB 18.7KB/s 00:02

3. Preserve File Permission During Transfer

If you want to preserve all the file permissions to the destination path then you need to use -p option as shown below. This option will not change the read, write and execute permissions of the file during remote transfer. It will be kept as it is.

[root@localhost ~]# scp -p /root/file2.txt root@192.168.0.110:/root
root@192.168.0.110's password:
file2.txt 100% 48KB 24.1MB/s 00:00

4. Transfer File with Compression Enabled

If you want to reduce the file size for copy operations then you need to use -C option to enable the compression as shown below. By reducing the file size you can increase the file transfer speed. This option will be most helpful when you are trying to large size of files and directories.

[root@localhost ~]# scp -C hello.txt root@192.168.0.110:/opt
root@192.168.0.110's password:
hello.txt 100% 48KB 7.5MB/s 00:00

5. Copy Files Quietly

If you want to copy files and directories without showing any warning messages then you need to use -q option as shown below. This option will also disables the progress meter and diagnostic messages.

[root@localhost ~]# scp -q hello.txt root@192.168.0.110:/opt
root@192.168.0.110's password:

6. Transfer File Based on IPv4 Address Only

If you want to tell your system to use only IPV4 Address to contact remote server for copy operations then you need to use -4 option as shown in below output. In this we are trying to copy local file hello.txt to remote server 192.168.0.110 under /opt directory using scp command in Linux.

[root@localhost ~]# scp -4 hello.txt root@192.168.0.110:/opt
root@192.168.0.110's password:
hello.txt 100% 48KB 22.7MB/s 00:00

7. Copy File between Remote Hosts Using localhost

If you want to copy files between 2 remote hosts using localhost then you need to use -3 option as shown below. You can also copy files and directories between 2 remote hosts without using localhost. In that it will be direct copy from one remote host to another.

root@localhost:~# scp -3 root@192.168.0.101:/root/example root@192.168.0.110:/opt

NOTE:

Please note that in this example I have enabled ssh Passwordless authentication with both the remote servers i.e 192.168.0.101 and 192.168.0.110 for smooth transfer of files and directories between the hosts. You can either do this or you can also use password based authentication to transfer files from one host to another. You can use any of this method depends on your requirement. You can check Passwordless ssh login using ssh keygen in 6 Easy Steps to enable passwordless authentication.

8. Recursively Copying all the files and Directories

If you want to copy all the files and directories to the remote server then you need to recursively copy it through -r option as shown below. This option will enable scp command in linux to copy entire files, directories and sub-directories to copy into the remote server path.

[root@localhost ~]# scp -r /root/test/ root@192.168.0.110:/opt
root@192.168.0.110's password:
example1.txt 100% 48KB 20.6MB/s 00:00
example2.txt 100% 48KB 25.0MB/s 00:00
example3.txt 100% 48KB 29.8MB/s 00:00

9. Disable Strict File Checking during scp from Remote to Local

If you want to disable strict file checking during scp from remote to local then you need to use -T option as shown below. This option will tell the system to disable strict file checking for any unnecessary and unwanted files. By default, it will do this checks when we try to scp from remote to local server.

[root@localhost ~]# scp -T /root/test/example1.txt root@192.168.0.110:/opt
root@192.168.0.110's password:
example1.txt 100% 48KB 22.4MB/s 00:00

10. Transfer File Using Key Based Authentication

If you want to transfer file based on key based authentication and not using password based authentication then  you need to use -i option and pass the private key to authentication through the remote server.

[root@localhost ~]# scp -i key example.txt centos@100.21.67.166:/home/centos
example.txt 100% 48KB 9.5MB/s 00:00

11. Transfer File Using different Port

If you want to transfer files and directories on a different port rather than default Port 22 then you need to use -P option and pass the port number to connect and initiate the transfer.

[root@localhost ~]# scp -P 4800 hello.txt root@192.168.0.110:/opt
root@192.168.0.110's password:
hello.txt 100% 48KB 27.2MB/s 00:00

NOTE:

Please make sure to open Port 4800 in the remote host before initiate a file transfer.

12. Transfer File Using DES Encryption

As stated earlier, by default scp command in linux uses AES-128 encryption algorithm to encrypt files and directories during transfer. You can also use another encryption algorithm using -c option as shown below. In this example, we are using 3des-cbc cipher to encrypt the files and directories.

[root@localhost ~]# scp -c 3des-cbc -r example/ root@192.168.0.110:/opt
root@192.168.0.110's password:
file.txt 100% 3893 1.7MB/s 00:00
file2.txt 100% 3893 2.7MB/s 00:00
file.list 100% 47 45.0KB/s 00:00

 

Also Read: SCP Man Page

Leave a Comment