{"id":278571,"date":"2021-06-18T19:00:00","date_gmt":"2021-06-18T16:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-add-your-ec2-pem-file-to-your-ssh-keychain-cloudsavvy-it\/"},"modified":"2021-06-18T19:00:00","modified_gmt":"2021-06-18T16:00:00","slug":"how-to-add-your-ec2-pem-file-to-your-ssh-keychain-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-add-your-ec2-pem-file-to-your-ssh-keychain-cloudsavvy-it\/","title":{"rendered":"#How to Add Your EC2 PEM File to Your SSH Keychain \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6aee7d8998185\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6aee7d8998185\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-your-ec2-pem-file-to-your-ssh-keychain-cloudsavvy-it\/#How_To_Use_Your_PEM_File\" >How To Use Your PEM File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-your-ec2-pem-file-to-your-ssh-keychain-cloudsavvy-it\/#Store_SSH_Keys_In_macOS_Keychain\" >Store SSH Keys In macOS Keychain<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-add-your-ec2-pem-file-to-your-ssh-keychain-cloudsavvy-it\/#Replace_id_rsa_With_Your_New_Key\" >Replace id_rsa With Your New Key<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Add Your EC2 PEM File to Your SSH Keychain \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-1767\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/821c14ca.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When you create a new instance in EC2, you\u2019ll be given a PEM file that acts as your access key. You\u2019ll have to use this to SSH into the server, so you\u2019ll want to add it to your keychain for easy access.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_To_Use_Your_PEM_File\"><\/span>How To Use Your PEM File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can use PEM files manually by adding the <code>-i<\/code>\u00a0flag to ssh:<\/p>\n<pre>ssh -i keyfile.pem user@host<\/pre>\n<p>This is unwieldy to type every time, so there are a few ways to fix this.<\/p>\n<p>The simplest method would be to add your own public keys to your EC2 instance, and ignore the PEM file for all future logins. Your public key is usually stored in <code>~\/.ssh\/id_rsa.pub<\/code>, so you\u2019ll want to copy that into the <code>~\/.ssh\/authorized_keys<\/code>\u00a0file on the server. If you\u2019re a one man team simply running one server, and don\u2019t mind doing this each time, this is all you have to do.<\/p>\n<p>However, you\u2019ll have to go through this process each time you create a new instance. But with PEM files, you can reuse them between instances. Also, they\u2019re independent of your personal private keys, so you can give them to other people who need ssh access.<\/p>\n<p>The <code>ssh-add<\/code>\u00a0command will store a key in your\u00a0SSH agent\u00a0until you log out:<\/p>\n<pre>ssh-add ~\/keyfile.pem<\/pre>\n<p>However, you\u2019ll need to run this on every reboot, so it\u2019s not ideal. You can add this to your <code>~\/.bashrc<\/code>\u00a0or <code>~\/.bash_profile<\/code>\u00a0to run every time you boot up the terminal, which solves the issue. Make sure to redirect output to <code>\/dev\/null<\/code>\u00a0to silence the command, or you\u2019ll see \u201cIdentity Added\u201d every time you open the terminal.<\/p>\n<pre>ssh-add ~\/keyfile.pem &gt;\/dev\/null 2&gt;&amp;1<\/pre>\n<p><strong>RELATED:<\/strong> <strong><em>What is SSH Agent Forwarding and How Do You Use It?<\/em><\/strong><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Store_SSH_Keys_In_macOS_Keychain\"><\/span>Store SSH Keys In macOS Keychain<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you\u2019re on macOS, you can store additional SSH keys in the macOS Keychain. Open up <code>~\/.ssh\/config<\/code>\u00a0and add the following lines:<\/p>\n<pre>Host *&#13;\nUseKeychain yes<\/pre>\n<p>You can now add keys with<\/p>\n<pre>ssh-add -K ~\/keyfile.pem<\/pre>\n<p>The keys will be stored in the keychain and persist across reboots. They will be automatically loaded just like <code>~\/id_rsa<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Replace_id_rsa_With_Your_New_Key\"><\/span>Replace id_rsa With Your New Key<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>While this option does work, it\u2019s not really something we recommend. But, if for some reason you <em>really<\/em> want your AWS private key to be your new personal private key, you can replace <code>id_rsa<\/code>\u00a0with the PEM file from AWS. <code>id_rsa<\/code>\u00a0is loaded by default, so you\u2019ll default to using this key for everything.<\/p>\n<p>Make <em>absolutely certain<\/em> you aren\u2019t using your current private key for anything (SSH to other servers, GitHub, etc). Even if you think you aren\u2019t, <strong>you should back up your current SSH keys before proceeding<\/strong>:<\/p>\n<pre>mv ~\/.ssh\/id_rsa ~\/.ssh\/id_rsa_old&#13;\nmv ~\/.ssh\/id_rsa.pub ~\/.ssh\/id_rsa_old.pub<\/pre>\n<p>The AWS PEM file needs to be converted to PKCS8 format to be used as a private key. You can do this with OpenSSL:<\/p>\n<pre>openssl pkey &lt; keyfile.pem &gt; keyfile.pkcs8<\/pre>\n<p>Then, you\u2019ll need to generate the corresponding public key, again using OpenSSL<\/p>\n<pre>openssl rsa -in keyfile.pkcs8 -pubout &gt; keyfile.pub<\/pre>\n<p>Then, making sure you\u2019ve backed up your old <code>id_rsa<\/code>, you can replace them with your new ones:<\/p>\n<pre>mv keyfile.pkcs8 ~\/.ssh\/id_rsa&#13;\nmv keyfile.pub ~\/.ssh\/id_rsa.pub<\/pre>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/1795\/how-to-add-your-ec2-pem-file-to-your-ssh-keychain\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Add Your EC2 PEM File to Your SSH Keychain \u2013 CloudSavvy IT&#8221; When you create a new instance in EC2, you\u2019ll be given a PEM file that acts as your access key. You\u2019ll have to use this to SSH into the server, so you\u2019ll want to add it to your keychain for easy&#8230;<\/p>\n","protected":false},"author":1,"featured_media":278572,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/821c14ca.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-278571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/278571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=278571"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/278571\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/278572"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=278571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=278571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=278571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}