I get this question a lot. This makes automation between servers great. Nothing like being able to sync data or log in grab some information.
It is super easy. Lets get started! So we want to be able to copy files from Server02 to Server01 without getting harrased with a password. Lets start out with what we will do on Server01!
Lets log in then lets generate keys for the a account we want to use. I am using the user bob for this example. Lets run the command ‘ssh-keygen -t rsa’
Okay I have attached the output below to show you what it will ask. First it asks for a place for the files. The default is exactly where we want to put it. So hit enter. Next it asks for a passphrase. For our purproses we don’t want one. So just hit enter twice!
ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/bob/.ssh/id_rsa): Created directory '/home/bob/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/bob/.ssh/id_rsa. Your public key has been saved in /home/bob/.ssh/id_rsa.pub. The key fingerprint is: 8d:e5:3c:5b:97:85:35:76:23:49:02:4d:ff:2b:b0:b7 bob@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ (removed some cool asci art here)
Now once we are done. It creates the file structure below.
.ssh ├── id_rsa └── id_rsa.pub
What we are concerned with is the id_rsa.pub. Some people get fancy and scp it over to the remote box we want to setup the ssh key on. I just cat it and copy. ‘cat id_rsa.pub’
cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1DZygXl+aC68m8DdMpBfQr6yQcIchwvcwCKvuZGddutoXoL7wCdmwWsm5qLFeeRcG3Irmte8C4+KEEvWWt3+BS8r8SrQpfJ/1YluxSLgwz6CRede58aqZv+Td7Yy1dIZucfhXgGtJCIrflfHVYMI97HPMStKg3yLuX0GcdkgtviKmtDmByqtb4N4dalgPLXHbQuloi4kIOlkLLYbuQbd4g5LcrOt56d8A3OGIjYp/4oefi5eXFlgCTmWvjqerbzTle5ub8UQstqaQqbKrkTNeWzVVe96xSD3UHy8ZvHTDdlwRT5WEGyP5038HPb0O2xJgPEBK1og/XnKKFQckAWJdQ== bob@localhost.localdomain
Now I copy the ssh-rsa line to the end. Now lets jump over the other server ‘Server02′!
so I want to log into my ‘bob’ user account on Server02.
If the account has never used the ssh keys before. I just get lazy and run
ssh-keygen -t rsa
This way I know the folders are created correctly. Now I know the file I need usually isn’t created but it might be. what I am looking for is
authorized_keys
So lets edit this file ‘authorized_keys’
vi .ssh/authorized_keys
Then I hit ‘i’ to insert! You know vim right?!? then we copy my key!
ssh-rsa AAAB3NzaC1yc2EAAAABIwAAAQEA1DZygXl+aC68m8DdMpBfQr6yQcIchwvcwCKvuZGddutoXoL7wCdmwWsm5qLFeeRcG3Irmte8C4+KEEvWWt3+BS8r8SrQpfJ/1YluxSLgwz6CRede58aqZv+Td7Yy1dIZucfhXgGtJCIrflfHVYMI97HPMStKg3yLuX0GcdkgtviKmtDmByqtb4N4dalgPLXHbQuloi4kIOlkLLYbuQbd4g5LcrOt56d8A3OGIjYp/4oefi5eXFlgCTmWvjqerbzTle5ub8UQstqaQqbKrkTNeWzVVe96xSD3UHy8ZvHTDdlwRT5WEGyP5038HPb0O2xJgPEBK1og/XnKKFQckAWJdQ== bob@localhost.localdomain
Next we ‘:wq’ to exit and save.
Now here is where most people get hung up! We have to make sure the authorized_keys has the permissions of 600
chmod 600 authorized_keys
Then I ‘cd ..’ that should put us back in our home dir. Now lets test this bad boy!
Jump back to server01.
Lets test
ssh server02 id
If we are lucky we see this.
uid=501(bob) gid=502(bob) groups=502(bob)
No passwords etc. It might prompt to except an inital key but we should see no password prompt!
Also just a side note. I used the user account ‘bob’. but if you are syncing between different accounts say bob -> root
You would test your key with ‘ssh root@server02 id’ If you leave off the ‘root@’ it will use the current logged in user.
Well hope that helps!
