<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vuong Tru</title>
    <description>The latest articles on Forem by Vuong Tru (@trubavuong).</description>
    <link>https://forem.com/trubavuong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F392052%2Fc93276b4-ee72-4561-b2a6-98027efdd3b1.jpg</url>
      <title>Forem: Vuong Tru</title>
      <link>https://forem.com/trubavuong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/trubavuong"/>
    <language>en</language>
    <item>
      <title>Best way to generate and manage SSH keys</title>
      <dc:creator>Vuong Tru</dc:creator>
      <pubDate>Wed, 17 Jun 2020 16:20:02 +0000</pubDate>
      <link>https://forem.com/trubavuong/how-to-generate-and-manage-ssh-keys-in-a-neat-way-2cik</link>
      <guid>https://forem.com/trubavuong/how-to-generate-and-manage-ssh-keys-in-a-neat-way-2cik</guid>
      <description>&lt;p&gt;Connecting to a remote server using an SSH key is quite simple. However, when you have a lot of keys or multiple GitHub accounts, problems may arise. In this article, I am going to show you how to generate and manage keys in a neat way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;1. What is SSH?&lt;/li&gt;
&lt;li&gt;2. SSH key&lt;/li&gt;
&lt;li&gt;3. Generating a new SSH key

&lt;ul&gt;
&lt;li&gt;Step #1: Run "ssh-keygen" command&lt;/li&gt;
&lt;li&gt;Step #2: Enter the private key's location&lt;/li&gt;
&lt;li&gt;Step #3: Enter a passphrase&lt;/li&gt;
&lt;li&gt;Step #4: Done&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;4. Potential problems due to multiple SSH keys

&lt;ul&gt;
&lt;li&gt;Problem #1: Too many authentication failures&lt;/li&gt;
&lt;li&gt;Problem #2: Multiple GitHub accounts&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;5. SSH config file&lt;/li&gt;

&lt;li&gt;6. Benefits

&lt;ul&gt;
&lt;li&gt;Benefit #1: Connecting to a server using its alias&lt;/li&gt;
&lt;li&gt;Benefit #2: Dealing with multiple GitHub accounts&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;7. Conclusion&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. What is SSH?
&lt;/h2&gt;

&lt;p&gt;SSH (Secure Shell) is an authenticated and encrypted network protocol used for remote communication between machines.&lt;/p&gt;

&lt;p&gt;SSH supports various authentication methods. Password authentication is the easiest method, but it suffers from security vulnerabilities, such as brute force attacks. Another method is public-key authentication, which is more secure, and for me, more convenient.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. SSH key
&lt;/h2&gt;

&lt;p&gt;An SSH key, or an SSH key pair, is a pair of keys: a public key and a private key.&lt;/p&gt;

&lt;p&gt;The public key:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Usually named &lt;code&gt;id_rsa.pub&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Acting as a public lock&lt;/li&gt;
&lt;li&gt;Placed on the SSH server that you want to log into&lt;/li&gt;
&lt;li&gt;Used to encrypt data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The private key:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Usually named &lt;code&gt;id_rsa&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Acting as a secret key&lt;/li&gt;
&lt;li&gt;Securely stored on your machine only&lt;/li&gt;
&lt;li&gt;Used to decrypt data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By default, these keys are stored in the &lt;code&gt;~/.ssh&lt;/code&gt; directory. You can also have multiple key pairs on your machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Generating a new SSH key
&lt;/h2&gt;

&lt;p&gt;To create a pair of keys, you can use the &lt;code&gt;ssh-keygen&lt;/code&gt; tool with just a few steps. Let's do it!&lt;/p&gt;

&lt;h3&gt;
  
  
  Step #1: Run "ssh-keygen" command
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="nt"&gt;-b&lt;/span&gt; 4096 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s2"&gt;"Your Name &amp;lt;your_email@example.com&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-C&lt;/code&gt; option is used as a note to specify who/when/where the key was generated. Although this option is optional, I highly recommend filling it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step #2: Enter the private key's location
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Generating public/private rsa key pair.
Enter file &lt;span class="k"&gt;in &lt;/span&gt;which to save the key &lt;span class="o"&gt;(&lt;/span&gt;/home/&amp;lt;user&amp;gt;/.ssh/id_rsa&lt;span class="o"&gt;)&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;Type]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step #3: Enter a passphrase
&lt;/h3&gt;

&lt;p&gt;A passphrase, an extra security layer, is used to encrypt your private key. You can leave it blank and press enter to skip creating a passphrase, but I strongly recommend setting up the one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Enter passphrase &lt;span class="o"&gt;(&lt;/span&gt;empty &lt;span class="k"&gt;for &lt;/span&gt;no passphrase&lt;span class="o"&gt;)&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;Type]
Enter same passphrase again: &lt;span class="o"&gt;[&lt;/span&gt;Re-type]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step #4: Done
&lt;/h3&gt;

&lt;p&gt;You can check the newly generated keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; ~/.ssh
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/id_rsa.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Potential problems due to multiple SSH keys
&lt;/h2&gt;

&lt;p&gt;If you have only one SSH key, congratulations, you have nothing to worry about. But what if you have more keys, for example, to access more remote servers?&lt;/p&gt;

&lt;p&gt;I will show you a few potential problems right away.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem #1: Too many authentication failures
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-i&lt;/span&gt; ~/.ssh/id_rsa_vps user@1.2.3.4

Received disconnect from 1.2.3.4 port 22:2: Too many authentication failures
Disconnected from 1.2.3.4 port 22
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, I failed to connect to the server with the corresponding private key, and the error was "Too many authentication failures".&lt;/p&gt;

&lt;p&gt;What?&lt;/p&gt;

&lt;p&gt;That means the SSH client tried a lot of other keys, and the authentication process failed before the key I specified was used.&lt;/p&gt;

&lt;p&gt;Let me explain.&lt;/p&gt;

&lt;p&gt;The SSH agent tracks private keys and their passphrases. When connecting to a server, the SSH client uses all the keys in the agent and the specified key to compose a key list to try each by each. The specified key will be added to the top of the list if it has been already tracked by the agent. Otherwise, it will be added to the end of the list.&lt;/p&gt;

&lt;p&gt;Not too complicated, right?&lt;/p&gt;

&lt;p&gt;Okay. The fact that the key I specified, &lt;code&gt;~/.ssh/id_rsa_vps&lt;/code&gt;, has not been tracked. Thus, there are at least 3 solutions as follows:&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution #1.1: Adding the private key to the SSH agent by using the "ssh-add" tool
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh-add ~/.ssh/id_rsa_vps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution #1.2: Ignoring the key list in the SSH agent by providing the SSH option "IdentitiesOnly=yes"
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="nv"&gt;IdentitiesOnly&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;yes&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; ~/.ssh/id_rsa_vps user@1.2.3.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution #1.3: Using the SSH config file
&lt;/h4&gt;

&lt;p&gt;Keep reading. I will describe it in section 5.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem #2: Multiple GitHub accounts
&lt;/h3&gt;

&lt;p&gt;This is an example of GitHub, but it is similar to any Git servers, such as GitLab, BitBucket, etc.&lt;/p&gt;

&lt;p&gt;Suppose you have multiple GitHub accounts (&lt;code&gt;personal&lt;/code&gt; and &lt;code&gt;team&lt;/code&gt;), and you have added the corresponding key to each account. When you try to clone a repository from one account, you may see the following error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git clone git@github.com:personal/repo.git

Cloning into &lt;span class="s1"&gt;'repo'&lt;/span&gt;...
ERROR: Repository not found.
fatal: Could not &lt;span class="nb"&gt;read &lt;/span&gt;from remote repository.

Please make sure you have the correct access rights
and the repository exists.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: if you do not see any errors, try the other account's repository.&lt;/p&gt;

&lt;p&gt;Let me explain.&lt;/p&gt;

&lt;p&gt;First, you cannot add the same key to multiple GitHub accounts. Second, when you clone a repository using SSH, the SSH client sends each key in the list (that I have just described in the "Problem #1" section) to the GitHub server.&lt;br&gt;
Depending on the key list order, &lt;code&gt;id_rsa_personal&lt;/code&gt; or &lt;code&gt;id_rsa_team&lt;/code&gt; will be accepted by the server. You know, if the accepted one does not belong to the owner of the repository, the permission error will be returned.&lt;/p&gt;

&lt;p&gt;Okay. The solution is right below.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. SSH config file
&lt;/h2&gt;

&lt;p&gt;Forget the SSH agent. Using the SSH config file is a better way to manage multiple SSH keys. Believe me, the above potential problems will never happen again.&lt;/p&gt;

&lt;p&gt;You can find the config file at &lt;code&gt;~/.ssh/config&lt;/code&gt;, or just create one.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/config

Host vps
  HostName 1.2.3.4
  Port 22
  User user
  IdentityFile ~/.ssh/id_rsa_vps
  IdentitiesOnly &lt;span class="nb"&gt;yes

&lt;/span&gt;Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github_personal
  IdentitiesOnly &lt;span class="nb"&gt;yes

&lt;/span&gt;Host github-team
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github_team
  IdentitiesOnly &lt;span class="nb"&gt;yes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me explain.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Host: I used as an alias of the host&lt;/li&gt;
&lt;li&gt;HostName: the real hostname to log into&lt;/li&gt;
&lt;li&gt;Port: the port number&lt;/li&gt;
&lt;li&gt;User: the user used to log into&lt;/li&gt;
&lt;li&gt;IdentityFile: the private key file&lt;/li&gt;
&lt;li&gt;IdentitiesOnly: setting it to "yes" that tells the SSH client to only use the private keys configured in the SSH config files, so the "Problem #1" will be resolved!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Benefits
&lt;/h2&gt;

&lt;p&gt;Let's discuss the benefits of using the SSH config file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefit #1: Connecting to a server using its alias
&lt;/h3&gt;

&lt;p&gt;You do not have to specify user, host, or port every time to connect to a server via SSH.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh vps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome!&lt;/p&gt;

&lt;p&gt;An example of using SCP to copy a file from the remote server to the local machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;scp vps:/path/to/the/remote/file /home/&amp;lt;user&amp;gt;/Downloads
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Benefit #2: Dealing with multiple GitHub accounts
&lt;/h3&gt;

&lt;p&gt;You can clone the repository that belongs to the corresponding account. For example, to clone the repository of the &lt;code&gt;personal&lt;/code&gt; account, I replaced &lt;code&gt;github.com&lt;/code&gt; by the alias &lt;code&gt;github-personal&lt;/code&gt; in the config file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git clone git@github-personal:personal/repo.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Conclusion
&lt;/h2&gt;

&lt;p&gt;Managing multiple SSH keys by using the SSH config file is not complicated. You can explore &lt;a href="https://linux.die.net/man/5/ssh_config" rel="noopener noreferrer"&gt;even more SSH options&lt;/a&gt; if you like. I hope this article is helpful to you.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>devops</category>
      <category>tutorial</category>
      <category>git</category>
    </item>
  </channel>
</rss>
