{"id":427,"date":"2021-02-11T18:40:41","date_gmt":"2021-02-11T17:40:41","guid":{"rendered":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/?p=427"},"modified":"2021-02-12T07:53:28","modified_gmt":"2021-02-12T06:53:28","slug":"putty-and-ssh-keys","status":"publish","type":"post","link":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/2021\/02\/putty-and-ssh-keys\/","title":{"rendered":"Passwordless Logins with PuTTY and SSH-Keys"},"content":{"rendered":"<p><em>This guide is about key-based logins to remote Unix\/Linux-servers from a Windows PC using PuTTY. If you have OpenSSH on your Windows, Mac or Linux computer, things should be a lot easier and you can <a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/2013\/11\/ssh-mit-macos\/\">type commands directly in a Terminal window<\/a>.<\/em><\/p>\n<h2>Preparation<\/h2>\n<p>Make sure that <a href=\"https:\/\/en.wikipedia.org\/wiki\/PuTTY\">PuTTY<\/a> is available on your computer.&nbsp; This guide uses the following 3 programs: PUTTY.EXE, PUTTYGEN.EXE, PAGENT.EXE.<br \/>\nIn case they are not installed or you run into problems, they can simply be extracted from the <a href=\"https:\/\/the.earth.li\/~sgtatham\/putty\/latest\/wa64\/putty.zip\">ZIP archive<\/a> and run directly (no formal installation or admin account necessary).<\/p>\n<figure id=\"attachment_428\" aria-describedby=\"caption-attachment-428\" style=\"width: 267px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/3x-exe-on-desktop.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-428 size-full\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/3x-exe-on-desktop.png\" alt=\"\" width=\"267\" height=\"229\"><\/a><figcaption id=\"caption-attachment-428\" class=\"wp-caption-text\">PUTTYGEN.EXE, PAGENT.EXE and PUTTY.EXE on Windows desktop<\/figcaption><\/figure>\n<h2>Key Generation<\/h2>\n<p>Start <strong>PUTTYGEN<\/strong> to generate an SSH key.<\/p>\n<p>Select <strong>Ed25519<\/strong> as the type of key in the &#8220;Parameters&#8221; section for a modern key format that will give you a smaller public key string which will be easier to copy and paste later. Then click on the <strong>Generate<\/strong> button.<\/p>\n<figure id=\"attachment_429\" aria-describedby=\"caption-attachment-429\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/generate-ed2219-key.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-429 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/generate-ed2219-key-300x295.png\" alt=\"\" width=\"300\" height=\"295\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/generate-ed2219-key-300x295.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/generate-ed2219-key.png 497w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-429\" class=\"wp-caption-text\">Choose a cool key type and generate<\/figcaption><\/figure>\n<p>Move your mouse around a bit to generate better randomness while the key is generated.<\/p>\n<figure id=\"attachment_430\" aria-describedby=\"caption-attachment-430\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/move-mouse.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-430 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/move-mouse-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/move-mouse-300x300.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/move-mouse-150x150.png 150w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/move-mouse.png 497w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-430\" class=\"wp-caption-text\">Random mouse movements<\/figcaption><\/figure>\n<p>When the key genartion is done, select the public part on the top and copy it to the clipboard. To do so, you can right click on the public key, then chose <strong>Select all<\/strong> and then right click again and chose <strong>Copy<\/strong>.<\/p>\n<figure id=\"attachment_431\" aria-describedby=\"caption-attachment-431\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/copy-pub-key.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-431 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/copy-pub-key-300x272.png\" alt=\"\" width=\"300\" height=\"272\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/copy-pub-key-300x272.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/copy-pub-key.png 548w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-431\" class=\"wp-caption-text\">Copy public key to clipboard<\/figcaption><\/figure>\n<p>Type in a passphrase for your new key to protect it and then click on<strong> Save private key<\/strong> to save your key file in a location only you personally have access to.<\/p>\n<figure id=\"attachment_432\" aria-describedby=\"caption-attachment-432\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/set-passphrase-and-save.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-432 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/set-passphrase-and-save-300x293.png\" alt=\"\" width=\"300\" height=\"293\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/set-passphrase-and-save-300x293.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/set-passphrase-and-save.png 507w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-432\" class=\"wp-caption-text\">Pick passphrase and save key<\/figcaption><\/figure>\n<p>You can close the &#8220;PuTTY Key Generator&#8221; window now.<\/p>\n<h2>Getting the Key to the Server<\/h2>\n<p>To get the public key to the server, you will need to log on using your regular credentials one last time.<\/p>\n<p>Start <strong>PUTTY<\/strong> to establish an interactive connection to the server. If you are connecting for the first time, it is probably a good idea to create and save a session now, so that you won&#8217;t have to type in the server name again and again later on. Type in the server name in the <strong>Host Name<\/strong> field and some name for the session (you can use the server-name here as well or make something up).<\/p>\n<figure id=\"attachment_433\" aria-describedby=\"caption-attachment-433\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-and-save-session.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-433 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-and-save-session-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-and-save-session-300x300.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-and-save-session-150x150.png 150w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-and-save-session.png 469w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-433\" class=\"wp-caption-text\">Create and save session in PuTTY<\/figcaption><\/figure>\n<p>Select the session you want and click on <strong>Open<\/strong> to initiate the connection. Note that when you connect to a server for the first time, you need to <a href=\"https:\/\/www.zedat.fu-berlin.de\/Login-Server\">verify<\/a> (if you want) and save the remote server&#8217;s SSH fingerprint.<\/p>\n<p>Log in with your regular username and password. Using the shell, create a <strong>.ssh<\/strong> directory on the server if it does not exist yet:<\/p>\n<pre><strong>mkdir .ssh<\/strong><\/pre>\n<p>Then create (or edit) the <strong>authorized_keys<\/strong> file within that directory:<\/p>\n<pre><strong>pico .ssh\/authorized_keys<\/strong><\/pre>\n<figure id=\"attachment_434\" aria-describedby=\"caption-attachment-434\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-ssh-authorized-keys.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-434 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-ssh-authorized-keys-300x194.png\" alt=\"\" width=\"300\" height=\"194\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-ssh-authorized-keys-300x194.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/create-ssh-authorized-keys.png 685w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-434\" class=\"wp-caption-text\">Create .ssh and .ssh\/authorized_keys<\/figcaption><\/figure>\n<p>You can use any editor that you are comfortable with on the remove server. A very simple one is called &#8220;pico&#8221;. When inside the editor, right-click to paste the contents of your clipboard. Note that if your public key is not in your clipboard anymore for some reason or another, and you are thus unable to paste it into the editor, you can open just <strong>PUTTYGEN<\/strong> again, load your key and copy the public part to the clipboard again (see above).<\/p>\n<figure id=\"attachment_435\" aria-describedby=\"caption-attachment-435\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pasted-pub-key.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-435 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pasted-pub-key-300x192.png\" alt=\"\" width=\"300\" height=\"192\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pasted-pub-key-300x192.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pasted-pub-key.png 685w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-435\" class=\"wp-caption-text\">Pasted public key<\/figcaption><\/figure>\n<p><strong>Important: Remove the line-breaks. <\/strong>There should be only spaces between the three parts of your key.<\/p>\n<figure id=\"attachment_436\" aria-describedby=\"caption-attachment-436\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pub-key-with-spaces-only.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-436 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pub-key-with-spaces-only-300x194.png\" alt=\"\" width=\"300\" height=\"194\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pub-key-with-spaces-only-300x194.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pub-key-with-spaces-only.png 680w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-436\" class=\"wp-caption-text\">Public key without line breaks<\/figcaption><\/figure>\n<p>Save the file and exit the editor. For the pico editor you can use <strong>[Ctrl]+[o]<\/strong> to save and <strong>[Ctrl]+[x]<\/strong> to exit. For extra safety you can make sure that both the file and the directory are readable for your user on the remote server only:<\/p>\n<pre><strong>chmod 600 .ssh\/authorized_keys<\/strong>\r\n<strong>chmod 700 .ssh<\/strong><\/pre>\n<p>You can then <strong>exit<\/strong> the SSH session.<\/p>\n<figure id=\"attachment_437\" aria-describedby=\"caption-attachment-437\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/save-and-secure-authorized-keys.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-437 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/save-and-secure-authorized-keys-300x195.png\" alt=\"\" width=\"300\" height=\"195\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/save-and-secure-authorized-keys-300x195.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/save-and-secure-authorized-keys.png 676w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-437\" class=\"wp-caption-text\">Save and make public key readable for you only<\/figcaption><\/figure>\n<h2>Logging in using SSH Keys and Agent<\/h2>\n<p><strong>PAGENT<\/strong> can be used to keep track of your SSH keys and supply them to PuTTY for logins.<\/p>\n<p>The idea is to have the agent process running in the background (in the Windows system tray), so that you only need to unlock your SSH key once using the previously set passphrase, and the agent will keep it (or multiple such keys) in memory and automatically use them to authenticate you when necessary. Some people put <strong>PAGENT<\/strong> in their &#8220;Startup&#8221; folder, others just start it on demand before making the connection with PuTTY.<\/p>\n<p>Start <strong>PAGENT<\/strong> by double-clicking it, and it should automatically appaer in your system tray (usually to the bottom right somewhere next to the date and time). Using a right click, you can add and remove SSH keys. Try doing this with your newly generated key now.<\/p>\n<figure id=\"attachment_438\" aria-describedby=\"caption-attachment-438\" style=\"width: 295px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pagent-task-bar.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-438 size-full\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pagent-task-bar.png\" alt=\"\" width=\"295\" height=\"251\"><\/a><figcaption id=\"caption-attachment-438\" class=\"wp-caption-text\">Add key to PAGENT<\/figcaption><\/figure>\n<p>Select your SSH keyfile and <strong>Open<\/strong> it.<\/p>\n<figure id=\"attachment_439\" aria-describedby=\"caption-attachment-439\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/select-ppk-and-open.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-439 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/select-ppk-and-open-300x188.png\" alt=\"\" width=\"300\" height=\"188\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/select-ppk-and-open-300x188.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/select-ppk-and-open.png 521w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-439\" class=\"wp-caption-text\">Select ppk file and open<\/figcaption><\/figure>\n<p>You can also right-click on the icon in the task bar again to <strong>View<\/strong> all loaded SSH keys.<\/p>\n<figure id=\"attachment_440\" aria-describedby=\"caption-attachment-440\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pagent-key-list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-440 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pagent-key-list-300x219.png\" alt=\"\" width=\"300\" height=\"219\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pagent-key-list-300x219.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/pagent-key-list.png 509w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-440\" class=\"wp-caption-text\">List of SSH keys in PAGENT<\/figcaption><\/figure>\n<p><em>While most of the above steps only need to be done once, this is where daily routine starts.<\/em><\/p>\n<p>Now let&#8217;s start <strong>PUTTY<\/strong> and open a connection to the server again.<br \/>\nEnter your login name and press the return key. You should be authenticated automatically using your SSH key.<\/p>\n<figure id=\"attachment_441\" aria-describedby=\"caption-attachment-441\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/ssh-connection-using-putty-and-pagent.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-441 size-medium\" src=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/ssh-connection-using-putty-and-pagent-300x193.png\" alt=\"\" width=\"300\" height=\"193\" srcset=\"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/ssh-connection-using-putty-and-pagent-300x193.png 300w, https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-content\/uploads\/ssh-connection-using-putty-and-pagent.png 677w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-441\" class=\"wp-caption-text\">Connection via PuTTY and PAGENT using SSH keys<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide is about key-based logins to remote Unix\/Linux-servers from a Windows PC using PuTTY. If you have OpenSSH on your Windows, Mac or Linux computer, things should be a lot easier and you can type commands directly in a Terminal window. Preparation Make sure that PuTTY is available on your computer.&nbsp; This guide uses [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[64,9,56],"tags":[69,41],"_links":{"self":[{"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/posts\/427"}],"collection":[{"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/comments?post=427"}],"version-history":[{"count":5,"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/posts\/427\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/userpage.fu-berlin.de\/~rober\/blog\/wp-json\/wp\/v2\/tags?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}