SCP Command: A Comprehensive Guide to Secure File Transfers

File transfer is an essential task in our digital life. Whether you sending files from one computer to another or sending files to your colleague security is important.

Where SCP (Secure Copy) comes in, it encrypts the file before sending which ensures the file you are transferring is not handed to an unauthorized person. SCP uses SSH (Secure Shell) to connect to the remote machine, SSH is a cryptographic network that provides rock-solid security even on an insecure network.

In this blog post, I’ll show how to use the scp command to securely copy files between remote and local machines.

SCP Command Overview

SCP (Secure Copy) is a command line tool for copying files from a remote server to a local one or vice versa. SCP is an SSH (Secure Shell) extension, which provides secure remote access to the remote Linux System. SCP is a simple yet powerful tool for managing and transferring files between two Linux systems.

How SCP Works

SCP works over SSH protocol. First, SCP makes a secure connection to the remote machine and then encrypts all the data that need to transfer and transfer the data. In the end, decrypt the transferred data.

Why Use SCP Over FTP

SCP offers several benefits over FTP. FTP is also a file transfer protocol that transfers files without encryption decryption. SCP offers

  • Security: SCP uses SSH to connect to the remote machine, uses the same authentication, and provides the same security as SSH. Which ensures secure copy between Linux machines.
  • Simplicity: SCP is simpler than more complex protocols like SFTP but is a powerful tool for copying files.
  • Flexibility: SCP offers great flexibility when moving or copying files. You can copy files from local to remote, remote to local, and remote to remote machine.

SCP Syntax

The basic syntax of the SCP command is as follows:

scp [options] [source] [destination]
  • options: SCP offers several flags for more control over transferring files. Some common flags are r (to copy directories recursively), p (to preserve file permissions and attributes), and P (to specify a custom SSH port).
  • source: This is the source location where the file is located. It can be a local machine or a remote machine.
  • destination: This is the destination location where you want to copy the file. It can be a local machine or a remote machine.

Before You Begin

SCP command don’t support navigation like SFTP, so before using scp command, note your source file location. For me, I have a backup file in my Linux server which was needed to copy in my local machine. I knew the name but didn’t know the location.

But for the scp command, you need the source location. To find the source file location, you can use the following command to know the location

sudo find / -name “backup.zip”

Basic SCP Command Examples

SCP command is a handy tool for securely copy files between two Linux/Unix machines. SCP offers a better secure data transfer rate than other protocols like SFTP. In the next section, I’ll discuss some basic examples of scp command.

Transferring Files from Local to Remote

You can copy files from Local to Remote which with scp command. To copy files first open your Linux terminal and navigate to the file’s directory. To navigate between directories, use cd command as follows

cd Documets/

Alternatively, you can specify the file location in the scp command as /home/user/Documetns.

Then use scp command to start copying the file. Enter the following line in the terminal to start the copy process.

scp example.txt [email protected]:~/files/

After entering this command, you will be prompted to enter your SSH password to start the copy process. If you use SSH key pair for authentication instead of a password, you need to specify SSH your private key location in the scp command. You can use the following command for ssh key pair authentication.

scp -i private_key.pem example.txt [email protected]:~/files/

This command will use your private key to connect the remote machine and copy the files.

scp command to copy files

Transferring Files from Remote to Local

You can also copy files from the remote to the Local machine with scp command. This process is the same as the previous command. You need to swap the source and destination locations only. To copy a file from remote to your local system, open the terminal and enter the following command

scp [email protected]:~/Documents/example.txt ~/files/

This command will ask you to enter your SSH password to continue. If you use SSH key pair for authentication, you need to use the following command specifying the SSH private key location for authentication.

scp -private_key.pem [email protected]:~/Documents/example.txt ~/files/

Transferring Files from Remote to Remote

SCP command also supports copying files from one remote machine to another machine. The process is also the same, but instead of executing the command locally, you need to execute the command from one remote machine.

To copy files from one remote to another, first connect to the remote with SSH where the file is located. You can connect to your remote server with the following command

After establishing the SSH connection, you can execute the scp command to copy files from one remote to another. The example command in this will be

scp [email protected]:~/Documents/example.txt [email protected]:~/files/

This command will copy the file from the first remote to the second.

SCP Advanced Options

In the previous example, I have shown some basic use cases of scp command. If these basic examples didn’t get you covered, scp command supports flags for more advanced usages. In the next section, I’ll discuss some advanced examples of SCP commands.

Specifying Port Number

If you have changed your SSH default port 22 to a non-standard port to enhance your server security, then you need to use -P flag to specify your non-standard SSH port every time you use scp command. You can use the following command for this

scp -P 2222 [email protected]:~/Documents/example.txt ~/files/

Preserving File Attributes

Sometimes it is necessary to preserve file attributes such as file permissions, time stamps, ownership, etc. If you want to copy files preserving these file attributes, you need to use the flag -p when using the scp command

scp -p [email protected]:~/Documents/example.txt ~/files/

Copy Entire Folder

SCP command usually copies files from one host to another, but if you need to copy a folder between local and remote, you can use scp file with flag -r, which means recursive copy. This command will recursively copy files of a folder.

For example, you have a folder called files in your local machine, and you want to copy this folder to your remote Linux server securely. Then use the following command.

scp -r ~/files/ [email protected]:~/Documents/

Bandwidth Limit

If you want to limit bandwidth use during the copy process, you can use -l flag followed by the maximum allowed bandwidth in kilobytes to limit bandwidth uses. For example, if you want to limit bandwidth use to 500 KB/s, you can use the following command

scp -l 500 [email protected]:~/Documents/example.txt ~/files/

SCP Permission Denied Issue

While transferring files from my Linux server to my local machine, I faced an issue and could not copy files to permission issues. If the file you want to copy doesn’t have proper permission, you can’t copy files, and scp will show you permission denied.

So it’s a good idea to know the permission before copying the files. To know the file permission, you can use the following command

ls -l /path/file

If you see file permission is restricted, you may need to change the permission before copying the files. You can set new permission during copy process with the following command.

chmod 755 /path/files

After copying, you can always change the permission to the previous state.

Conclusion

If you need to copy a few files from local to remote or vice versa, then scp is your best tool. SCP provides speed and security while transferring files. You can transfer any files remotely and locally by following this tutorial. I have also addressed some common issues while copying files.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *