Thursday, February 25, 2010

Copy files over a network in Linux via scp command

SCP which stands for Secure Copy Protocol is a command line tool that can be used to connect to a remote host and copy files between two hosts. it uses ssh for the file transfer. Here I'm going  to show you how to do this.

Before doing the file transfer you need to have the ssh installed.
sudo apt-get install ssh
After that reboot the machine or enter the following command.
/etc/init.d/ssh restart
The command used to copy files is,
scp [options] source-file [user@to-host-ip:]
Some of the [options] that may be used frequently include,
-r :Recursively copies entire directories. Use this if you are copying directories
-l : Limits the used bandwidth, specified in Kbit/s

'user' is a user in the remote host, and 'to-host-ip' is its IP address. Type command ifconfig on remote host to get to know the IP address of the it.

Full command according to the manual(man scp) is,
scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2
For example, to copy a directory named scpTest/ from your machine to a remote host(user-name:kasun, ip:, Go to the Directory which contains the scpTest/.
scp -r scpTest/ kasun@
(Note the colon : at the end.) It will ask for the user password. Provide it and the progress meter appears showing the progress of the file copying. Files/Folders will be copied to the home folder.

That's it! Just ask/comment if you got any problem.