About scp...

scp (Secure CoPy) is a secure and network-aware version of the UNIX rcp remote copy command and allows files to be transferred between different computers via an encrypted end-to-end link. Making use of the infrastructure provided by ssh, it is considerably more secure than both rcp and the notoriously insecure ftp.

All modern Linux, UNIX and Mac computers have an scp client installed as part of the basic operating system and a number of file transfer packages for Windows also support scp including modern versions of FileZilla and the popular WinSCP program.

Basic usage - copying a file to a remote system

Assuming you are already logged into a computer and you want to copy a file from your current directory to your own home directory on another computer, where you have the same username; typing
scp filename host:

where filename is the file you want to copy and host is the name of the remote host you want to copy it to will achieve this. If your username on the remote system is different from your username on the system you are currently using, the command:

scp filename username@host:

where username is your username on the remote host will allow you to copy the file to the remote system.

If you want to copy the file to a directory other than your home directory on the remote system, you can add a path after the ':' colon that follows the hostname, like this:

scp filename username@host:/path/to/other/destination/directory

and you can even change the name of the file when it reaches the destination if you give a new filename after the colon or at the end of the path as in these two examples:

scp old-filename username@host:new-filename
scp old-filename username@host:/path/to/other/destination/directory/new-filename

the first example renames the remote copy in your home directory on the remote system while the second example is used for when you want to copy the file somewhere other than your home directory and rename it at the same time.

The examples shown so far assume that you are currently in the same directory as the file you want to transfer but you can transfer files from other directries by adding the path in front of the filename. You can also select multiple files and, in fact, use all of the standard shell wildcards - * ? [abcde] [A-D] [!A-D] {word1,word2} - just as if you were copying files on the local machine.

Copying a file from a remote system to the local system

Instead of copying files from the computer you are presently logged into to a remote system, you can do things the other way round, copying from the remote system to your local machine using a slightly different syntax that puts the remote hostname and filename in front of the destination; the command:

scp host:filename .

will copy filename to your current directory on your local system; the '.' dot means, quite literally, 'here', the present directory you are in. So if you are in your home directory on the local machine and you give the command above, then the remote file is transferred to your local home directory. But if you change to another directory beforehand, such as /tmp, and then give this command, the file will be transferred to /tmp instead.

Instead of using a dot, you can of course use paths as already described above, both on the remote system and on the local destination system, to find the file at the remote end and/or put the copy exactly where you want it to go.

Advanced usage

More advanced uses include copying files between two remote systems without actually having to log into either of them. The basic syntax is:

scp user1@host1:filename user2@host2:

where user1@host1 is the source from which you want to copy filename from and user2@host2 is the destination where you want the copy to go to. You will be prompted first for the password for user1 on host1 and then a second time for user2 on host2. Here are some examples:

scp host1:myfile host2:

is the simplest possible form of this command where your username is identical on both systems and you are simply copying from your home directory on one system to the other. All of the variations described above for specifying differents usernames and source & destination paths can be used here too and the command can then become quite complex!

Copying files without using a password

As scp uses the SSH (Secure SHell) transport system to make the connection between the two systems involved in a file copy, most of the features of SSH are available to scp too. This includes the ability to make connections without having to type in a password as long as you have a private key in your home directory on the computer you are copying a file from and the matching public key in your home directory on the destination machine. This is described in detail in the SSH pages.

Do remember that like any other data copy or move operations, you will need the appropriate read and write permissions to move files around in this way. As a normal user, attempts to do something like 'scp myfile jbloggs@yourcomputer:/etc' will fail as only the root user on the remote system will have write access to the /etc directory there. And attempts to read and copy files such as /etc/shadow will fail at either end (unless you are the root user) since these are inaccessible to most users for security reasons.

Andy Thomas

unix/Research Computing Manager,
Department of Mathematics

last updated: 29.04.2020