Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

Friday, May 20, 2011

SSH without password

Ever since I joined IISc, ssh-ing into other machines seems to be a daily task. Its hard to type in passwords every time. So this is what I did to avoid typing passwords for ssh.

Lets call the machine you are connecting from as "home" with your user name "homeuser" and the machine you are connecting to as "server" with your user name as "serveruser". Please note that in case the account in your "home" computer is compromised, the attacker can effectively login to the "server" as well. So use this with caution.

First you have to generate an RSA public-key, private-key pair. The public key will be placed in the server computer so that it can identify your computer when you ssh to it. Thankfully Linux installations come with all the tools that you require to do this. So all you have to do to generate this key pair is open a terminal in "home" and type:
homeuser@home:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/homeuser/.ssh/id_rsa):
Created directory '/home/homeuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/homeuser/.ssh/id_rsa.
Your public key has been saved in /home/homeuser/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:69:bf:89:0a:37:16:e9:25:e2 homeuser@home
Next you have to store the public-key in "/home/homeuser/.ssh/id_rsa.pub" to "server". For this first create a directory on "server" by ssh-ing to it: (don't worry if the directory is already there):
homeuser@home:~$ ssh serveruser@server mkdir -p .ssh
serveruser@server's password:
Next copy the public key from "home" to the "server"
homeuser@home:~$ cat .ssh/id_rsa.pub | ssh serveruser@server 'cat >> .ssh/authorized_keys'
serveruser@server's password:
Now you are done. Try ssh-ing to the server and you will be directly greeted with a prompt:
homeuser@home:~$ ssh serveruser@server
serveruser@server:~$
Please note that:
  • This method kind of identifies your "home" computer to the "server", so in case your account in "home" is compromised, immediately delete the corresponding key from .ssh/authorized_keys at the "server".
  • You have to do this every time you install a new OS in "server" or "home".
  • I have only tested this on the machines that I use ... so this should work well in Fedora, Ubuntu and Mac. Sometimes your version of ssh might differ and you might have to put the public key to ".ssh/authorized_keys2" and change its file permissions to 640 and change file permissions of ".ssh" to 700.
Thanks for reading through. If you have any further questions/comments, just post it here. 

Edit [2011-09-30] For all the lazy people (like me) out there, here is a simple one liner that will do the same trick. Just type in the server password twice when the terminal asks.
ssh-keygen -t rsa; ssh-keygen -y; ssh serveruser@server mkdir -m 700 -p ~/.ssh; scp ~/.ssh/id_rsa.pub serveruser@server:~/.ssh/authorized_keys
 This simple and very effective one-line idea was given to me by my colleagues at amazon.

Friday, May 28, 2010

How to ssh to Mac

Ever since I got my Mac Book Pro, I have been trying to ssh into it. Unlike Fedora, Ubuntu and RHEL, Macs do not have this ssh switched on by default. Here is the step-by-step procedure for doing so (note that this is for the machine to which you need to connect, not the one from which you would connect) :

  1. Go to "System Preferences"
  2. Select "Sharing"
  3. Unlock the lock symbol at the bottom-left corner
  4. Check "Remote Login"
  5. Now close this window and try out ssh-ing to your machine. In case you do not know the IP of your machine, type "ifconfig" in the terminal to get it.
One could have also modified the ssh configuration file located at /etc/sshd_config for the same. Initially I had also done the same. However the method that I described above is completely GUI based, so even non-geeks can try it out. Besides this interface also allows one to tweak a lot of other things as well.

Disclaimer: I am a Mac newbie, its only since last one week that I have been using it. The above procedure is what I figured out after some attempts in my Mac Book Pro running OS X. I do not really know if it would be the same for other Macs as well.