How to Use SCP Command to Securely Transfer Files and Directories

Published on  

14 min read

How to Use SCP Command to Securely Transfer Files and Directories

Learn how to securely transfer files and directories between different machines using the SCP command. Our comprehensive guide covers the basic syntax, useful options, and step-by-step instructions for transferring files both to and from a remote server.

If you're someone who often needs to transfer files and directories or folders between different servers or between your local machine and a remote server, then the SCP command is a great tool to have in your arsenal.

SCP stands for Secure Copy Protocol and is a way to securely transfer files and directories or folders between two different machines over an encrypted SSH connection. SCP is developed by OpenBSD on the Secure Shell (SSH) protocol. SCP is also known as OpenSSH secure file copy. In this article, we'll go over how to use the SCP command to securely transfer files and directories.

Prerequisites

Before we dive into the details of how to use the SCP command, let's make sure we have everything we need to get started:

  • A remote server to transfer files to or from
  • A user account on the remote server with SSH access
  • A local machine with the SCP command installed (most Linux and macOS systems come with SCP preinstalled)

Basic syntax of the SCP command

The basic syntax of the SCP command is as follows:

scp [options] [source] [destination]
  • [options]: Any additional options you want to use with the SCP command (we'll cover some useful options later on in this post)
  • [source]: The path to the file or directory or folder you want to transfer
  • [destination]: The destination path where you want to transfer the file or directory to

Transfer a file using the SCP command

Let's start with a simple example: transferring a file from our local machine to a remote server. Here's the command you would use:

scp /path/to/local/file.txt username@remote:/path/to/remote/
  • /path/to/local/file.txt: The path to the file you want to transfer
  • username: Your username on the remote server
  • remote: The IP address or domain name of the remote server
  • /path/to/remote/: The destination path where you want to transfer the file to on the remote server

SCP Command Example:

scp /home/developer/file.txt [email protected]:/home/root

In order to copy the file named file.txt, the remote user with the username root and the remote having the IP address 10.10.10.10 can be specified. In case you do not specify a remote directory, the file will be automatically copied to the home directory of the remote user. Upon initiation of the file transfer, you will be prompted to enter the password of the user. The process of transfer will begin thereafter.

If you do not include the filename in the destination directory, the file will be copied to the new location with its original name. However, if you wish to save the file under a different name, you must provide the new name while copying.

scp /home/developer/file.txt [email protected]:/home/root/newname.txt

In case the SSH connection on the remote server is active on a port other than the default port number 22, you can indicate the port number explicitly by utilizing the -P parameter:

scp -P 2233 /home/developer/file.txt [email protected]:/home/root

Transfer a directory or folder using the SCP command

Transferring a directory is just as easy as transferring a file. Here's the command you would use:

scp -r /path/to/local/directory/ username@remote:/path/to/remote/
  • -r: The -r option tells the SCP command to transfer the directory recursively (i.e., transfer all the files and subdirectories/sub-folders within the directory/folder)
  • /path/to/local/directory/: The path to the directory/folder you want to transfer
  • username: Your username on the remote server
  • remote: The IP address or domain name of the remote server
  • /path/to/remote/: The destination path where you want to transfer the directory/folder to on the remote server

SCP Command Example:

scp -r /home/developer/testfolder [email protected]:/home/root

By entering this command, local folder testfolder and it's files will be copied to /home/root directory of 10.10.0.10 server. The newly created directory will be same name as local directory name. If you want to change the directory name, you should enter below command:

scp -r /home/developer/testfolder [email protected]:/home/root/newtestfolder

Transfer a file from a remote server to your local machine using the SCP command

Transferring a file from a remote server to your local machine is just as easy as transferring a file from your local machine to a remote server. Here's the command you would use:

scp username@remote:/path/to/remote/file.txt /path/to/local/
  • username: Your username on the remote server
  • remote: The IP address or domain name of the remote server
  • /path/to/remote/file.txt: The path to the file you want to transfer on the remote server
  • /path/to/local/: The destination path where you want to transfer the file to on your local machine

Transfer a directory from a remote server to your local machine using the SCP command

Transferring a directory from a remote server to your local machine is just as easy as transferring a file from a remote server to your local machine. Here's the command you would use:

scp -r username@remote:/path/to/remote/directory/ /path/to/local/
  • -r: The -r option tells the SCP command to transfer the directory recursively (i.e., transfer all the files and subdirectories within the directory)
  • username: Your username on the remote server
  • remote: The IP address or domain name of the remote server
  • /path/to/remote/directory/: The path to the directory you want to transfer on the remote server
  • /path/to/local/: The destination path where you want to transfer the directory to on your local machine

Transfer a File Between Two Remote Systems using the SCP command

Transferring a file between two remote systems using the SCP command is a straightforward process. All you need is SSH access to both systems and a basic understanding of the SCP command syntax. In short, you use the same SCP command syntax that you would use to transfer a file from a local machine to a remote server, but with the additional step of specifying the source and destination systems.

Here's the basic syntax you would use to transfer a file between two remote systems using the SCP command:

scp [options] username1@remote1:/path/to/source/file.txt username2@remote2:/path/to/destination/
  • [options]: Any additional options you want to use with the SCP command.
  • username1: Your username on the source remote system.
  • remote1: The IP address or domain name of the source remote system.
  • /path/to/source/file.txt: The path to the file you want to transfer on the source remote system.
  • username2: Your username on the destination remote system.
  • remote2: The IP address or domain name of the destination remote system.
  • /path/to/destination/: The destination path where you want to transfer the file to on the destination remote system.

As with any file transfer, make sure you have the necessary permissions to access the file and directory on both remote systems before initiating the transfer. With this basic syntax and some SSH access, you should be able to securely transfer files between two remote systems using the SCP command in no time!

Transfer multiple files and directories using the SCP command

As we have seen in the previous examples, the process of transferring a file to a remote system can be carried out using the SCP command. In the following section, we shall explore the process of using this command to transfer multiple files from a local system to a remote system.

scp [file-1] [file-2] [file-n] username@remote:/path/to/remote/directory/

SCP Command Example:

scp file1.txt file2.txt [email protected]:/home/root

By entering this command, both file1.txt and file2.txt will be copied to /home/root directory of 10.10.0.10 server. If you want to transfer multiple folders, enter below command with -r parameter:

scp -r folder1/ folder2/ [email protected]:/home/root

If you want to transfer files and folder at the same time you have to enter the following SCP command:

scp -r file1.txt file2.txt folder1/ [email protected]:/home/root

Useful options

Now that we've covered the basic syntax of the SCP command and how to transfer files and directories, let's take a look at some useful options you can use with the SCP command:

  • -p: The -p option preserves the modification times, access times, and modes of the original files and directories.
  • -q: The -q option suppresses the progress meter and non-error messages.
  • -C: The -C option compresses the data being transferred, which can speed up the transfer over slow network connections.

SCP: permission denied

SCP permission denied error can occur due to various reasons such as incorrect permissions, incorrect login credentials, or firewall restrictions. Take a look below scp command example:

scp file1.txt [email protected]:/home/root

Output:

scp:/home/root: permission denied

If the output refers something like scp permission denied  or scp write remote failure that means the user developer do not have proper permissions to store file1.txt on /home/root directory. Ensure that the file or directory you are trying to transfer has the correct permissions. Check if the file or directory has read and write permissions for the user or group you are using to access it.

SCP: Login credentials error

Double-check the username and password you are using to access the remote system. Ensure that you have the correct login credentials to access the system.

SCP: Firewall settings error

Check if the firewall is blocking the transfer. You may need to modify the firewall settings to allow the transfer.

SCP: SSH is not enabled error

Ensure that SSH is enabled on the remote system. If it is not enabled, you may need to enable it to allow the transfer.

SCP: Other permission error or sudo error

If you are facing permission denied error while copying files to a directory, try using sudo before the command. This will allow you to execute the command with root privileges and bypass any permission restrictions.

Problems with SCP Command

The SCP command can encounter both performance and security issues while transferring files between different machines. Here are some of the most common concerns related to these aspects:

Performance Issues:

  • Slow Transfer Speed: SCP can be slow, particularly when transferring large files or multiple files at once.
  • No Transfer Resuming: SCP doesn't allow transfer resuming, so if a transfer is interrupted or fails, you'll need to restart the entire transfer from the beginning.

Security Issues:

  • Lack of Encryption: By default, SCP transfers are encrypted, but the encryption can be disabled which could lead to security breaches.
  • Vulnerability to Man-in-the-Middle Attacks: SCP is vulnerable to man-in-the-middle attacks, particularly if used on an unsecured network, where an attacker could intercept the data and gain access to the transferred files.
  • Potentially Deprecated: According to OpenSSH, the SCP protocol is several decades old, has not been updated and is known to have several security vulnerabilities.

Alternatives to SCP Command

So, SCP has been found to have some issues. What other options are available to transfer files between computers without the need for setting up a new system or using slow physical transfer methods?

SFTP is often considered the successor to SCP and is built on top of SSH for transport-layer security. Unlike SCP, SFTP does not require separate access setup and can provide a custom interactive prompt for navigating the remote filesystem, or you can automate commands using a pre-written script.

Another viable alternative is Rsync, which can serve as a drop-in replacement for the SCP command, leveraging SSH for secure transfer. Rsync is primarily focused on performance and utilizes local computation to reduce network data transfer. Rather than purely transferring data, Rsync does data synchronization and only sends the differences between remote and local content if they are similar.

Conclusion

The SCP command is a powerful tool for securely transferring files and directories/folders between different machines over an encrypted SSH connection. With the basic syntax and some useful options covered in this post, you should be well-equipped to start using the SCP command in your own workflow. Happy transferring!

About the Author

Rexposed Staff

Was this helpful?