Canagon.com - Website for $499, beautiful premade designs, high-performance hosting on all continents, dedicated support team. Learn more >
$ ssh -i ~/ec2.pem ubuntu@12.34.56.78
- First step is to generate Key Pair and PEM file.
- Next step is to upload certificate to your remote server in command line using SSH, first time with password.
- Last step, testing connection client to server without using a password.
1) How to generate a Key Pair for authentication without password
$ ssh-keygen -t rsa -b 2048 -v
Generating public/private rsa key pair.
Enter file in which to save the key (/home/anonymouse/.ssh/id_rsa):
hetznerEnter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in hetzner.
Your public key has been saved in hetzner.pub.
The key fingerprint is:
bb:c6:9c:ee:6b:c0:67:58:b2:bb:4b:44:72:d3:cc:a5 localhost@localhost
The key's randomart image is:
2) Uploading the generated certificate from client computer to server
$ ssh-copy-id -i ~/hetzner.pub root@
12.34.56.78
~/.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting, you may still want to use a password. $ sudo nano ~/.ssh/authorized_keys
or $ sudo cat
~/.ssh/authorized_keys
, you should see a file with a one or more lines of random characters, these are the uploaded or generated keys known to this machine.~/.ssh/authorized_keys
looks like this, i cut off few hundred of characters from right of both lines:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAX ...
ssh-rsa
AQCqFd4B798zz9Lu+a3jGjhVXBRx ...
3) Test the connection
$ sudo ssh -i ~/hetzner.pem root@12.34.56.78
On client side
If you do not wish to supply the key path every time on client computer when connecting to remote server, one must tell OpenSSH where to look for private key, by default it looks in ~/.ssh/id_rsa and other folders, use ssh with -v parameter, verbose mode will print what it does step by step on screen. Usually this file should contain something like
-----BEGIN RSA PRIVATE KEY-----
fjksdfjsdlfjksdlfjlsdjfsdl0GrdNS326iv4CcJHASJ2EMpXnIaUpBtc5U2SY14yq8/4gfRLHLdbwzzx/O
PEjlPv1BX4OJlxSWtKPaQsb5QsgwJseoNmBl1djTSY3haZS9P89MsNKiqlv1XtwbcMYOQRVydFdn
.....
.....
......
NHfo3MomYtSoawyBFfsdfsdfasdfasdfasdfasJKJFLSJLDJKSJDVXG58e2Vn7vmY4DYHDDkBd3Y=
-----END RSA PRIVATE KEY-----
You may have this file with .pem suffix. Pem is your private key, unlike .pub - the public key, private key stays always only on you computer, newer give up your private key. Content of ~/.ssh/id_rsa can be replaced with .pem file, it works fine, no conversion is needed.
Two or more private keys
If you have more servers and you wish to connect using multiple private keys, create ~/.ssh/config file, that contain following lines:
Host server1 server1.company.com
Hostname 12.34.56.78
User ubuntu
IdentityFile /media/11361B1123123634/server1.pem
Host server2 server2.company.com
Hostname server2.company.com
User root
IdentityFile /media/11361B1123123634/server2.pem
Host myPC myPC.local
Hostname 192.168.0.106
User mike
IdentityFile /home/mike/.ssh/id_rsa
This file is recognized by ssh by default, it must be named config full path: ~/.ssh/config and if you wish to use sudo (for example later in cron with rsync), this file must be also accessible as /root/.ssh/config. In above file /media/11361B1123123634/ is my encrypted USB drive, so the upper two connections works only if the drive is mounted. In Host, first name is short name that can be used with ssh command, for example:
$ ssh server1
This should now connect you to server1.company.com without typing a password, this way also rsync and other command that use ssh may be used to work with other servers without supplying typed or visible passwords every time they communicate.
Troubleshooting
Agent admitted failure to sign using the key
# eval "$(ssh-agent)"
# ssh-add
# chmod 550 .ssh
Troubles with key path, rsync prompting for password when should not
If
using rsync with sudo, it looks for key file in /root/.ssh/config not
in /home/user/.ssh/config, so be sure to copy or link this file to
correct location, otherwise ssh and scp will be working fine while rsync
will prompt for password.