Tuesday, February 10, 2009

SSH With Keys

Short but sweet tutorial for enabling your ability to ssh with a key instead of a password. The typical use for this is to enable a user to ssh to a machine without having to enter a password. Note that this method is only as secure as your key. If your key is compromised, you're screwed.

Pre-requisites:
  • Account on local machine (here) that matches username of account on remote machine (there)
  • SSH server that allows keys instead of passwords on the remote machine

    First, assuming you have a user on both machines, notroot, while logged in as that user, generate your ssh-key:
    notroot@here:/home/notroot$ ssh-keygen
    Generating public/private rsa key pair.

    You will be asked where to save it, the default location should be fine:
    Enter file in which to save the key (/home/notroot/.ssh/id_rsa):
    Created directory '/home/notroot/.ssh'.

    Then you'll be asked for a passphrase. Now, you might want to know what the use of doing this is if you're just going to need to enter a password again. It's not a password. It's a passphrase. It's something you know and the key is something you have, both of which make the overall security a little better. A non-blank passphrase, coupled with an SSH server on the remote side that doesn't accept just a password, makes for a more secure SSH system. But, of course that wouldn't be easy. So to be easy, leave this blank:
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /home/notroot/.ssh/id_rsa.
    Your public key has been saved in /home/notroot/.ssh/id_rsa.pub.
    The key fingerprint is:
    12:ab:34:cd:56:ef:78:gh:90:ij:12:kl:34:mn:56:op notroot@here
    The key's randomart image is:
    +--[ RSA 2048]----+
    |:-)(-: |
    |8-DD-8 |
    |@-| B-> |
    |00 |
    |--- |
    |? |
    | |
    | |
    | |
    +-----------------+

    OK, you've got the key, now what? Well, for awhile, there was a bunch of hassle in moving the key and copying the username and whatnot all over creation in the remote directories and other such hassle. Believe me, it was a pain. Now? Simple:
    notroot@here:~/.ssh$ ssh-copy-id -i id_rsa.pub notroot@there
    notroot@there's password:
    Now try logging into the machine, with "ssh 'notroot@there'", and check in:

    .ssh/authorized_keys

    to make sure we haven't added extra keys that you weren't expecting.

    Voila! Now, to test it, try to ssh to the box:
    notroot@here:~/.ssh$ ssh there
    Linux 2.6.21.5-smp.

    Tomorrow will be canceled due to lack of interest.

    notroot@there:~$

    Perfect.
  •