March 29, 2019
March 29, 2019
Transferring files securely is paramount, when using a VPS. Luckily it can be done easily by using the SCP command. In this article, you’ll learn how to learn how to use SCP commands, and how they can work for you.
What are SCP commands precisely? Based on the Berkeley Software Distribution (BSD) Remote Copy Protocol (RCP), SCP (Secure Copy) is a network file transfer protocol that allows easy and secure file transfers between a remote and a local host, or two remote locations.
Full-stack developers frequently use an SCP command for its authentication and encryption features without requiring third-party hosting services like Github. It is an easy way to prevent exposing your data to packet sniffers, as well as preserve its confidentiality.
In essence, SCP is a mixture of RCP and SSH (Secure Shell). It relies on the first to perform copy operations, and the second to encrypt the information and authenticate remote systems.
Unlike Rsync, everything that you need to use the SCP command line successfully is a username and password or passphrase for the systems involved in the transfer. It streamlines the process since you don’t need to log into either of them.
The basic SCP command example looks like this:
scp [other options] [source username@IP]:/[directory and file name] [destination username@IP]:/[destination directory]
It might seem complicated at first, but we’ll happily break it down!
In this example we’re performing a transfer between two VPS servers.
In a real world scenario it would look like this:
scp -p email@example.com:/media/scp.png firstname.lastname@example.org:/desktop/destination
See, it’s pretty simple! If you’re copying to, or from a local machine, you won’t need the IP address, the destination or source path like /desktop/folder_name.
Let’s talk about other options you can use to modify the SCP command. There are 20 common ones that you can use both in single character form (-o), as well as their descriptive equivalent (–option). Commonly used options are:
Since SCP uses SSH encryption, you will need the ssh password for the file transfer to happen. Moreover, it is necessary to have read permission on the machine you’re about to copy from and write privileges on the machine(s) you’ll copy to.
For the connection authentication and configuration, you will have to generate an ssh key pair in the terminal by using the following command:
ssh-keygen -t rsa
You copy this key to the remote system using:
Once you’ve authenticated on the remote machine(s), the public key will be copied and you’re ready to start the transfers.
If you do not remember the root password(s) for either system, you can have the ssh client select the file from which the private identity key for RSA confirmation is read automatically.
For protocol version 2, the default identity path of the host key is ~/.ssh/id_dsa, while for protocol version 1 it is ~/.ssh/id_rsa. Then, you must find out where the back-up for the private and public keys are stored so you can employ the ssh command to automatically use them.
For the /back-up/home/jack/.ssh path the command is as follows:
ssh -i /back-up/home/user/.ssh/id_dsa email@example.com
Pro tip: this option has the default value –overwrite [yes], so unless you specify the –overwrite no or –overwrite ask option in your scp command, the operation will overwrite those files that have identical names and locations without any warning.
If you’re transferring large files we recommend using a tmux session or running the command inside a different screen. Moreover, you should also use the option –v for big transfers. It will force scp to display any debugging connections, authentication, or configuration problems.
The best part about SCP is that it gives you the possibility 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.
We’ll copy a local file scp.zip, to the user of the remote machine called root. The username is followed by the server’s IP address.
scp /users/Edward/desktop/scp.zip firstname.lastname@example.org:/writing/article
If you do not have an automatic ssh client confirmation set up, you will be prompted to enter the password from the remote machine’s user and will see a progress meter. It would look something like this:
email@example.com’s password: novel3.zip 100% 0 0.0KB/s 00:00
But let’s say that the remote machine is set 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 firstname.lastname@example.org:/writing/article
If you also want to change the name of the file during the transfer operation, then your command will look like this (if your port is not the default one, just add –P and the port number):
scp /users/Edward/desktop/scp.zip email@example.com:/writing/article/howtoscp.zip
If you want to copy a directory that has more files and/or subdirectories, use the –r command line we described earlier
scp -r /users/Edward/desktop firstname.lastname@example.org:/writing/article
In this process, the source and target of the command are reversed, so this must be reflected in your syntax. This time, we are trying to copy scp.zip from the same remote host to our local machine:
scp email@example.com:/writing/articles/SCP.zip Users/Edward/Desktop
Again, this should prompt the same SSH login output where you have to input the password unless authentication was disabled by sudo privileges, or you forced the ssh client to use a private key on your machine.
In order to copy files from one remote host to another, you will have to enter the passwords for both accounts after you run this command in your terminal.
scp firstname.lastname@example.org:/writing/article/scp.zip email@example.com:/publishing
The command above copies the source file /writing/article/scp.zip from the first host to the second one. To copy folders, just add the –r option and specify the folder path instead of the file inside of it, like we did before.
Under normal circumstances, the file goes straight from the first remote host to the second. However, if you want to reroute the operation through your machine, you can add the -3 option:
scp -3 firstname.lastname@example.org:/writing/article/scp.zip email@example.com:/publishing
That’s it, all there is to it!
In this article, we’ve learned how to easily transfer files between remote and local hosts with the scp command. This is extremely useful when you’re working with multiple servers. The secure copy protocol makes it easier to successfully copy information from one remote host to another without having to log in.
Furthermore, this method of remote file transfer encrypts your data with secure shell, which will ensure the confidentiality of the information that is being transmitted.