Accessing Remote Machines Passwordless
By Diwanshu Shekhar
- 2 minutes read - 290 wordsIf you are tired of entering password everytime you ssh into your remote machine, you may want to create public-provate key pair and store your public key into your remote machine and the private key into your local machine. You can create such key pair by running the following command from your local machine -
ssh-keygen
This will create two files in your .ssh directory on your $HOME
folder - one with the name id_rsa
which is your private key and the other with id_ras.pub
which is it’s corresponding public key
Place the contents of the public key in the $HOME/.ssh/authorized_keys
of the remote machine.
Now, you should be able to connect your remote machine using -
ssh user@[ip address of the remote machine]
If you want to run a few commands after you passwordlessly connect to your remote machine, your shell script should follow the following format:
ssh user@[ip address of the remote machine] "
mkdir ~/test;
ls ~;
"
Note: The permissions of the files in .ssh
folder for the keys and authorized_keys files, their ownership should be similar to the figure below:
And the permission of the .ssh
folder itself should be 700
One helpful tip I want to live you with before ending this article is the use of config file in the ~/.ssh
folder. If you are ssh-ing many different hosts and if you want to save time typing the entire name of the host you’re ssh-ing into or just don’t want to go through the hassle of remembering long names, you can add these host names into your ~/.ssh/config
file in the following format:
Host api-server-node
HostName 192.168.0.24
User ds
Host emr-master-prod
HostName emr-prod.example.com
User ed
Host emr-master-stage
HostName emr-stage.example.com
User ed
Port 8088