Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am lazy at home and use password authentication for my home machines. I am ready to move to key based authentication. There are many options on the web on how to do this, including catting then sshing the key over, scping the key over directly, etc.

I am looking for the easiest and recommended way to copy a key over, hopefully there is a convenience wrapper somewhere in the Ubuntu ssh package?

I'm already aware on how to shut off password logins.

share|improve this question

6 Answers 6

up vote 107 down vote accepted

The ssh-copy-id command (in the openssh-client package and installed by default) does exactly this:

ssh-copy-id user@hostname.example.com

copies the public key of your default identity (use -i identity_file for other identities) to the remote host.

The default identity is your "standard" ssh key. It consists of two files (public and private key) in your ~/.ssh directory, normally named identity, id_rsa or id_dsa (and the same with .pub), depending on the type of key. If you did not create more than one ssh key, you do not have to worry about specifying the identity, ssh-copy-id will just pick it automatically.

share|improve this answer
What is the default identity? –  Oxwivi Jun 4 '11 at 18:28
@Oxwivi: The default identity is your "standard" ssh key. It consists of two files (public and private key) in your ~/.ssh directory, normally named ``identity, id_rsa` or id_dsa (and the same with .pub), depending on the type of key. If you did not create more than one ssh key, you do not have to worry about specifying the file, ssh-copy-id will just pick it automatically. –  Marcel Stimberg Jun 6 '11 at 13:56
for different port use this: ssh-copy-id "user@host -p 6842" –  jibon57 Jan 25 '14 at 20:27
What if the remote server you're copying to doesn't allow password prompts and is basically locked down save for SSH access? –  Cyle Jan 29 '14 at 23:13
On mac you can do brew install ssh-copy-id and then run the command. –  Avishai Sep 30 '14 at 14:31

I like the answer from Marcel. I did not know this command. I've always been using what I had found on a SUN web site:

cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> .ssh/authorized_keys && echo "Key copied"'

I thought to post it here still, because it is a good illustration of what can be achieved in shell code with the power of ssh. But using the ssh-copy-id is definitively a safer way to do it properly!

Note that if the folder .ssh does not already exist, the above command will fail. In addition, it might be better when creating the file to set a minimum possible permission (basically read-write for owner only). Here is a more advanced command:

cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'umask 0077; mkdir -p .ssh; cat >> .ssh/authorized_keys && echo "Key copied"'
share|improve this answer
This exact commands also work from a Mac too –  Mihai P. Feb 20 at 4:09
It should work under any Unix with most shell. I've update the post with an updated command in case the folder .ssh does not exist on the remote side. –  Huygens Mar 1 at 14:54

Graphical method

  1. Open ApplicationsPasswords and KeysMy Personal Keys.
  2. Select your key and then click RemoteConfigure Key for Secure Shell.

Set Up Computer for SSH Connection

share|improve this answer
I don't have "Passwords and Encryption Keys" in my menu. –  Jorge Castro Sep 27 '10 at 20:33
What Ubuntu release are you using? In Ubuntu 10.04 LTS this should be available by default. –  ændrük Sep 27 '10 at 20:46
10.10, just added the tag. Marcel's answer is what I'm looking for, though +1 for your desktop oriented answer! –  Jorge Castro Sep 27 '10 at 21:04
This program is seahorse. –  Gilles Sep 27 '10 at 21:12
I think this application is in the System->Preferences menu in 10.10 (can't check it right now, I'm on 10.04) –  Huygens Oct 11 '10 at 22:07

In Ubuntu Maverick and higher, there is another method using the ssh-import-id program.


  1. You need a Launchpad account so login or create an account
  2. After logging in, click the button next to SSH keys:
  3. Paste the contents of your public key file in that field (including comment). Such a key looks like:

    ssh-rsa AAAAB3Nza .... UyDOFDqJp lekensteyn

    Here, ssh-rsa indicates that the key is a RSA key, AAAAB3Nza .... UyDOFDqJp is the actual key and lekensteyn is the comment.

  4. Save the key by pressing Import Public Key
  5. If everything went well, your key should now be listed under SSH keys:

The package ssh-import-id needs to be installed on the machine which needs to be accessed from remote. This package is installed together with the openssh-server package as it's a recommended package for openssh-server. After making sure that ssh-import-id has been installed On the client machine, run:

ssh-import-id [launchpad account name]

This will download the public key from the Launchpad servers over HTTPS which protects you from MITM attacks.

On Ubuntu Lucid and before, you can accomplish the same with:

wget https://launchpad.net/~[lp acount name]/+sshkeys -O - >> ~/.ssh/authorized_keys && echo >> ~/.ssh/authorized_keys

The echo command is needed to get an extra newline after the line with the SSH key.

share|improve this answer

for custom port

ssh-copy-id -i "user@hostname.example.com -p2222"

-i switch defaults to ~/.ssh/id_rsa.pub, if you want another key, put the path of the key after -i

WARNING: If you did not write the -i it will copy all your keys found in ~/.ssh

share|improve this answer

You can use secure copy, too:

scp -p yourFile theRemotehost:/path/to/folder
share|improve this answer
That only works if you have a single key. Otherwise you need to append the new key to the authorized_keys file. –  Egil Apr 14 '11 at 12:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.