For windows users:
To generate ssh keypair using puttygen in windows, follow my guide here.
For linux users:
To generate ssh keypair using puttygen in windows, follow my guide here.
For linux users:
This guide assumes that key based authentication is already enabled in the server. If you are a server admin, you can follow the guide here to set it up. If you are an end user, just confirm with your server provider whether key based authentication is enabled in ssh.
1. Create a key pair in local machine using the following command:
ssh-keygen -t rsa
Just press enter to save the key pair in the default location. For root user, it will be /root/.ssh and for any other user, it will be /home/user/.ssh (assuming /home/user is the home dir of user)
You do not need to enter any pass-phrase during key creation, or else you will be asked to enter pass-phrase each time you login with key.
You now have two keys generated inside .ssh directory. id_rsa and id_rsa.pub.
id_rsa_pub is the public key and needs to be put in the server.
2. Assign proper permission.
chmod 600 .ssh/id_rsa
3. copy the contents of id_rsa.pub into the file '/root/.ssh/authorized_keys' in the server to which you need to connect (if you need to connect as root user) or '/home/user/.ssh/authorized_keys' if you need to connect as "user".
(IMP: if there is no .ssh folder in your user's home directory, create one)
scp .ssh/id_rsa.pub user@serverip:~user/.ssh/temp.pub
4. Now login to the server and copy the contents of id_rsa.pub into the file authorized_keys.
ssh user@serverip
cat .ssh/temp.pub >> .ssh/authorized_keys
And you are done. Just logout and then simply login using the normal ssh command and you won't be prompted for a password.
Note: if you didn't save the private key in the default location, just use -i flag along with ssh command and specify the location of private key file.
ssh -i /test/id_rsa root@serverip
Note: In some cases, you would need to add the private key into ssh-agent after generating it. For that issue the foll command in your local machine after generating key pair.
ssh-add .ssh/id_rsa
.ssh/id_rsa is the private key path and may vary according to the way you saved them.
If you experience an error when issuing the above command, it can be rectified by issuing the following command:
eval `ssh-agent`
and then
ssh-add .ssh/id_rsa






0 comments:
Post a Comment