{"id":120,"date":"2011-05-12T14:33:08","date_gmt":"2011-05-12T21:33:08","guid":{"rendered":"http:\/\/www.robogourmet.com\/?p=120"},"modified":"2012-03-15T23:47:43","modified_gmt":"2012-03-16T06:47:43","slug":"public-key-authentication-with-ssh","status":"publish","type":"post","link":"http:\/\/www.robogourmet.com\/?p=120","title":{"rendered":"Public Key Authentication with SSH"},"content":{"rendered":"<p>I spent some time this week trying to get public key authentication to work with SSH so that when I used TortoiseSVN, I wouldn&#8217;t have to keep typing in my password. I generated a key, installed the public key on the repository server &#8211; but nothing. SSH connections still asked for a password. These are the problems I had on a Windows 7 machine running PuTTY, Pageant and the aforementioned TortoiseSVN, connecting to both OpenSSH and commercial SSH servers running some flavor of UNIX.<\/p>\n<p><strong>1. Generate a keypair.<\/strong><br \/>\nIf you&#8217;re using PuTTY, you can use the utility PuTTYGen to generate a keypair. You want to generate either an SSH-2 RSA or an SSH-2 DSA keypair and definitely use a good passphrase! I would also recommend putting in a descriptive key comment which includes your name and the generation date and even if it&#8217;s an RSA or DSA key. Something like userName-rsa-20110512. The keypair consists of a private key and a public key. Save both files and also copy the public key in the box called &#8220;Public key for pasting into OpenSSH authorized_keys file.&#8221; The private key you should keep safe on your client machine. The public key should be installed on the server in one of three places.<\/p>\n<p><strong>2. Install the public key<\/strong><br \/>\nNewer versions of OpenSSH want your key to be in a file ~\/.ssh\/authorized_keys. Older versions had two files, ~\/.ssh\/authorized_keys for SSH1 connections and ~\/.ssh\/authorized_keys2 for SSH2 connections. Basically just copy and paste your public key into these files. If you didn&#8217;t copy it from Pageant as described in step 1, you can load your private key in Pageant and it will regenerate the public key for you to copy. Only public keys you want to have access should be in the authorized_keys\/authorized_keys2 files! Each key in the file looks something like:<\/p>\n<blockquote><p><code>ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIBeIywsWQFXXoWsQG7aNKkt4AVyjkt4\/c7yCudH5sMUrsnfpBxGJn1SjNaZorCnZm4j7k+gGw2QFJBrJFsWLAqZwceanHQlid9xmKB9wTN45yzfqFuKrYwpByT9npX2MMC7RjOUm7ccPH3gg1KYn2kPIv\/EGsSHNwGYG56EKBZaxQ== userName-rsa-20110512<\/code><\/p><\/blockquote>\n<p>This breaks down to the key type (RSA\/DSA), the key itself, and the comment field.<\/p>\n<p><strong>One important thing<\/strong> &#8211; newer versions of the OpenSSH server daemon check the permissions of the authorized_keys\/authorized_keys2 files to ensure that they are <em>not<\/em> world or group writable and will quietly fail if it is! I spent a long time trying to figure this out before a sysadmin put me straight.<\/p>\n<p>The commercial version of SSH has a file ~\/.ssh2\/authorization which lists the accepted key files, each line looking something like:<\/p>\n<blockquote><p><code>Key keyFile.pub<\/code><\/p><\/blockquote>\n<p>where keyFile.pub is a text file containing your public key. This was generated by PuTTYgen when you hit the &#8220;Save public key&#8221; button and should look like:<\/p>\n<blockquote><p><code>---- BEGIN SSH2 PUBLIC KEY ----<br \/>\nComment: \"userName-rsa-20110512\"<br \/>\nAAAAB3NzaC1yc2EAAAABJQAAAIBeIywsWQFXXoWsQG7aNKkt4AVyjkt4\/c7yCudH<br \/>\n5sMUrsnfpBxGJn1SjNaZorCnZm4j7k+gGw2QFJBrJFsWLAqZwceanHQlid9xmKB9<br \/>\nwTN45yzfqFuKrYwpByT9npX2MMC7RjOUm7ccPH3gg1KYn2kPIv\/EGsSHNwGYG56E<br \/>\nKBZaxQ==<br \/>\n---- END SSH2 PUBLIC KEY ----<\/code><\/p><\/blockquote>\n<p>Where I work, my home directory is on a central file server and is automatically linked to by the other servers on the network, so I have my public key in all three locations to handle the different versions of SSH servers on the network.<\/p>\n<p><strong>3. Connect with your key.<\/strong><br \/>\nIn your PuTTY connection settings under Connection &gt; SSH &gt; Auth, you want to specify the location of your private key. If you&#8217;re going to be using Pageant, you&#8217;ll instead want to check the box &#8220;Attempt authentication using Pageant.&#8221;<\/p>\n<p><strong>4. Setup Pageant to handle your private keys. (optional)<\/strong><br \/>\nPageant is a utility that manages your private keys so you don&#8217;t have to specify which key is for what connection. Also, if your private key is protected by a passphrase, you only have to enter it once and Pageant will store the decoded key. This is pretty straight forward &#8211; just run Pageant and add your private key(s).<\/p>\n<p>That&#8217;s it! At this point you should be able to connect without password authentication! If you&#8217;re having problems, you may want to try setting up a connection in PuTTY where you give it the path to your private key directly and see if that works.<\/p>\n<p><em>Edit (February 17, 2012): Recently installed TortoiseSVN 1.7 and public key authentication broke. <a title=\"Upgrading to TortoiseSVN 1.7\" href=\"http:\/\/www.robogourmet.com\/?p=313\" target=\"_blank\">Apparently it requires PuTTY\/Pageant 0.61 or later.<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I spent some time this week trying to get public key authentication to work with SSH so that when I used TortoiseSVN, I wouldn&#8217;t have to keep typing in my password. I generated a key, installed the public key on the repository server &#8211; but nothing. SSH connections still asked for a password. These are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":{"0":"post-120","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-tech-tips"},"_links":{"self":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts\/120"}],"collection":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=120"}],"version-history":[{"count":11,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":371,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions\/371"}],"wp:attachment":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}