Posted by Helder Guerreiro
To carry out a remote login without the authentication, with the pair user and password, is to install on the remote machine the public part of our authentication key.
The problem is that, for having an automatic login, the authentication key
must not be protected with a password.
This method is quite safe, assuming that the
private part of our athentication key doesn't fall in to the wrong hands!
Keeping this in mind, supose we have two machines, MailServer and BackupServer and we want to connect from the first to the second using this method.
We enter the MailServer with the user MailUser that will use this funcionality and run the command:
$ ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/MailUser/.ssh/id_rsa): Created directory '/home/MailUser/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/MailUser/.ssh/id_rsa. Your public key has been saved in /home/MailUser/.ssh/id_rsa.pub. The key fingerprint is: 06:19:2c:12:f2:e8:28:6e:46:e4:8f:99:88:b5:21:5f MailUser@mailServer
When it asks for the password, on the lines in red, just do <ENTER>
The next step is to copy, in a safe way, the public key id_rsa.pub
to the user account on the remote server BackupServer.
Lets assume, for example purposes, that the user id in this server is BackupUser.
We can do this copy using ssh itself:
$ scp ~/.ssh/id_rsa.pub BackupUser@backupServer:~/
To finalize, we enter the BackupServer and we add the public key to the list of authorized keys:
$ ssh BackupUser@backupServer $ cat id_rsa.pub >> ~/.ssh/authorized_keys
We can also do this with only one command:
$ cat .ssh/id_rsa.pub | ssh BackupUser@backupServer 'cat >> .ssh/authorized_keys'
The advantages of this type of authentication are obvious if you
want to carry out automatization tasks.
The names used on this example
give you already a clue of a possible application that is the backup
to remote machines.
Since we can also run command remotely with ssh the applications are endless.
Just an example, to get a folder listing on a remote machine we can do:
$ ssh BackupUser@backupServer 'ls'