Easy Way To Copy Files Over SSH

SSH is a protocol that can help you manage a remote computer, as well as let you upload and download files. You can also use SSH keys instead of a password!
Copying files requires the secure copy command, which is actually pretty simple:

scp [options] [original file] [destination_file]

The syntax for the [destination file]  portion is usually where users have the most difficulty. When you are addressing a remote file you should use this format:

user@server:path/to/file

The server, in this case, can either be an IP address or a URL. After the IP/URL address you'll just write the standard file path after a colon.. Here’s an example:

scp –P 40050 Desktop/filename.txt nms@192.168.1.50:~/Desktop/filename.txt

We used the -P flag here which let’s you specify the port you want to use. Otherwise, it'll just default to port 22. Note that this flag is case sensitive and should be uppercase.  Depending on how your system is configured you might not need to use this flag.
Then I just used the original file, which is “filename.txt,” and it is located in the “Desktop” directory. The destination file is located in “~/Desktop/filename.txt”. The command has been executed by the user “nms” on computer “192.168.1.50”.
What if you need to do the reverse operation and copy a file from a remote server to your local machine? You would then switch the source and destination within the same command.


Here’s an example:

scp –P 40050 nms@192.168.1.20:~/Desktop/filename.txt Desktop/filename.txt

I will have copied a file from the folder” ~/Desktop/” of the remote computer to the “Desktop” folder on my computer.
If you want to copy a whole directory you will have to use the -r flag, in lowercase. The command would then look like this:

scp -r –P 40050 nms@192.168.1.20:~/Desktop/filename.txt Desktop/filename.txt

You can actually “combine” flags. Instead of using:

scp –P –r …

You can use:

scp –Pr …

SSH Without Passwords

Secure copy is great as you can put it in scripts and let it perform backups to remote computers. However, you won’t always be around to provide the password. Being asked to enter a password every time you run the script can get tiresome.Thankfully, you can use key files to get around this problem. You can generate two key files; a private one for your computer and a public one for the remote computer. You’ll then be able to use these instead of a password

First use the following command on your computer:

ssh-keygen –t rsa

The command will generate two keys which should be kept in:

~/.ssh/

with the name “id_rsa.pub” for the public key and the name “id_rsa” for the private key.
You’ll be asked about where to save the keys. If you want to use the defaults, just hit Enter and you will also be asked to type a passphrase. You can hit the Enter key again to leave it blank.
You should then copy the file with the public key to the remote computer. Now the “scp” command can be used to put the keys in place, like this:

scp -r –P 40050 .ssh/id_rsa.pub nms@192.168.1.20:~/.ssh/authorized_keys2

The destination of the key on the remote server will be located in:

~/.ssh/authorized_keys2

And you’re done!
You’ve learned how to use the scp command by itself, as well as generating key files, and using scp with key key files to copy data without using a password. Secure copy over SSH is very flexible. You can use the command for small operations or in conjunction with server control panels for security and flexibility in bulk operations.

February 24 2016

Add or review comments

Please leave your comment

Existing comments

Comments 2


David Pickett
I always liked something more like: $ find ... -type f | cpio ... | gzip -9 | ssh user@system 'gunzip | cpio ...' It allows you to select your own favorite compression and level, distributes the load over many cores/CPUs, relieves ssh of secondary tasks so it just does d/encryption. If directory creation is not an issue, it can be parallelized by tools like GNU parallel. Even then, you can do directory creation first, as it is not CPU or net bandwidth sensitive/intensive. Fun for all those cores! Options for cpio can be set for less compatible hosts, update only if older.
David Pickett
PS: When bandwidth is low, slower but more intense compressors like bzip2 may be worthwhile to minimize that hit. The slower speed can be compensated by more parallelism. (Leave some bw for others!) At very least, consider scp with high compression, possibly in n parallel sessions, to speed things and spare some bw for others. Each scp could be give m file names, for instance. This spares the cost of SSH session setup.
Alex
I presonally like the rsync option over ssh more... since it can continue where it left off when the pipe breaks or the network stalls. Especially useful for large collections of files and directories preserving rights... Works almost the same as scp but can also handle updates. <code> rsync -rvah --progress [localDir[ [user]@[remote host]:[remote path] </code>

Get notified about new publications and product updates.
Please note we do not share information to anyone.