SSH Public Key Auth
If you frequently access hosts using ssh with user and password and do not know public key authentication, this post is for you.
I will not explain about advantages and disadvantages, clearly and practically I want to show you how create and use the private key to authenticate on ssh.
To create a public key and a private key we use the command ssh-keygen
$ ssh-keygen -t dsa |
Type enter until create the keys, that will stay at .ssh in your home
$ ls -l ~/.ssh /home/your_username/.ssh/id_dsa /home/your_username/.ssh/id_dsa.pub |
Now you need copy the content of public key to autorized_key file at the .ssh directory of your remote server.
There are many ways to do it, the simplest may be the following.
$ ssh remote_user@remote_host "echo `cat ~/.ssh/id_dsa.pub` >> ~/.ssh/authorized_keys" |
In one line, this command connect to remote host and copy the public key to correct file. Probably you need to type the remote user’s password to login.
Replace remote_user to user that should login in remote host and, logically replace remote_host to server that you want to login.
May occur that the .ssh does not exists in the remote server at the home user directory, to solve it I run the command to create directory if do not exists.
$ ssh remote_user@remote_host mkdir -p .ssh |
It is important to stress that the server should be configured to accept public key authentication, where is configured in the file /etc/ssh/sshd_config
# allow public key authentication
PubkeyAuthentication yes
If necessary to reconfigure the server, you need restart the service:
For CentOS
$ svc -du /var/service/sshd/ |
Plus!
I created a script that does everything at once. You only need to replace the variables that I mentioned above (user and host) and then the script is ready:
I hope this helps.
Deixe um Comentário
No Comments »
RSS feed for comments on this post. TrackBack URL