The scp (secure copy protocol) is used to copy files between local to remote or remote to remote machines.
It uses SSH to ensure the files are being transferred securely so make sure you have all the credentials to use the SSH like username, IP, and password.
And in this guide, I will walk you through the following:
- Transfer files from the local machine to the remote machine
- Transfer files from the remote machine to the local machine
- Transfer files between the remote machines
So let's start with the first one.
How to transfer files to remote servers using the scp command
First, let's have a look at the syntax:
scp [options] [source] [destination]
[options]: is used to tweak the default behavior of the command such as you can add the
-roption and it will copy every file in the selected directory.
[source]: is where you'd have to specify the file that you want to copy. And if you are using files from the remote source, you'd have to mention the username and IP address (eg.
[destination]: is the destination where you want to paste the copied files. And if you want to paste files in the remote directory, you'll have to mention the username and IP/domain of the host (
Now, let's have a look at some examples of how you can use the scp command.
1. Copy files and directories from the local machine to the remote machine
Let's start with the files.
To copy files from your local system to the remote machine, you'd have to follow the given command syntax:
scp file.txt user@remoteIP:/path/to/destination/
For example, here, I wanted to send a text file named
Hello.txt inside the
Docs directory of my remote server, so I used the following command:
scp Hello.txt [email protected]:~/Docs/
Now, let's have a look at how you can copy-paste directories.
Copying directories to the remote server
The syntax will remain the same, but this time, you'll have to use the
-r flag which will copy files recursively as shown:
scp -r my_directory/ user@remoteIP:/path/to/destination/
For example, here, I copied the
Tutorial directory to the remote server:
scp -r Tutorial [email protected]:~/Images/
On the right window, I'm logged in to my remote server using the SSH and as you can see, it copied the entire directory as intended!
What if you are using SSH on a different port?
If you've changed the port for SSH and it is listening to any other port, you'd have to specify the port number using the
scp -P [port-no] File user@remoteIP:/path/to/destination/
2. How to copy files from the remote to the local system
To copy files from the remote directory to the local system, all you have to do is execute the scp command in the following syntax:
scp user@remote:/path/to/remote/file /path/to/local/destination/
For your reference, here, I have copied one text file
LU.txt from the remote machine to the local:
scp [email protected]:~/LU.txt /home/sagar/Tutorial
3. How to copy files from one remote machine to another
And enabling password-less login in SSH worked for me.
If you're a system admin, you must be dealing with multiple systems on a daily basis, and in that case, this will work as a blessing.
To do so, you'd have to use the scp command in the following manner:
scp user1@remote1:/path/to/file user2@remote2:/path/to/destination/
Here, I have copied
Hello.txt from one remote server to another one:
And there you have it!
Want to harden the SSH? Here are a few tips
By design, SSH is a pretty secure protocol, but what if I tell you that you can make it even more secure?
Yes, and here's the detailed guide for that:
I hope you will find this guide helpful.
And if you have any queries, feel free to ask in the comments.