Secure copy (SCP) is a command-line tool allowing you to copy files or folders between two hosts using the SSH protocol, meaning that everything you move using this command and the passwords you type in is going to be encrypted. Using this tool, you are going to be able to move files between your local machine and a remote server, between a remote server and your local machine, and between two remote servers from your local machine.
The examples in this article aim to cover all three situations, and hopefully, teach you how to make the most out of them.
Note that we are going to discuss the SCP command between two Unix-based servers. Unfortunately, Windows does not natively support this command, and to use it, you need to install special software.
Copying files from your local computer to a remote server.
Syntax of the command is as follows:
scp -P <port> <file> <user@remotehost/IP>:/folder/on/remote/host>
The "-P" flag is optional and should only be used if the server's SSH service is listening on a different port other than the default 22. Upon typing this command, a password prompt is going to appear, requesting the remote server's password. Once submitted, the transfer process is going to start. The command line is going to display the progress with a percentile representation underneath the password prompt.
user@remotehost's password:
file 53% 0 5M/s 00:30
To move a directory, please add the "-r" flag to the command:
scp -r /local/computer/folder user@remotehost:/remotehost/directory
Copying files from a remote server to your local computer.
You can refer to this process as a "reverse scp" as it is going to copy the file from the remote server to you, instead of copying it from you, to the remote server.
Here is an example:
scp user@remotehost:/remotefolder/file /local/computer/folder
The process is pretty similar. A password prompt is going to appear, asking you to type in the remote user's password, and the transfer is going to start shortly after. Add the "-P" flag if the remote host has a custom SSH service port.
Copy files between two remote hosts from your local machine
To copy a file between two remote hosts, all you need are both of the passwords for each server and, of course - the SSH server port number. Here is what the command is going to look like:
scp remoteuser1@remotehost1:/remotedir/file remoteuser2@remotehost2:/remotedir/
Similarly, if there is a custom port on one of the servers, add the "-P" flag and enter the port. The transfer process is going to start if you submit the passwords for both remoteuser1 and remoteuser2 ONLY. Please add the "-r" flag if you want to transfer a folder.