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.