Secure copy (SCP) Unix Networking Command
There are times, when sitting on your Linux box, working on a command line terminal, you suddenly feel a need of copying a file to/from a remote host. At this point you desire a command line utility that could do this task for you in just one line. Well, this is where 'scp' saves your day :-)
This article makes an attempt to describe the 'scp' command on Linux.
- 'scp' stands for secure copy and is used to copy a file to/from a remote server.
- This utility provides encryption.
- This command comes along with the 'ssh' package.ie, you need to have ssh on your linux box in order to use scp.
- To install ssh package on your ubuntu or ubuntu-like system, run the following command :
sudo apt-get install ssh
- 'scp' is better than 'rcp' because 'scp' uses encryption.
- 'scp' depends upon authentication. So, one needs to provide the username and password of the linux/unix box to/from where the file is being copied.
Following is the syntax of a basic 'scp' command :
scp [[user-name@]source-host:]source-file-path [[user-name@]dest-host:][dest-file-path]
- Since the scp command provides authentication. So, 'user-name' is the user login ID which has the rights to access the file at that particular path
- source-host/dest-host is the host name or IP address of the host from/to where one wants to copy the file/Directory.
- One can omit the '[[user-name@]source-host:]' if the source host is the host from where the scp command is being issued.
- source-file-path or dest-file-path is the complete path from/to where in the source/dest host the file is desired to be copied.
Some basic options used with scp command:
Preserves the modification and access times, as well as the permissions of the source-file in the destination-file
Do not display the progress bar
Recursive, so it copies the contents of the source-file (directory in this case) recursively
Displays debugging messages
Following is the syntax of Linux scp command to send file or directory to a remote computer:
scp -r [/<path>/filename] [user-login-name@ip address] : .
Following is the syntax of Linux scp command to retrieve file or directory from a remote computer:
scp -r [user-login-name@ip address] : [/<path>/filename] .
Following is the syntax of Linux scp command to copy file or directory from a remote host to a remote destination computer:
scp -r [user-login-name@src ip address] : [/<path>/filename] [user-login-name@dest ip address] : [/<path>/filename]
In the above commands :
- The option -r stands for recursively copying( Like in case of a directory).
- '.' means current directory.
- <path> is the complete path where the file/directory resides.
- In command 1 : A file kept at /<path>/filename at the host from where the 'scp' command is being typed is being copied to a remote host '[user-login-name@ip address]' in its current directory '.'
- In Command 2 : A file at remote host '[user-login-name@ip address]' which is kept at path '/<path/filename' is being copied to the host from where 'scp' command is being issued. The destination path of the file is the current directory.
- In Command 3 : A file at a remote host '[user-login-name@src ip address]' which is kept at '/<path>/filename' is being copied to the destination host '[user-login-name@dest ip address]' at a path '/<path>/filename'
Lets look at a real time example. Since I do not have a LAN setup, so I will use source/destination as my machine only :
sudo scp /home/himanshu/practice/main.c /home/himanshu/Desktop/main.c
The above command copies main.c from '/home/himanshu/practice' to '/home/himanshu/Desktop'.
I know it seems more like a cp command here but the copy operation that took place here was encrypted. Also, if you are on LAN or if you have access to a remote server then scp command comes in real handy.
Some points to remember
- Multiple names or wild-card character '*' can be used to copy more than one files in one go to a destination computer. For example :
$ scp file1.txt file2.txt <user-name>@<remote-IP>:.
$ scp *.txt <user-name>@<remote-IP>:.
- One can use 'putty' to login from windows OS to Linux OS and then run the scp command from putty shell to copy file from Linux box to Windows box.
- To remove scp from your ubuntu or ubuntu-like box, run the following command :
We removed ssh package from system in order to remove scp because it was with ssh package through which we installed scp as it comes along bundled with
sudo apt-get autoremove ssh
- Some times people face problems where in scp says that data has been successfully transferred but actually the file/directory has not been copied to the
destination. I have never encountered this problem but from others experiences, this problem may occur due to the the 'echo' commands in the .bashrc file
being executed at the remote machine. The problem is well known but the root cause is yet to be pointed out. :-)
To conclude, scp command is useful utility for remote copying of files or directories just like ftp. It uses ssh and provides encryption and authentication for data transfer.