Tag Archives: ssh-copy-id

Ubuntu Terminal How to Copy a File to or from a Remote Machine using SCP Tips, Tricks and Tutorials 04 AUG 2010

SCP is a powerful utility that allows us to securely copy files between remote machines. What makes it even more powerful is the ability to add it to scripts and so for today’s quick tutorial I will walk you through how you can go about either copying files from or to a remote machine with the use of SCP.

Now normally when you copy files with SCP you will be prompted for the remote machine’s password in order to continue with the copy operation. However, obviously if we wish to script with SCP we will need to get around this restriction and so we take a quick dive into the of public key cryptography.

Basically what we are going to do now is generate a public/private key pair for your local machine which is going to run the SCP function and then hand out the public to the remote machine so that the two of them can communicate quite happily with one another.

To generate the key pair, enter:

ssh-keygen -t rsa

The machine will ask you to enter a file name in which to save the generated key – just hitting enter will force it to create the key file in its default spot. It will then ask for a passphrase, which you can happily skip by just pressing enter twice.

The system will then spit out its report and reveal to you the location where it saved the key file (which always ends .pub by the way)

You then need to put the generated key data into the ~/.ssh/authorized_keys file of the user which you will be ‘logging in’ as on the remote machine.

A simple way to do this is to use the ssh-copy-id function. Simply call:

ssh-copy-id -i ~/.ssh/id_rsa.pub username@host

Once done, you can test this by ssh’ing into the remote machine and checking the existence of the ~/.ssh/authorized_keys file.

Okay, and now we are ready for the actual SCP file copy bit!

To copy files to a remote machine, simply use:

scp -v /myfilestocopy username@host:/directorytosave

Alternatively, to pull down files from a remote machine, use:

scp -v username@host:/myfilestocopy /directorytosave

Note that the -v switch isn’t necessary – I just like seeing on the screen that something is actually happening!

And there you go, as simple as that. Nifty.

Related Link: http://www.howtogeek.com/

Ubuntu Terminal: How to ssh-copy-id When the SSH Port is not 21 CodeUnit 02 AUG 2010

For the most part, the default port for SSH access into a Linux machine is 22. However, many people change this default in the name of security, meaning that for most of our function usage, we simply need to use the -p port number switch when trying to access with that remote machine.

However, interestingly enough, the -p switch was never bundled with the useful ssh-copy-id function, meaning that should you try something like this:

ssh-copy-id -p221 -i ~/.ssh/id_rsa.pub username@host

you will get a reply back reading: Bad port ‘umask 077; test -d .ssh || mkdir .ssh ‘ cat >> .ssh/authorized_keys’.

Not exactly encouraging.

However, there is actually a simply way to fix this and use ssh-copy-id when interacting with a non-default port 22 machine. Simply enclose your port declaration together with the host name within quotation marks!

So the correct usage would now look like this:

ssh-copy-id -i ~/.ssh/id_rsa.pub ‘-p 221 username@host’

And damn it, it actually works! Nice.