SCP: Sending files between servers using SSH

SCP or Secure Copy is a useful program for sending files between two or more computers over a secure shell (SSH) connection. In this post, I’ll show you how to use SCP to send files from your local computer to a remote, copy files from a remote computer to your local or copy files between two servers.

An SCP command has the following basic structure:

scp [OPTION] [user@src_host:] file1 [user@dest_host"] file2

Where OPTION refers to the optional flags you can pass to scp such as the following:

  • -P remote host SSH port
  • -p preserve file modification and access times
  • -q quiet
  • -C compress
  • -r copy directories recursively

The colon (:) that comes after the host IP address or domain name is important.

Sending a file from the local computer to a remote computer

scp file.txt remote_username@host1.com:/path/to/remote/dir

Copy a file from the remote computer to the local computer

scp remote_username@host1.com:/path/to/file.txt /local/directory

Copy files between TWO remote computers

scp user1@host1.com:/path/to/file.txt user2@host2.com:/path/to/file/location

Secure copy to an EC2 instance without password

scp -i mykey.pem somefile.txt root@my.ec2.id.amazonaws.com:/