Using SCP to transfer files to and from remote server

Everytime I need to transfer files between my ubuntu server to my local machine I have forgotten how to use the scp command. Therefore I needed to write it down so I can remember it for the next time I need to transfer files to the remote server or to my local machine.

A bit basics of the SCP command

Secure copy protocol or SCP is a means of securely transferring computer files between a local host and a remote host or between two remote hosts. It is based on the Secure Shell (SSH) protocol. "SCP" commonly refers to both the Secure Copy Protocol and the program itself. Wikipedia

There are a ton of options that can be added to the SCP command. I will try to explain the most useful (to me at least).

  • -r: This will transfer recursively. So if it is a direktory, it will transfer the whole directory with all the content.
  • -p: This is the way you can specify a different port number. By default the default for SCP (SSH) is 22.
  • -i: This is where you specify the private key. If you do not use public/private keys, drop this option and the command will prompt you for the password for the given user.

Transfering from remote to local

scp -r -i /Users/username/.ssh/privatekey_rsa /Users/username/Projects/

This command will login as remoteusername using the privte key /Users/username/.ssh/privatekey_rsa. Then it will transfer the folder at the remote location /var/www/ into the local direktory /Users/username/Projects/

Transfering from local to remote

scp -r -i /Users/username/.ssh/privatekey_rsa /Users/username/Projects/

This command will do the same as transfering from remote to local, but the other way.

Teis Lindemark

Read more posts by this author.