{"id":743,"date":"2023-09-27T01:53:00","date_gmt":"2023-09-27T01:53:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=743"},"modified":"2023-09-27T01:53:00","modified_gmt":"2023-09-27T01:53:00","slug":"generate-ssh-key-pair","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=743","title":{"rendered":"How to Generate SSH Key (Public\/Private) &#8211; Beginners Guide"},"content":{"rendered":"<p>SSH (Secure Shell) Key is the secure way to log in to Linux servers as compared to passwords.<\/p>\n<p>Every DevOps engineer has to use SSH key-based authentication when working with Linux servers. Also, most cloud platforms offer and recommend SSH key-based server authentication for enhanced security<\/p>\n<p>This blog post aims to provide a detailed, step-by-step guide on how to create an SSH key pair for authenticating Linux servers and applications that support SSH protocol using <a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/ssh-keygen.1.html?ref=devopscube.com\" rel=\"noreferrer noopener\">SSH-keygen.<\/a><\/p>\n<h2 id=\"ssh-keygen\">SSH-Keygen<\/h2>\n<p><strong><code>ssh-keygen<\/code><\/strong> is the utility to create SSH keys. It is part of every Linux and MAC system.<\/p>\n<p>You can use the man command below to understand the <strong><code>ssh-keygen<\/code><\/strong> utility and all available options.<\/p>\n<pre><code>man ssh-keygen<\/code><\/pre>\n<p>Or you can refer to the <a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/ssh-keygen.1.html?ref=devopscube.com\" rel=\"noreferrer noopener\">ssh-keygen online man page<\/a><\/p>\n<p>Let&#8217;s look at different ways and options to generate SSH keys.<\/p>\n<h2 id=\"steps-to-create-an-ssh-key\">Steps to Create an SSH Key<\/h2>\n<p>Follow the steps given below to create an SSH key.<\/p>\n<h3 id=\"step-1-open-the-terminal\">Step 1: Open the Terminal<\/h3>\n<p>Open the workstation terminal if you are using a laptop to Desktop.<\/p>\n<p>If you are using a headless server, proceed to the next step.<\/p>\n<h3 id=\"step-2-generate-the-key-pair\">Step 2: Generate the Key Pair<\/h3>\n<p>Execute the following <strong><code>ssh-keygen<\/code><\/strong> command to generate the SSH key pair. It generates and saves the Keys in the default <strong><code>$HOME\/.ssh<\/code><\/strong> location. By default,  the private key is named as <strong><code>id_rsa<\/code><\/strong> , and the public key is named as <strong><code>id_rsa.pub<\/code><\/strong><\/p>\n<pre><code>ssh-keygen<\/code><\/pre>\n<h3 id=\"step-3-save-the-key\">Step 3: Save the Key<\/h3>\n<p>The <strong>ssh-keygen<\/strong> command will prompt for the following options.<\/p>\n<ol>\n<li><strong>Enter the file in which to save the key<\/strong>:- Local path of the SSH private key to be saved. If you don&#8217;t specify any location, it gets stored in the default SSH location. ie, <strong><code>$HOME\/.ssh<\/code><\/strong><\/li>\n<li><strong>Enter passphrase<\/strong>: A passphrase is used to protect the SSH private key. You can leave this empty. If you choose to add a passphrase, you will have to enter it again.<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-28.png\" class=\"kg-image\" alt=\"SSH key generation\" loading=\"lazy\" width=\"532\" height=\"546\"><\/figure>\n<h3 id=\"step-3-key-created\">Step 3: Key Created<\/h3>\n<p>You&#8217;ve successfully created an SSH key pair. You&#8217;ll find two files: <code>id_rsa<\/code> (private key) and <code>id_rsa.pub<\/code> (public key), usually in the <code>~\/.ssh\/<\/code> directory.<\/p>\n<h2 id=\"generate-ssh-keys-with-custom-options\">Generate SSH Keys With Custom Options<\/h2>\n<p>Let&#8217;s take a look at an example <code>ssh-keygen<\/code> command to generate SSH keys with custom options.<\/p>\n<p>The following command generates an SSH key named ssh-key in the <code>$HOME\/.ssh<\/code> location with username <code>vagrant<\/code> with -C flag and passphrase <code>mysecret<\/code> with <code>-q -P<\/code> flag.<\/p>\n<pre><code>ssh-keygen -t rsa -f ~\/.ssh\/ssh-key -C vagrant -b 4096 -q -P \"mysecret\"<\/code><\/pre>\n<p>Let&#8217;s understand the flags.<\/p>\n<ol>\n<li><strong>-t rsa<\/strong>: It is the ssh key algorithm. It is the default algorithm used by <code>ssh-keygen<\/code>.<\/li>\n<li><strong>-f :<\/strong> keyfile name.<\/li>\n<li><strong>-q -P: <\/strong> To add passphrase without prompt<\/li>\n<li><strong>-b:<\/strong> Key Encryption Level. The default is 2048 bits<\/li>\n<li><strong>-C:<\/strong> To set the comment in the last line of the public key. It is typically used to replace the default username set by the command. You can also use this flag to set the server username.<\/li>\n<\/ol>\n<p>If you add the Linux username to the key file with <code>-C<\/code> , you can directly perform SSH without specifying the username in the SSH command.<\/p>\n<p>For example,<\/p>\n<pre><code>ssh -i ~\/.ssh\/ssh-key 192.81.209.247<\/code><\/pre>\n<p>If you don&#8217;t want a passphrase and create the keys without a passphrase prompt, you can use the flag <code>-q -N<\/code> as shown below.<\/p>\n<pre><code>ssh-keygen -t rsa -f ~\/.ssh\/ssh-key -C vagrant -b 2048 -q -N \"\"<\/code><\/pre>\n<h2 id=\"how-to-use-your-ssh-key\">How to Use Your SSH Key<\/h2>\n<p>Now that you have created the SSH key, you can use it as a server for SSH authentication.<\/p>\n<p>Here is how you can do it.<\/p>\n<p>Use <code>ssh-copy-id<\/code> command or manually place the id_rsa<code>.pub<\/code> file content into the <code>~\/.ssh\/authorized_keys<\/code> file on the remote server.<\/p>\n<p>Use the ssh command as given below<\/p>\n<pre><code>ssh username@remote_server<\/code><\/pre>\n<p>If you have saved the SSH key with a different name other than id_rsa to save it to a different location, use the following format.<\/p>\n<pre><code>ssh -i \/path\/to\/private-key-file username@remote_server<\/code><\/pre>\n<p>For example,<\/p>\n<pre><code>ssh -i ~\/.ssh\/ec2-server.pem ubuntu@34.56.76.34<\/code><\/pre>\n<h2 id=\"supported-ssh-key-algorithms\">Supported SSH key algorithms<\/h2>\n<p>SSH (Secure Shell) supports multiple key algorithms for better security. The following are the supported SSH key algorithms.<\/p>\n<ol>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/RSA_(cryptosystem)?ref=devopscube.com\" rel=\"noreferrer noopener\">Rivest-Shamir-Adleman (RSA):<\/a><strong> <\/strong>Generally considered secure with key lengths of at least 2048 bits.<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Digital_Signature_Algorithm?ref=devopscube.com\" rel=\"noreferrer noopener\">Digital Signature Algorithm (DSA):<\/a> Limited to 1024-bit key length, making it less secure.<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Elliptic_Curve_Digital_Signature_Algorithm?ref=devopscube.com\" rel=\"noreferrer noopener\">Elliptic Curve Digital Signature Algorithm (ecdsa):<strong> <\/strong><\/a>Provides high security with shorter key lengths (256-bit and above).<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/EdDSA?ref=devopscube.com\" rel=\"noreferrer noopener\">Ed25519 <\/a>&#8211; EdDSA signature scheme using SHA-512 (SHA-2) and Curve25519. It offers High security with a 256-bit key length.<\/li>\n<\/ol>\n<p>Choosing the algorithm depends on your security requirements. One real-world example is the Amazon ec2 ssh key pair. It supports ED25519 and 2048-bit SSH-2 RSA keys for Linux instances.<\/p>\n<p>To check the algorithm of an existing key you can use the following command.<\/p>\n<pre><code>ssh-keygen -l -f ~\/.ssh\/id_rsa<\/code><\/pre>\n<h2 id=\"ssh-key-faqs\">SSH Key FAQs<\/h2>\n<h3 id=\"how-do-i-manually-generate-ssh-keys\">How do I manually generate SSH keys?<\/h3>\n<p>You can manually generate the SSH key using the ssh-keygen command. It creates the public and private in the $HOME\/.ssh location.<\/p>\n<h3 id=\"is-it-possible-to-use-ssh-keygen-to-create-an-ssh-key-without-a-passphrase\">Is it possible to use ssh-keygen to create an SSH key without a passphrase?<\/h3>\n<p>Yes. If you leave the passphrase prompt empty, the ssh keys get generated without any password. You can also pass the flag -q -N &#8220;&#8221; to the ssh-keygen command to avoid the prompt.<\/p>\n<h3 id=\"what-if-i-lose-my-ssh-key\">What if I lose my SSH key?<\/h3>\n<p>If you lose your private key, remove its corresponding public key from your server&#8217;s <code>authorized_keys<\/code> file and create a new key pair. It is recommended to save the SSH keys in a secret management tool.<\/p>\n<h3 id=\"can-i-use-the-same-ssh-key-for-multiple-servers\">Can I use the same SSH key for multiple servers?<\/h3>\n<p>Yes, you can use the same public key on multiple servers. You just need to add it to each server&#8217;s <code>authorized_keys<\/code> file under ~\/.ssh directory. <\/p>\n<h3 id=\"how-to-create-an-ssh-key-online\">How to create an SSH key online?<\/h3>\n<p>It is advisable not to use any online services to create SSH keys. As a standard security best practice, use the ssh-keygen command to create the SSH key.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this guide, we looked at important commands to create SSH public\/private key pairs. It adds a critical layer of security to your Linux systems.<\/p>\n<p>When it comes to SSH keys, always follow the security best practices to avoid private key misuse.<\/p>\n<p>If you are learning Linux, check out the <a href=\"https:\/\/devopscube.com\/list-linux-networking-troubleshooting-and-commands-beginners\/\" rel=\"noreferrer noopener\">Best Linux Networking and Troubleshooting Commands<\/a>.<\/p>\n<p>Also, If you are starting your journey as a DevOps Engineer, check out my <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\" rel=\"noreferrer noopener\">DevOps Engineer Roadmap<\/a>.<\/p>\n<blockquote><p><strong>Note<\/strong>: Always follow best security practices when dealing with SSH keys to ensure your systems remain secure.<\/p><\/blockquote>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/generate-ssh-key-pair\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Generate SSH Key (Public\/Private) &#8211; Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/generate-ssh-key-pair\/<\/p>\n","protected":false},"author":1,"featured_media":744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=743"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/744"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}