Table of Contents
- 1. SCP Syntax
- 2. Copying a File from Local to Remote Host
- 3. Copying Files and Directories Recursively
- 4. Copying File from Remote to Local Host
- 5. Copying File from one Remote Host to other Remote Host
- 6. Limiting Bandwidth Usage While Copying
- 7. Specify Specific Port with SCP
- 8. Enabling Compression While Copying
- 9. Preserve Permissions, Modes, and Access Time of Files While Copying
- Conclusion
SCP stands for secure copy. It is a Linux command-line utility used to securely copy files and directories between servers. SCP uses the SSH protocol, so it requires a password or password less authentication between servers. With SCP, you can copy files between two remote hosts from your local system, as well as copy files between local and remote hosts.
In this tutorial, we will show you how to use the SCP command with examples.
1. SCP Syntax
The basic syntax of the SCP command is shown below:
scp [option] user@source-ip:/file_or_directory user@dest-ip:/directory
user: Name of the user.
source-ip: IP address of the system from where you want to copy files or directories.
dest-ip: IP address of the system destination system.
Some of the options used in scp command are listed below:
-P: Specify the ssh port number of the destination host.
-r: Used to copy files and directories recursively.
-v: Used to display verbose output during file transfer.
-C: Used to enable file compression.
-i: Specify the SSH key.
-l: Used to limit the bandwidth while copying.
-p: Used to preserve permissions, modes, and access time of files while copying.
2. Copying a File from Local to Remote Host
If you want to copy a file named /etc/rc.local from localhost to the remote host (172.20.10.3) in /mnt directory, run the following command:
scp /etc/rc.local [email protected]:/mnt/
You should see the following output:
[email protected]'s password: rc.local 100% 306 0.3KB/s 00:00
If you want to copy multiple files (in this example we will use /etc/fstab and /etc/hosts, and copy them to the remote host (172.20.10.3) into /mnt directory), run the following command:
scp /etc/fstab /etc/hosts [email protected]:/mnt/
You should see the following output:
[email protected]'s password: fstab 100% 628 0.6KB/s 00:00 hosts 100% 249 0.2KB/s 00:00
3. Copying Files and Directories Recursively
You can use the recursive option -r with SCP to copy the entire directory from one system to another.
For example, to copy the directory named /var/log/apache2 from localhost to the remote host (172.20.10.3) in /mnt directory recursively, run the following command:
scp -r /var/log/apache2 [email protected]:/mnt/
You should see the following output:
[email protected]'s password: error.log 100% 0 0.0KB/s 00:00 access.log.3.gz 100% 61KB 61.3KB/s 00:00 other_vhosts_access.log 100% 0 0.0KB/s 00:00 access.log.4.gz 100% 317 0.3KB/s 00:00 error.log.3.gz 100% 1622 1.6KB/s 00:00 error.log.1 100% 2352 2.3KB/s 00:00 access.log 100% 0 0.0KB/s 00:00 access.log.1 100% 1287 1.3KB/s 00:00 access.log.2.gz 100% 239 0.2KB/s 00:00 error.log.2.gz 100% 246 0.2KB/s 00:00 error.log.5.gz 100% 1471 1.4KB/s 00:00 error.log.4.gz 100% 2296 2.2KB/s 00:00
You can also use -v option with SCP to display verbose output during the copying process:
scp -vr /var/log/apache2 [email protected]:/mnt/
4. Copying File from Remote to Local Host
If you want to copy a file named /etc/hostname from the remote host (172.20.10.3) to the localhost in /opt directory, run the following command:
scp [email protected]:/etc/hostname /opt/
You should see the following output:
[email protected]'s password: hostname 100% 6 0.0KB/s 00:00
5. Copying File from one Remote Host to other Remote Host
To copy files and directories between two remote hosts, you will need to configure SSH key-based authentication between both remote hosts.
After configuring key-based authentication, copy a directory named /var/log from one remote host (172.20.10.3) to the other remote host (172.20.10.4) in /mnt directory, run the following command:
scp -r [email protected]:/var/log [email protected]:/mnt/
6. Limiting Bandwidth Usage While Copying
You can use -l option with SCP to limit the bandwidth during the copying process.
For example, to copy a file named google-chrome-stable_current_amd64.deb from localhost to the remote host (172.20.10.3) in /opt directory and limit the bandwidth to 1000 KB/sec (1000×8), run the following command:
scp -l 8000 /home/vyom/Downloads/google-chrome-stable_current_amd64.deb [email protected]:/opt/
You should see the following output:
[email protected]'s password: google-chrome-stable_current_amd64.deb 100% 60MB 1.0MB/s 01:01
7. Specify Specific Port with SCP
In some cases, the SSH port is different on the destination host. In this case, you can use -P option to specify the SSH port.
For example, to copy a file named /etc/hosts on the local system to the remote host (172.20.10.3) in /mnt directory using the port 8088, run the following command:
scp -P 8088 /etc/hosts [email protected]:/mnt/
8. Enabling Compression While Copying
You can use -C option with SCP to enable compression at source and decompression at the destination host. This can increase the transfer speed on large files.
For example, to copy a directory /var/log/nginx on the local host to the remote host (172.20.10.3) in /mnt directory with compression, run the following command:
scp -r -C /var/log/nginx [email protected]:/mnt/
You should see the following output:
[email protected]'s password: error.log 100% 0 0.0KB/s 00:00 access.log.3.gz 100% 217 0.2KB/s 00:00 error.log.1 100% 974 1.0KB/s 00:00 access.log 100% 0 0.0KB/s 00:00 access.log.1 100% 90 0.1KB/s 00:00 access.log.2.gz 100% 239 0.2KB/s 00:00
9. Preserve Permissions, Modes, and Access Time of Files While Copying
You can use -p option with SCP to preserve permissions, access time, and modes during the copying process.
For example, to copy a file named magento-ce-2.3.5-p1-2020-04-24-08-59-28.tar.bz2 on the local host to the remote host (172.20.10.3) in /mnt directory and preserve permissions, access time, and modes, run the following command:
scp -p /home/vyom/Downloads/magento-ce-2.3.5-p1-2020-04-24-08-59-28.tar.bz2 [email protected]:/mnt/
You should see the following output:
[email protected]'s password: magento-ce-2.3.5-p1-2020-04-24-08-59-28.tar.bz2 100% 112MB 55.9MB/s 00:02
Conclusion
In the above guide, you learned how to use SCP to securely transfer files and directories from one server to another. Get started with SCP on a VPS Hosting account with Atlantic.Net today!