SSH Keys Simplified
I’ve come across SSH keys in the past, but I never used them since I found them difficult to use and the couldn’t find any need! Recently I was introduced to them in MediaWiki Hackaton here at IIT Roorkee, and that’s when I learnt about them. I wrote this article assuming you know nothing about SSH keys and tried to keep it as simple as possible.
What is SSH?
SSH(Secure Socket Shell) is a UNIX-based command interface and protocol for securely getting access to a remote computer. SSH keys are one of the common authentication techniques that people use to log into a Unix session.
As a common example, let’s say you’re using Git and you want to push your commits to your remote repository(let’s say on Github. Up until now, you’d type in your Username and Password each and every time you push your commits to the Github servers - which is gruelling! This problem can be resolved by using SSH keys, where you can authenticate against the key once and now every shell session you start will authenticate using the same SSH key.
Let us say you’re in a house with many locks in it. To open each lock, you had to select a particular key from a bunch of them! Now if we are using SSH keys, we can open each and every lock using a single key! See, isn’t that amazing? All we need is to create a public and private key where:
Private Key == House key
Public Key == House locks
Generating a new SSH key (Using OpenSSH)
SSH keys can be generated by using the following commands in terminal:
- Before generating a new SSH key, let us check for existing ones
ls -a ~/.ssh # Lists the files in your .ssh directory, if they exist
If you have them already, you’ll see a list of files probably by the names id_rsa (private key), id_rsa.pub (public key) and known_hosts. If not, proceed to next step.
- If the above files aren’t listed, then execute the following command to generate a pair for you:
ssh-keygen -t rsa -b 4096 -C "email@example.com" # Creates a new ssh key, using the provided email as a label Generating public/private rsa key pair.
- Then you’ll be prompted for a file name where it’ll be saved. Press Enter for default.
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
- Now you’ll be asked for a passphrase. Type in a passphrase (you’ll be authenticated against SSH keys using this) and press Enter.
Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
Voila! You’ve generated a SSH key pair!
For a start, use SSH keys with Github. Refer to Github for further instructions.
Let me know what you think of this article on twitter @utkarshgpta or leave a comment below!