Now these days, playing with remote servers is one of the great skill which is necessary for all kind of developers especially full-stack ones and while with them, there is one problem which every person will face which is Sharing data from your local system to remote server. For instance, It can be some already created or downloaded scripts that you may want to execute on remote server. The one option is to push your scripts on Github and then clone them over remote servers which works fine but there is an another tool which we can use much more efficiently and its totally secure. Yes, I am talking about SCP.
Lets go through its formal definition ?
The SCP ( Secure Copy Protocol ) is a network protocol, based on the BSD RCP protocol, which supports file transfers between hosts on a network.
SCP allows files to be copied to, from, or between different hosts. It uses SSH for data transfer and provides the same authentication and same level of security as SSH.
?? For more information about SCP. Click here
? SCP runs over TCP port 22 by default.
Prerequisites
- A VPS server.
? I am using temporary AWS?s EC2 Instance as my Remote Host which will be deleted shorty after publishing of this blog post. So, screenshot with ubuntu@<IP> is remote side terminal.
- We are starting with a file at Local host ( localToRemote.txt ) which we will transfer to Remote host & a file at Remote host ( remoteToLocal.txt ) which we will transfer from there to our Local host.
Left Side is the Local terminal and Right Side is Remote terminal.
- Make sure you must have added your SSH key on Remote host. Click here to know more about SSH based authentication.
?? The list
- From Local host to the Remote host
- From Remote host to Local host
- Multiple files from Local host to Remote host
- Directory from Local host to Remote host
- Directory from Remote host to Local host
?? Variables Used
- user : Username of the Remote host.
- remote_host : Address of the remote host. Either IP or Domain Name.
- file_path : Path of file on Local host
- remote_file_path : Path of file on Remote host
- remote_dir : Path of directory on Remote host
- local_dir : Path of directory on Local host
Before starting
Basic structure of SCP command is : ? scp <OPTIONS> <SOURCE_FILE_OR_DIR> <TARGET_FILE_OR_DIR>
and for paths which are on remote host we will add server address first. i.e <user>@<ip_or_domain_name>:<remote_dir> .
?? Let?s Begin
? File from Local host to the Remote host
scp <file_path> <user>@<remote_host>:<remote_dir>
Example of Local host to the Remote host
?? Here scp -i “key.pem” is because my remote server is on EC2. So, instead of simple SSH key based auth, EC2 uses PEM file for login. To know more about all this ? Visit this link. If you already have SSH key set-up, you can skip both these terms.
? File from Remote host to the Local host
scp <user>@<remote_host>:<remote_file_path> <local_dir>
Example of Remote host to the Local host ( both side are local terminals )
You Can share any file using these commands. i.e .zip , .tar, etc.
? Multiple files from Local host to the Remote host
scp <file_path> <file_path> <user>@<remote_host>:<remote_dir>
Example of sharingMultiple files from Local host to the Remote host
? Directory from Local host to the Remote host
scp -r <local_dir> <user>@<remote_host>:<remote_dir>
Example of sharing Directory from Local host to the Remote host
? Directory from Remote host to the Local host
scp -r <user>@<remote_host>:<remote_dir> <local_dir>
Example of Directory from Remote host to the Local host ( both sides are local terminals )
That?s it!!
Happy Coding !!
? Listening to ?Beerus Sama tea time ? 1 hour? and I am also going to have a cup of tea with snacks.
If you like this article, please give it some claps ? and share it! If you do not like this post or have any type of questions regarding anything that i mentioned in this post. Feel free to ask me. Just post an issue in my ?Ask Me Anything? by clicking here.
For more like this, follow me on Medium or Twitter. To ask a Question visit this link. More about me on my website.