Tutorial: Allow auto login between Linux machines

In my work, I create many shell scripts which automate different procedures.

Sometimes, it is required that a script will be able to log into a remote machine and run some commands there or get files from the remote machine, so doing this “trick” will allow the script to log into the remote machine without the need to enter a password, this little auto login trick will save a great amount of time.

It is also convenient if you want to allow a user to log into a remote machine using root privileges without exposing root password to him.

In order to achieve that, we will need to follow the next steps:

For the example I will use Lin1 and Lin2.

I would like to allow Lin1 to connect to Lin2 without the need of entering a password:

On Lin1, type:

# ssh-keygen -t rsa and then keep pressing ‘Enter’ until getting a new prompt line, like so:

auto login

 

The previous command we ran generated public and private keys of the Lin1 machine.

Next, In order to send the public key that we generated to a remote machine, we will run:

# ssh-copy-id user@remote_machine_ip

auto login

 

This command actually reads your rsa public key and sends it to the remote machine which is then added to the end of the file ~/.ssh/authorized_keys on the remote machine.

If for any reason you would like to undo this, you will have to log into the remote machine, edit ~/.ssh/authorized_keys , find the relevant line and delete it, then the machine will no longer know Lin1 machine.

 

After running the command, we will be asked to supply the user’s password, in my case it was root’s password.

And that’s it, next time I will ssh to the remote machine it will log in automatically without the need to enter a password, like so:

auto login

This has been tested on Ubuntu and CentOS but it works on every Linux distribution.

Comments

comments