Wednesday, August 13, 2008

Passwordless login to the linux servers

If you have multiple servers and do not want to type password everytime you login to the server.
You can create the passwordless sessions using public key private key encryption.
Public Key to be stored on the remote server
private key to be stored in local machine
You have to download the puttygen.exe for generating the public private key pair.
Select RSA / DSA using the encryption required

Now
  1. Download PuTTY
  2. Download PuTTYgen
  3. Open PuTTYgen
    • Select SSH-2 RSA
    • Click ‘Generate’
  4. Save the Private Key
    • Click ‘Save private key’ (you do not have enter a password)
    • Save the private key in location easy to remember.
  5. Copy public-key
    • Select all text in the public key area.
    • right-click and select copy
  6. Configure the server settings in Putty
    • Open Putty
    • Click Session (left column)
      • Enter the server hostname or IP address
      • Enter a name under ‘Saved Sessions’
    • Click Connection > Data (left column)
      • Enter ‘root’ for the auto-login username
    • Click Connection > SSH (left column)
      • Select ‘2′ as the Preferred SSH protocol version.
    • Click Connection > SSH > Auth (left column)
      • Browse to the private key from step 4.
    • Click Session (left column)
      • Click Save
  7. Open a session with the server
    • Open PuTTY
    • Select the session saved earlier.
    • Click ‘Load’
    • Click ‘Open’
    • Login
  8. Add client public key.
    • You should still have the public-key in the clip-board from step 5.
    • Open the authorized_keys file on the linux server
      • [root@server]#vi ~/.ssh/authorized_keys2
      • Press the ‘i’ key to insert in vi.
      • Go to the bottom of the file and right-click on the putty screen (This should insert the public key generated with PuTTYgen)
      • Press the ‘esc’ key to get out of insert mode in vi.
      • Press ‘:’ to enter command mode in vi
      • Type ‘wq’ to write and quit vi
      I use nano editor if you do not have nano installed than run
    • #yum install -y nano
    • Now nano ~/.ssh/authorized_keys2
    • Copy the public key from puttygen and paste in this file. make sure you have all the matter in one single line or it won't work. I spent lot of time figuring it out.

  9. Now, you should be finished. Let’s test.
    • Open PuTTY
    • Select the session saved earlier.
    • Click ‘Load’
    • Click ‘Open’
  10. If you were able to login without entering your username and password you are finished!!If not, please continue to troubleshoot.

Troubleshooting: If you do not have a /root/.ssh folder, we will have to create one and set the permissions:

[root@server]#mkdir ~/.ssh
[root@server]#chmod 700 ~/.ssh

If you do not have a authorized_keys2 file, we will need to create one and set the permissions:

[root@server]#vi ~/.ssh/authorized_keys2
[root@server]#chmod 644 ~/.ssh/authorized_keys2

If you get an error that the key was rejected, you need to make sure the permissions are set correctly on the .ssh directory and authorized_keys2 file.

[root@server]#chmod 700 ~/.ssh
[root@server]#chmod 644 ~/.ssh/authorized_keys2

You are all set.

Enjoy!!!

No comments: