Securely copy files to/from a remote server
If you are a system administrator or a dev -op guy or any guy working or learning on Computer Science, at least per once in your life you may have encounter a situation where you want to transfer or copy some file (s) from a known remote server to your computer and there is a high probability that the server having the resource you need may not have a GUI (Graphical User Interface).
In these cases you can save your day with the scp
command which comes by default in new linux based operating systems. If you are on Windows please follow these instructions in setting up WinSCP
.
SCP stands for Secure Copy Protocol
and this command is heavily used on maintaining linux servers where GUI is not installed (headless servers). The basic syntax of the command looks like below,
scp userName@destination_host:locationOfTheSource locationOfTheDestination
an example for above would be like
scp ubuntu@192.168.2.28:/home/admin/Documents/sample1.txt ~/Downloads/
in above the sample1.txt
file which exists in /home/admin/Documents/
directory of the remote server (machine) at 192.168.2.28
will be get copied to the ~/Downloads/
directory my local machine.
The same can be done in opposite direction incase we need to copy something from our machine to the remote server(machine)
scp sourceFileLocation userName@destination_host:locationInTheDestination
an example for above would be like
scp ~/Downloads/sample2.txt ubuntu@192.168.2.28:/home/admin/Documents/
in above the sample2.txt
file which exists in my ~/Downloads/
directory will be get copied to the /home/admin/Documents/
directory of the remote server (machine) at 192.168.2.28
.
There are so many parameters in the scp
command that you can use for various use cases, listed below are the most commonly used parameters. Please refer the scp man page for more information on all the parameters available.
- -C
For transferring files quickly over the network.
This parameter will compress the content you sent over the network but at the destination the content be in same size as before the compression. Sample command will be as follows,
scp -C ~/Downloads/sample.zip ubuntu@192.168.2.28:/home/admin/Documents/
run two samples like above one with -C
and other without -C
, you could observe that command with -C
completed the transfer very quickly.
But please do note that the above parameter will not have any effect on sending already compressed files like zip, rar,iso and etc.
- -r
For recursively copying.
This parameter can be used for recursively copying files inside a given directory as follows,
scp -r ~/Downloads/pictures/ ubuntu@192.168.2.28:/home/admin/Pictures/
- -c
For changing the encryption used.
This parameter can be used to change the default AES-128
encryption used by thescp
. Following is an example of using the 3des
algorithm for encryption.
scp -c 3des ~/Downloads/sample2.txt ubuntu@192.168.2.28:/home/admin/Documents/
- -v
For verbose mode.
This parameter can be used to enable debug logs in scp
. This can help you in finding connection, authentication and other related errors. Sample command will look like as below.
scp -v ~/Downloads/sample.zip ubuntu@192.168.2.28:/home/admin/Documents/
Above are the most used parameters ofscp
, please visit scp man page for more details and other arguments.