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.
The
ssh-copy-id
command (in the openssh-client package and installed by default) does exactly this: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 namedidentity
,id_rsa
,id_dsa
,id_ecdsa
orid_ed25519
(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.In case you do not have an identity, you can generate one with the tool
ssh-keygen
.In addition, if the server uses a port different from the default one (
22
) you should use quotation marks in this way (source):I like the answer from Marcel. I did not know this command. I've always been using what I had found on the Oracle web site:
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 thessh-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:Graphical method
On Ubuntu you can fetch your keys from Launchpad:
Details:
Paste the contents of your public key file in that field (including comment). Such a key looks like:
Here,
ssh-rsa
indicates that the key is a RSA key,AAAAB3Nza .... UyDOFDqJp
is the actual key andlekensteyn
is the comment.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 theopenssh-server
package as it's a recommended package foropenssh-server
. After making sure thatssh-import-id
has been installed On the client machine, run: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:
The
echo
command is needed to get an extra newline after the line with the SSH key.for custom port
-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
Here is a less secure, but very simple solution(not recommended for servers):
Move
~/.ssh
to the new machine and runssh-add
. DONE!LONG ANSWER:
~/.ssh
to an USB drive, or to any other storage you like.~
aka/home/$USER
.ssh-add
, on the new machine done.ssh-copy-id
does exactly that. I am not sure why some of the other answers here add inaccurate information. The help shows the following:I just tried the following on Ubuntu 18.04 client with a CentOS 7.6 server and it worked like a charm. The example shows using a custom port of
2222
, and specifying a public key at~/.ssh/path-to-rsa.pub
Before running the command, I actually used the
-n
switch at the end to do a dry run which confirmed that the command will work as intended. Once I confirmed it I ran the command again as above, without the-n
switch.If you already have a host key like in the case of AWS EC2, then do this
Next time, simply do this: