How to use the SCP command to transfer files

The Linux Secure Copy Protocol (SCP) command is a network file transfer protocol that allows easy and secure transfer of files between a remote and a local host, or between two remote locations. It is based on the Remote Copy Protocol of .

SCP is frequently used by full-stack developers for its authentication and encryption functions without requiring third-party hosting services like Github. It’s an easy way to avoid exposing your data to packet sniffers and keep it confidential.

In essence, SCP is a mixture of RCP and (Secure Shell). It is based on the first to carry out copy operations and on the second to encrypt information and authenticate remote systems.

Unlike , all you need to use the SCP command line successfully is a username and password or passphrase for the systems involved in the transfer. This speeds up the process as there is no need to log in to any of them.

Transferring files securely is paramount when using a . Fortunately, it can be easily done using the SCP command. In this article you will learn how to use SCP commands and how they can help you.

Take advantage of discounts and upgrade your Linux VPS server. Save up to 70%!

Linux SCP Command Example Syntax

The basic SCP command example looks like this:

scp :/ :/

It may seem complicated at first, but we’ll gladly break it down!

In this example we are making a transfer between two VPS servers.

  • are modifiers that you can add to the SCP command. We will see the most popular ones later.
  • is the username and IP of the machine that has the file you want. It would look something like root@123.123.123.12
  • :/ inform the SCP command that you are about to write to the source directory
  • is where the file is located, and its name. It looks something like /users/Gustavo/Desktop/SCP.png
  • is the username and IP of the target machine.
  • finally, there is the destination directory where the file will be saved.

In a real world scenario it would look like this:

scp -p root@162.168.1.1:/media/scp.png edward@162.168.1.2:/desktop/destination

It’s pretty simple! If you’re copying to or from a local machine, you won’t need the IP address, destination, or source path like /desktop/folder_name.

See also  How to start a business from scratch (in 9 steps)

Let’s talk about other options you can use to modify the SCP command. There are 20 common ones that you can use either as a single character (-o), or as its descriptive equivalent (–option). The most used options are:

  • –P port allows to specify a different input to the server (the default port for the TCP port for the command is 22)
  • –c cipher gives you the ability to specify the encryption algorithm that the client will use. Some of the values ​​you can use are ‘aes256-ctr’, ‘aes256-cbc’, ‘blowfish-cbc’, ‘arcfour’, ‘arcfour128’, ‘arcfour256’, ‘cast128-cbc’, aes128-ctr’, ‘ aes128-cbc’, ‘aes192-ctr’, ‘aes192-cbc’, and 3des-cbc’. The default option in the shell configuration is ‘AnyStdCipher’
  • –q will run the operation in quiet mode, which means only critical errors will be displayed.
  • –r is for recursive copy, which will include all subdirectories.
  • –4 or -6 can be used if you want to choose the protocol version used, either IPv4 or IPv6. You can also configure the IP address requirements more extensively with the address-family keyword.
  • –p will preserve the initial modification times and attributes of the file.
  • –u will delete the source file after the transfer is complete.
  • –c will allow compression of the data while the transfer operation is taking place.

Aspects to consider

Since SCP uses SSH encryption, you will need the ssh password for the file transfer to take place. Also, you need to have read permission on the machine you are about to copy from and write privilege on the machine(s) you will copy to.

For authentication and connection setup, you will need to generate an ssh key pair in the terminal using the following command:

ssh-keygen -t rsa

Copy this remote system key using:

ssh-copy-id user@remote_machine

Once you’ve authenticated on the remote machine(s), the public key will be copied and you’re ready to start transfers.

If you don’t remember the root password(s) for each system, you can have the ssh client select the file from which the private identity key is automatically read for RSA confirmation.

For protocol version 2, the default host key identity path is ~/.ssh/id_dsawhile for version 1 of the protocol it is ~/.ssh/id_rsa. Next, you need to find out where the backup private and public keys are stored so you can use the ssh command to use them automatically.

See also  15 VPS Security Tips to Avoid Attacks on Your Server

for the route /backup/home/jack/.sshthe command is as follows:

ssh -i /back-up/home/user/.ssh/id_dsa user@yourserver.servername.domain

Pro tip: this option has the default value –overwrite so unless you specify the option –overwrite No either –overwrite ask in the scp command, the operation will overwrite files that have identical names and locations without any warning.

If you are transferring large files, we recommend using a tmux session or running the command from a different screen. In addition, you should also use the -v option for large transfers. This will force scp to show any debug connection, authentication, or configuration issues.

Copying files with the SCP command

The best part about SCP on Linux is that it gives you the ability to transfer files between two hosts or between a host and a local machine. Let’s see how the command should be used for each type of transfer.

Local file to a remote location

We will copy a local file scp.zipto the user on the remote machine called root. The username is followed by the IP address of the server.

Example:

scp /users/Edward/desktop/scp.zip root@191.162.0.2:/writing/article

If you don’t have an ssh client autocommit configured, you’ll be prompted to enter the remote machine’s user password, and you’ll see a progress meter. It would look something like this:

root@191.162.0.2’s password: novel3.zip 100% 0 0.0KB/s 00:00

But let’s say the remote machine is configured to listen for SSH connections on a port other than the default 22. In this case, you must specify that port using an option.

scp -P 2322 /users/Edward/desktop/scp.zip root@191.162.0.2:/writing/article

If you also want to rename the file during the transfer operation, your command will look like this (if your port is not the default, just add –P and the port number):

scp /users/Edward/desktop/scp.zip root@191.162.0.2:/writing/article/howtoscp.zip

If you want to copy a directory that has more files and/or subdirectories, use the command -r that we described above

scp -r /users/Edward/desktop root@191.162.0.2:/writing/article

See also  Minecraft Hosting | Root Access And Quick Settings

Remote file to local machine

In this process, the source and destination of the command are reversed, so this must be reflected in the syntax. This time, we are trying to copy scp.zip from the same remote host to our local machine:

scp root@191.162.0.2:/writing/articles/SCP.zip Users/Edward/Desktop

Again, this should prompt the same SSH login output where you must enter the password, unless authentication is disabled by sudo privileges, or you have forced the ssh client to use a private key on your machine.

Remote file to another remote location

To copy files from one remote host to another, you’ll need to enter the passwords for both accounts after running this command in your terminal.

Example:

scp root@191.162.0.2:/writing/article/scp.zip edward@11.10.0.1:/publishing

The above command copies the source file /writing/article/scp.zip from the first host to the second. To copy folders, just add the option –r and specify the folder path instead of the file inside it, as we did before.

Under normal circumstances, the file goes directly from the first remote host to the second. However, if you want to redirect the operation through your machine, you can add the option -3:

scp -3 root@191.162.0.2:/writing/article/scp.zip edward@11.10.0.1:/publishing

That is all!

conclusion

In this article we have learned how to easily transfer files between remote and local hosts with the Linux SCP command. This is extremely useful when working with multiple servers. Secure Copy Protocol (SCP) makes it easy to successfully copy information from one remote host to another without having to log in.

In addition, this method of remote file transfer encrypts your data with secure shell (SSH), which will guarantee the confidentiality of the information that is transmitted.

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...