Atlantic.Net Blog

How to Securely Transfer Files Using SCP

Hitesh Jethva
by Atlantic.Net (64posts) under VPS Hosting
0 Comments

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.

Prerequisites

  • A fresh Ubuntu 18.04 VPS on the Atlantic.net Cloud Platform.
  • A root password configured on your server.

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 18.04 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 18.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

1. SCP Syntax

The basic syntax of the SCP command is shown below:

scp [option] [email protected]:/file_or_directory [email protected]:/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!

Get A Free To Use Cloud VPS

Free Tier Includes:
G2.1GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources