Tuesday, December 16, 2014

[Servers] : SSH login without password

SSH login without password


We use SSH or Secure Shell to make connection between computers to execute commands. Username and password authentication is normally used to establish connection. With a good strong password, it’s secure enough to use SSH since everything is encrypted through SSH. In this article I’m going to show you how to SSH login without password or SSH Passwordless Login Using SSH Keygen by using private/public key-based authentication system. Key-based authentication is more convenient and secure than tradition password authentication since pair of ssh private/public key is encrypted with asymmetric cryptography.
Secure Shell keys are made of two keys: a private key, that must be kept secret, and a public key which can be uploaded to any computer you need to access a ssh server. Note: Keys can be used with a password (passphrase to be correct) or without one.

Generate Keys

SSH supports both RSA and DSA, there are differences between DSA and RSA, but for our purpose to encrypt and decrypt data for SSH (Secure Shell), it’s up to use what to choose. By default if you don’t defined which algorithm, ssh-keygen will create RSA. Your ssh private/public keys will be generated and saved to your home ~/.ssh directory.
Creating RSA Secure Shell Keys
RSA (Rivest-ShamirAdleman) algorithm is a cryptosystem, owned by RSA Security which similar to DSA is widely used to transmit date securely over networks or the Internet. The greatest benefit of using RSA is RSA uses asymmetric keys. Meaning that if you send an encrypted message, everyone can encrypt data for you to read by using public key, but only you have a private key (only you have it) which is required to decrypt the data.
By default, ssh-keygen or ssh-keygen -t rsa will create a 2048 bit RSA key pair. For maximum security (over kill for our purpose), if you want you can increase the key to 4096 bit for RSA.
Just hit enter and don’t change the ssh keys location, if you don’t want ssh with password or passpharse, leave it empty. Output for ssh-keygen -t rsa:
Creating DSA Secure Shell Keys
Similar to RSA, DSA is another algorithm which is developed by NSA or US. National Security Agency. DSA must meet US. government’s standard for digital signatures which is safe for SSH usage. DSA or Digital Signature Algorithm is based on discrete logarithm, which is a one way math problem. Means it’s easy to create one way math equation, but you can’t solve that equation another way around.
Just hit enter and don’t change the ssh keys location, if you don’t want ssh with password or passpharse, leave it empty. Output for ssh-keygen -t dsa:

Key files permissions

Typical directories and files permission in Linux
Normal directories permission: 755 or (rwx r-x r-x)
  • Owner has Read, Write and Execute
  • Group has Read and Execute only
  • Other has Read and Execute only
Normal files permission: 644 or (rw- r– r–)
  • Owner has Read and Write
  • Group has Read only
  • Other has Read only
SSH private and public keys need stricter directories and files permission. Generally ssh private keys will be stored in user’s .ssh directory or ~/.ssh/. You will want 700 for ~/.ssh/ directory and 600 for private key, and 644 public key. You don’t need change public key permission since it has default linux file permission.
~/.ssh/ directory permission: 700 or (rwx — —)
  • Owner has Read, Write and Execute
  • Group has no rights
  • Other has no rights
Private key files permission: 600 or (rw- — —)
  • Owner has Read and Write
  • Group has no rights
  • Other has no rights
To change ~/.ssh/ directory and private keys permission
For DSA private key
For RSA private key
You might get error like this if you have not set your ~/.ssh/ directory and private/public key correctly.
or
For authorized_keys on remote host
You might get error like this if you have not set your authorized_keys files correctly.

Copy public key to the SSH server

Now you should have your SSH public and private keys. You will need to copy your public key to the remote ssh server which you want to login without a password :) There are two ways to do it, the hard way (manual copy) or the easy way by using ssh-copy-id command. I will show you how both ways in this article.
Manual way or Geek way
Depends on what key type you generated DSA or RSA, the ssh public key should look like id_dsa.pub or id_rsa.pub. Basically you need to copy the content of the public key on your local machine to the remote ssh server.
Copy the public key content in your local machine
Change directory to /.ssh/ directory
View DSA public key
View RSA public key
Sample ssh RSA public key
You should be able to view the ssh public key, copy full content of the file to a text editor for temporary.
Next, login to the remote ssh server with your username and password, yes the old way :)
You wil need to create authorized_keys in ~/.ssh/ directory on the remote ssh server. In the remote ssh server
Copy and save the content of the ssh public key from your local machine which I told you to copy it to text editor to authorized_keys file on remote ssh remote server.
Ease way or lazy way
I’m going to reveal the secret Linux Command which help you to copies localhost ssh public key and install to the remote machine’s authorized_keys file. Please don’t tell this secret to anyone or the world will be doomed. In your local machine, type in:
You will be asked to type your password of the user at the remote-host once to copy/import id_rsa.pub file from your localhost to remote-host. It’s fast and easy, isn’t it?
There is also another secret Linux Command to perform same function as ssh-copy-id if you want to know
For RSA ssh public key
For DSA ssh public key

Configure SSH server to accept key authentication

We have everything almost ready to go, the last but not least is to configure ssh server to accept private/public key authentication.
Open sshd_config file
Search for
Make sure that two lines are not commented( hash # sign in-front of the line), if you see # or hash sign in-front of that two line, remove the # or hash sign, and yes answer after RSAAuthentication and PubkeyAuthentication. Save and restart your ssh server.
or

Login to SSH server without password

Everything is set to go, you should be able to login to your remote ssh server without password (if you didn’t set passphrase when you generate ssh keys).
or if you copied/saved your private key to different location, you can use

Changing your passphrase

If you have decided to use passphrase for extra layer of security for ssh private key, in case if you want to change your passphrase later on, you can use -p option with ssh-keygen to change passphrase of private key file.
Don’t forget to use -p at the end of ssh-keygen, if you don’t use -p option, ssh-keygen command will generate a new pair of public/private ssh key again over your old keys (you will be asked before anything was overwritten), and your old public/private key will be useless.
By changing your passphrase of your private key does not affect your public key, means you don’t need to do anything with our old public key, but you will get a new private key with new private passphrase.

Convert ssh key to putty ppk private key

By any chance if you need to use putty on a windows machine to login your remote ssh server, you have to convert your private ssh key generated in linux to putty private key ppk format. On your windows machine, download PuTTY Key Generator, it’s called PuTTYgen: puttygen.exe
After downloading PuTTY Key Generator, run puttygen.exe file. To convert linux private ssh key to putty ppk private key, go to Conversions, it’s on the top bar. Then choose Import key, locate your linux ssh private key. You will something like the picture below.
In Parameters section, depends on what type of your private key, dsa or rsa, choose SSH-2 RSA or SSH-2 DSA accordingly. After PuTTY Key Generator imported/loaded your linux ssh private key, choose Save private key
(I used passphrase for my keys, if you didn’t use passphrase, you should not see *****)
putty_key_generator

SSH login without password using PuTTY

Putty is a free telnet/ssh client, you can use PuTTY to login with password or with private/public ssh key. After you you converted your id_rsa or id_dsa private key to putty ppk format, you can use that .ppk file to login to your remote ssh server without typing password.
First, start your putty.exe, you can se Host name (or IP adress) and Port text box. Type in your SSH server host name or address, and your ssh server port.
putty_session
Next, on the left hand size, you should see Category, under Connection, go to SSH, then Auth. Click on Browse to locate your putty ppk private key. After that click on open to start your ssh session. You will be asked for your ssh username, if you have your public/private ssh key set up correctly, you won’t be asked for password.
putty_ssh_authentication
Create a public SSH key from private key
In case you lose your public key in the remote ssh server for any reason, you can recreate public key from your old private key by using -y option with ssh-keygen command
For private RSA key
For private DSA key

Note:
  1. If you see
It means ssh-agent does not recognize the newly generated ssh key yet. Logout and login your current ssh session, or use this command
  1. To add another layer of protection with your public/private key, use passphrase. You still need your private key to login but with a passphrase (password).
  2. you can copy your private key (id_dsa or id_rsa) to your usb flash drive, and use your private key to login to your remote ssh server anywhere you go.
  3. You can also generate public / private ssh key by using PuTTY Key Generator, I will show you how in another article.
  4. I found some great videos about DSA and RSA algorithm if you really want to understand more about them.
  5. If you have configured everything but still being asked for password, the remote server might have SELinux (Security-Enhanced Linux) enabled. You can check if SELinux is enabled or not by using
    or
    If SELinux is enabled or enforcing, you can disable it by using this command
  6. If you use putty and get the error:
    Make sure you set the right permissions for authorized_keys file and .ssh directory

No comments: