Thursday, 20th November 2008
scp - secure copy (remote file copy program), copies files between hosts on a network. It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh. Any file name may contain a host and user specification to indicate that the file is to be copied to/from that host. Copies between two remote hosts are permitted.
scp was only created as a hack to provide secure file transfer between computers until the sftp protocol was finialised and implemented. Now we prefer that sftp is used since it is supported by most servers including our own and is generally more efficient and versatile when transfering files via the internet. However its still a useful tool to know in the cases sftp is not available.
General options (and most useful)
-p : perserve permissions, this attempts to maintain the same permissions on the original file
when coping to the new location.
-r : recursive copy entire directories.
-q : quiet mode, this basically just disables the progress meter.
-v : verbose mode, outputs debugging messages, useful for working out whats happening if the copy
doesn't work.
While there's a few additional options they are only really for advanced use of this program which is rarely required. You can type man scp to get a complete list of options and their uses at the console. But where you need more advanced usage its better to use either sftp or rsync.
Lets take our first senario, your at home on the PC, have linux running and are connected to a linux server
using ssh. There's a couple of files on your your machine under your default web folder that you want to copy
up to your web folder on the server. You've got all the permissions sorted out so you don't want to have to
redo them once the files are uploaded.
$ scp -pr ~/public_html/* username@server-address:~/public_html
For those of you who have access to the Computer Society servers and you don't have access to another linux machine to try this out the following command will demonstrate whats happening. Create a directory first called test under your home directory. Then use the command to copy the files. You may get some output asking if you are happy with the RSA fingerprint, this is just a identification id so that other hosts cannot trick you into thinking your accessing a differnet server.
$ mkdir ~/test
$ scp -pr ~/public_html/* username@localhost:~/test
If you need it you can also copy files from a remote server using the following format. The second line is the example for those using the compsoc server.
$ scp -pr username@server-address:~/public_html/* ~/public_html
$ scp -pr username@localhost:~/public_html/* ~/test