{"id":27945,"date":"2021-07-16T20:12:39","date_gmt":"2021-07-16T20:12:39","guid":{"rendered":"https:\/\/linuxiac.com\/?p=27945"},"modified":"2022-05-19T13:16:05","modified_gmt":"2022-05-19T13:16:05","slug":"generate-ssh-key-pair","status":"publish","type":"post","link":"https:\/\/linuxiac.com\/generate-ssh-key-pair\/","title":{"rendered":"How to Generate an SSH Key Pair (with Examples)"},"content":{"rendered":"\n<p>Password authentication is the default method most SSH clients use to authenticate with remote servers, but it suffers from potential security vulnerabilities like brute-force login attempts.<\/p>\n\n\n\n<p>An alternative to password authentication is using&nbsp;authentication with SSH key pair, in which you generate an SSH key and store it on your computer.<\/p>\n\n\n\n<p>SSH keys are an easy way to identify trusted computers without involving passwords.&nbsp;They are widely used by network and systems administrators to control servers remotely.<\/p>\n\n\n\n<p>Therefore, the SSH key authentication is more secure than password authentication and arguably more convenient.<\/p>\n\n\n\n<p>The central concept is that instead of a password, one uses a key file that is virtually impossible to guess. You give the public part of your key, and when logging in, it will be used, together with the private key and username, to verify your identity.<\/p>\n\n\n\n<p>The steps below will show you how to generate an SSH key pair and add the public key to the server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"check-for-existing-ssh-keys\">How to Generate an SSH Key Pair on Linux<\/h2>\n\n\n\n<p>When generating SSH keys under Linux, you can use the&nbsp;<code>ssh-keygen<\/code>&nbsp;command. It is a tool for creating new authentication key pairs for SSH.<\/p>\n\n\n\n<p>To generate an SSH key pair, open up the terminal and type in the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">ssh-keygen -t rsa<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Just press enter when it asks for the file, passphrase, or same passphrase. The command generates a pair of keys in the <code>~\/.ssh<\/code> directory by default.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2021\/07\/generate-ssh-key-on-linux.png\"><img decoding=\"async\" width=\"865\" height=\"557\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2021\/07\/generate-ssh-key-on-linux.png\" alt=\"ssh-keygen Generate an SSH Keys on Linux\" class=\"wp-image-39043\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2021\/07\/generate-ssh-key-on-linux.png 865w, https:\/\/linuxiac.com\/wp-content\/uploads\/2021\/07\/generate-ssh-key-on-linux-360x232.png 360w, https:\/\/linuxiac.com\/wp-content\/uploads\/2021\/07\/generate-ssh-key-on-linux-768x495.png 768w, https:\/\/linuxiac.com\/wp-content\/uploads\/2021\/07\/generate-ssh-key-on-linux-480x309.png 480w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/a><\/figure>\n\n\n\n<p>You now have two files:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>id_rsa<\/code><\/strong> (Private Key). The private key is stored on your local computer and should be kept secure, with permissions set so that no other users on your computer can read the file.<\/li>\n\n\n\n<li><strong><code>id_rsa.pub<\/code><\/strong> (Public Key). The public key needs to be placed on the server you intend to log in to. You can freely share your public key with others.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">ls -l \/home\/linuxiac\/.ssh\/<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code lang=\"xml\" class=\"hljs language-css language-xml\"><span class=\"hljs-selector-tag\">drwx------<\/span> 2 <span class=\"hljs-selector-tag\">linuxiac<\/span> <span class=\"hljs-selector-tag\">linuxiac<\/span> 4096 <span class=\"hljs-selector-tag\">Jul<\/span> 16 18<span class=\"hljs-selector-pseudo\">:31<\/span> .\n<span class=\"hljs-selector-tag\">drwxr-xr-x<\/span> 4 <span class=\"hljs-selector-tag\">linuxiac<\/span> <span class=\"hljs-selector-tag\">linuxiac<\/span> 4096 <span class=\"hljs-selector-tag\">Jul<\/span> 16 18<span class=\"hljs-selector-pseudo\">:31<\/span> ..\n<span class=\"hljs-selector-tag\">-rw-------<\/span> 1 <span class=\"hljs-selector-tag\">linuxiac<\/span> <span class=\"hljs-selector-tag\">linuxiac<\/span> 2610 <span class=\"hljs-selector-tag\">Jul<\/span> 16 18<span class=\"hljs-selector-pseudo\">:31<\/span> <span class=\"hljs-selector-tag\">id_rsa<\/span>\n<span class=\"hljs-selector-tag\">-rw-r--r--<\/span> 1 <span class=\"hljs-selector-tag\">linuxiac<\/span> <span class=\"hljs-selector-tag\">linuxiac<\/span>  576 <span class=\"hljs-selector-tag\">Jul<\/span> 16 18<span class=\"hljs-selector-pseudo\">:31<\/span> <span class=\"hljs-selector-tag\">id_rsa<\/span><span class=\"hljs-selector-class\">.pub<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can place the public key on any server and then connect to the server using <code>ssh<\/code>.  When the public and private keys match up, the SSH server grants access without the need for a password.<\/p>\n\n\n\n<p>If you are unsure how to do it, I recommend going through our short and easy-to-follow guide, &#8220;<a href=\"https:\/\/linuxiac.com\/ssh-login-without-password\/\">How to Setup SSH Login Without Password<\/a>.&#8221;<\/p>\n\n\n\n<p>You can generate an even larger SSH key with the <code>-b<\/code> option for increased security.<\/p>\n\n\n\n<p>The <code>-b<\/code> flag instructs <code>ssh-keygen<\/code> to increase the number of bits used to create the key pair and is suggested for additional security. For example, for 4096 bits, do:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">ssh-keygen -t rsa -b 4096<\/code><\/span><\/pre>\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>In this article, you have learned how to generate SSH key pairs using&nbsp;<code>ssh-keygen<\/code>. SSH keys have numerous advantages over passwords:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Increased security<\/strong>: They are nearly impossible to brute force or guess.<\/li>\n\n\n\n<li><strong>Ease of management<\/strong>: No more creating and changing random passwords.<\/li>\n\n\n\n<li><strong>Automated tasks<\/strong>: Because you don\u2019t need to type your password every time, it&#8217;s easier to automate tasks that require SSH.<\/li>\n<\/ul>\n\n\n\n<p>For more about the&nbsp;<code>ssh-keygen<\/code>&nbsp;command in Linux, consult its&nbsp;<a href=\"https:\/\/linux.die.net\/man\/1\/ssh-keygen\" target=\"_blank\" rel=\"noreferrer noopener\">manual page<\/a>.<\/p>\n\n\n\n<p><strong>Related: <a href=\"https:\/\/linuxiac.com\/ssh-to-port-other-than-22\/\">SSH to Port Other Than 22: How to Do It (with Examples)<\/a><\/strong><\/p>\n\n\n\n<p>Feel free to leave a comment if you have any questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to generate an SSH key pair on your computer, which you can then use to authenticate your connection to a remote server.<\/p>\n","protected":false},"author":10,"featured_media":27942,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,4417],"tags":[3374,64],"class_list":["post-27945","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","category-linux-knowledge","tag-security","tag-ssh"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6},"post_title_panel":"","has_hero_section":"default","bd7510abe191b6e3db41fda26ecde3d7":"","hero_section":"type-2","hero_elements":[{"id":"custom_title","enabled":true,"heading_tag":"h1","title":"Home","__id":"TiJvISZwtunjrAPkP8-9d"},{"id":"custom_description","enabled":false,"description_visibility":{"desktop":true,"tablet":true,"mobile":false},"__id":"Ay2dnwetT3NLZGEbJy70d"},{"id":"custom_meta","enabled":true,"meta_elements":[{"id":"author","enabled":true,"label":"By","has_author_avatar":"yes","avatar_size":50,"__id":"F7ZNRPZpzNhOTG8pZv_5d"},{"id":"post_date","enabled":false,"label":"On","date_format_source":"default","date_format":"M j, Y","__id":"XY6uQPyteCCfYwmcYi2PB"},{"id":"updated_date","enabled":true,"label":"Updated","date_format_source":"default","date_format":"M j, Y","__id":"PAlF6U4V_d9PJ_Hyua7u5"},{"id":"categories","enabled":false,"label":"In","style":"simple","__id":"sd8zeyh1oDd0YVAxh2uI6"},{"id":"comments","enabled":true,"__id":"QigEn0cWJdhEUnhi59ziv"}],"page_meta_elements":{"joined":true,"articles_count":true,"comments":true},"__id":"9jIhLOXLGAqa6afjQSWee","meta_type":"label","meta_divider":"circle"},{"id":"breadcrumbs","enabled":false,"__id":"PtMJprxwXdSMT1sN7oikp"}],"b92bbf19cb223571822ec00dd3c0a2ee":"","hero_alignment1":"CT_CSS_SKIP_RULE","hero_margin":40,"hero_alignment2":"center","hero_vertical_alignment":"center","8daab8c84515be4596dd35db734a4fae":"","hero_structure":"normal","7d4b9ac272816dd75f69130c2375733a":"","page_title_bg_type":"custom_image","custom_hero_background":{"attachment_id":42888,"url":"https:\/\/linuxiac.com\/wp-content\/uploads\/2021\/07\/generate-ssh-key-header-1024x248.png"},"parallax":{"desktop":false,"tablet":false,"mobile":false},"f53e4ffbc63dc0a8bf40a82c31c2a6b8":"","hero_height":"250px","pageTitleFont":{"family":"Default","variation":"Default","size":{"desktop":"2.8em","tablet":"2.8em","mobile":"2.8em","__changed":[]},"line-height":"CT_CSS_SKIP_RULE","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"pageTitleFontColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageMetaFont":{"family":"Default","variation":"n6","size":"12px","line-height":"1.3","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"uppercase","text-decoration":"CT_CSS_SKIP_RULE"},"pageMetaFontColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"hover":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"page_meta_button_type_font_colors":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"hover":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"page_meta_button_type_background_colors":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"hover":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageExcerptFont":{"family":"Default","variation":"Default","size":"CT_CSS_SKIP_RULE","line-height":"CT_CSS_SKIP_RULE","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"pageExcerptColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"breadcrumbsFont":{"family":"Default","variation":"Default","size":"CT_CSS_SKIP_RULE","line-height":"CT_CSS_SKIP_RULE","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"breadcrumbsFontColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"initial":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"hover":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageTitleOverlay":{"background_type":"color","background_pattern":"type-1","background_image":{"attachment_id":null,"x":0,"y":0},"gradient":"linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)","background_repeat":"repeat","background_size":"auto","background_attachment":"scroll","patternColor":{"default":{"color":"#e5e7ea"}},"overlayColor":{"default":{"color":"CT_CSS_SKIP_RULE"}},"backgroundColor":{"default":{"color":"CT_CSS_SKIP_RULE"}}},"pageTitleBackground":{"background_type":"color","background_pattern":"type-1","background_image":{"attachment_id":null,"x":0,"y":0},"gradient":"linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)","background_repeat":"repeat","background_size":"auto","background_attachment":"scroll","patternColor":{"default":{"color":"#e5e7ea"}},"overlayColor":{"default":{"color":"CT_CSS_SKIP_RULE"}},"backgroundColor":{"default":{"color":"var(--theme-palette-color-6)"}}},"pageTitlePadding":{"top":"50px","bottom":"50px","left":"auto","right":"auto","linked":true},"7b01945cf3063297b60c42617ea50cc8":"","page_structure_type":"default","4d91dd104f432d97de22cdb1112b00a8":"","content_style_source":"inherit","content_style":"wide","f3763deab1c7ca2e97a8925ddbeb06aa":"","vertical_spacing_source":"inherit","content_area_spacing":"both","background":{"background_type":"color","background_pattern":"type-1","background_image":{"attachment_id":null,"x":0,"y":0},"gradient":"linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)","background_repeat":"repeat","background_size":"auto","background_attachment":"scroll","patternColor":{"default":{"color":"#e5e7ea"}},"overlayColor":{"default":{"color":"CT_CSS_SKIP_RULE"}},"backgroundColor":{"default":{"color":"CT_CSS_SKIP_RULE"}}},"content_background":{"background_type":"color","background_pattern":"type-1","background_image":{"attachment_id":null,"x":0,"y":0},"gradient":"linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)","background_repeat":"repeat","background_size":"auto","background_attachment":"scroll","patternColor":{"default":{"color":"#e5e7ea"}},"overlayColor":{"default":{"color":"CT_CSS_SKIP_RULE"}},"backgroundColor":{"default":{"color":"var(--theme-palette-color-8)"}}},"content_boxed_shadow":{"inherit":false,"blur":18,"spread":-6,"v_offset":12,"h_offset":0,"inset":false,"enable":true,"color":{"color":"rgba(34, 56, 101, 0.04)"}},"boxed_content_spacing":{"desktop":{"top":"40px","bottom":"40px","left":"40px","right":"40px","linked":true},"tablet":{"top":"35px","bottom":"35px","left":"35px","right":"35px","linked":true},"mobile":{"top":"20px","bottom":"20px","left":"20px","right":"20px","linked":true}},"content_boxed_radius":{"top":"3px","bottom":"3px","left":"3px","right":"3px","linked":true},"2258b0505a414ac3ba931dff7624c300":"","disable_featured_image":"no","disable_post_tags":"no","disable_share_box":"no","disable_author_box":"no","disable_posts_navigation":"no","disable_subscribe_form":"no","63977d714d0d984608ca8172912abb50":"","disable_related_posts":"no","disable_header":"no","disable_footer":"no","10c3a950c21e1c9a63a323f1a9c747a9":"","header_footer_scripts_panel":"","header_scripts":"","6f43b91e0d46bf0e9385b7f15c7ccc62":"","header_after_body_scripts":"","9de8ed11e61b276a29dd82f02b2ecb7c":"","footer_scripts":""},"_links":{"self":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/27945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/comments?post=27945"}],"version-history":[{"count":0,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/27945\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media\/27942"}],"wp:attachment":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media?parent=27945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/categories?post=27945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/tags?post=27945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}