[tips'n'tricks]SSH Automatic Login using Public Key
Hehehehe .. another sharing again ..
Perhaps almost all engineer know how to do it but .. i think it’s okay for writing it in here since it’s very usefull for others (hopefully).
So lets the tutorial begin then ..
Condition
- Let say you have PC / Laptop (PC-A) at your home and want to connect to your server (Server-A) in your office.
- You’re too tired for always typing your password each time you want to logon to your Server-A.
- You need to implement some script which will be automatically executed remotely from your PC-A to Server-A
So Here it is the steps :
ON YOUR PC-A do the following (assuming your PC-A is *nix based not windows based):
- login to your PC-A with your username and password (let say you use “gundul” as your username for logging in).
- generate your key by issuing this command
ssh-keygen -t rsa
Note : just press Enter if it asks about where to put the file and passphrase as default but if you want to make it more secure just follow the direction that appears.
- your key will be generated in ~/.ssh/id_rsa (this is your private key) and ~/.ssh/id_rsa.pub (this is your public key).
- issue this following command (IMPORTANT !!!!)
chmod 400 ~/.ssh/id_rsa
Note : if you don’t do above command to change the file permission, you will always be asked for password even you already done all this tutorial.
- copy the ~/.ssh/id_rsa.pub from PC-A to SERVER-A under any directory by any way (scp,ftp,whatever).
STEPS ON SERVER-A
- login to SERVER-A with your preferred username and password (let say you use “kampret” as your username for logging in).
- let say you already copy above file to /tmp directory on SERVER-A, now you just need to issue this following command :
cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys2
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys2
rm /tmp/id_rsa.pub
DONE !!!
FOR TESTING IT :
- login to your PC-A with “gundul” as username
- ssh to SERVER-A with “kampret” as username
ssh kampret@server-a
you should automatically login without being prompted by password.
So .. once again .. hopefully this tutorial will be helpful.
Any comment ? just drop your question below.
-Superpinjal-