<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>The Contra Code</title><link href="https://blog.contracode.com/" rel="alternate"></link><link href="https://blog.contracode.com/feeds/all.atom.xml" rel="self"></link><id>https://blog.contracode.com/</id><updated>2018-11-01T18:58:00-04:00</updated><entry><title>The Definitive Guide to SSH Authentication on GitLab</title><link href="https://blog.contracode.com/posts/2018/Jun/21/definitive-guide-to-ssh-on-gitlab/" rel="alternate"></link><published>2018-06-21T00:00:00-04:00</published><updated>2018-11-01T18:58:00-04:00</updated><author><name>Jared Contrascere</name></author><id>tag:blog.contracode.com,2018-06-21:/posts/2018/Jun/21/definitive-guide-to-ssh-on-gitlab/</id><summary type="html">

&lt;p&gt;While there are good &lt;a href="https://docs.gitlab.com/ee/ssh/"&gt;instructions&lt;/a&gt; in GitLab's documentation that explain how to set up key-based SSH authentication, these instructions fall short of a clear, concise, and complete solution. In addition to this, if you wish to use multiple SSH identities for specific services or environments, there are &lt;em&gt;even more&lt;/em&gt; hurdles to overcome.&lt;/p&gt;
&lt;p&gt;This post will help you to set up SSH key-based login on GitLab, so you won't have to enter in your credentials every time you issue a &lt;code&gt;git push&lt;/code&gt;, and so you can avoid the issues I encountered along the way.&lt;/p&gt;
&lt;p&gt;Let's get started!
</summary><content type="html">&lt;h3&gt;&lt;a name="intro"&gt;Introduction&lt;/a&gt;&lt;/h3&gt;


&lt;p&gt;While there are good &lt;a href="https://docs.gitlab.com/ee/ssh/"&gt;instructions&lt;/a&gt; in GitLab's documentation that explain how to set up key-based SSH authentication, these instructions fall short of a clear, concise, and complete solution. In addition to this, if you wish to use multiple SSH identities for specific services or environments, there are &lt;em&gt;even more&lt;/em&gt; hurdles to overcome.&lt;/p&gt;
&lt;p&gt;This post will help you to set up SSH key-based login on GitLab, so you won't have to enter in your credentials every time you issue a &lt;code&gt;git push&lt;/code&gt;, and so you can avoid the issues I encountered along the way.&lt;/p&gt;
&lt;p&gt;Let's get started!
&lt;/p&gt;
&lt;h3&gt;&lt;a name="generate-ssh-key"&gt;Generate an SSH Key&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;First, we need to generate an SSH key. We can do that with:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ssh-keygen -o -t rsa -b 4096 -C "foo"&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This will prompt you to overwrite the key if you've already generated one. (That's okay!) If you already have an SSH key, use &lt;code&gt;Ctrl-C&lt;/code&gt; or &lt;code&gt;n&lt;/code&gt; to stop this process and proceed to the &lt;a href="#import-ssh-key"&gt;next step&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To generate a key &lt;em&gt;specific to GitLab&lt;/em&gt;, use:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ssh-keygen -o -t rsa -b 4096 -f ~/.ssh/id_rsa.gitlab -v -C "foo"&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This will generate a OpenSSH-formatted key pair, using a 4096-bit cipher. Note that you can replace &lt;strong&gt;&lt;em&gt;foo&lt;/em&gt;&lt;/strong&gt; something more helpful to you, be it your email address or anything that helps you identify the key; the &lt;code&gt;-C&lt;/code&gt; flag in the command represents a comment. For that, I tend to use my machine name.&lt;/p&gt;
&lt;h3&gt;&lt;a name="import-ssh-key"&gt;Import the SSH Key&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Next, copy this to your clipboard with &lt;code&gt;xclip&lt;/code&gt;. If you don't have xclip, install it with:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sudo apt install -y xclip&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Then, copy the public key to your system clipboard wtih:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;xclip -sel clip &amp;lt; ~/.ssh/id_rsa.gitlab.pub&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This assumes that you're using a key created just for GitLab. If that's not the case, use &lt;code&gt;xclip -sel clip &amp;lt; ~/.ssh/id_rsa.pub&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After this, you'll need to navigate to the &lt;a href="https://gitlab.com/profile/keys"&gt;SSH Keys&lt;/a&gt; section of your GitLab profile.&lt;/p&gt;
&lt;p&gt;That page looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="GitLab Key Import" src="https://blog.contracode.com/images/general/definitive-guide-to-ssh-on-gitlab/key-import.png"&gt;&lt;/p&gt;
&lt;p&gt;Yes, I know: I don't have to put a mosaic filter over the &lt;strong&gt;Key&lt;/strong&gt; field, since it is, after all, a &lt;em&gt;public&lt;/em&gt; key. It's okay to share, and that's the beauty of asymmetric encryption! Nonetheless, I've chosen not to publish it here. It's not for you!&lt;/p&gt;
&lt;p&gt;Once on the &lt;em&gt;SSH Keys&lt;/em&gt; page, paste your public key into the &lt;strong&gt;Key&lt;/strong&gt; field and select the &lt;strong&gt;Add Key&lt;/strong&gt; button. You've imported the SSH key! Wahoo!&lt;/p&gt;
&lt;h3&gt;&lt;a name="update-ssh-config"&gt;Update your SSH Config&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;If you are using an SSH key specific to GitLab, you'll need to add this to your &lt;code&gt;~/.ssh/config&lt;/code&gt; file to tell &lt;code&gt;ssh-agent&lt;/code&gt; which private key to use to authenticate.&lt;/p&gt;
&lt;pre&gt;
Host gitlab.com
  User git
  Hostname gitlab.com
  IdentityFile ~/.ssh/id_rsa.gitlab
  Port 22
&lt;/pre&gt;

&lt;p&gt;Again, this task is optional if you are using the default &lt;code&gt;id_rsa&lt;/code&gt; key.&lt;/p&gt;
&lt;h3&gt;&lt;a name="update-git-remote-origins"&gt;Update Git Remote Origins&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;If you've been working with GitLab for a while, you should issue the &lt;code&gt;git remote -v&lt;/code&gt; command within each repository to review the &lt;strong&gt;origin&lt;/strong&gt; remote and update it, if necessary. If this begins with "https", you will continue to be prompted for your username and password, despite having an SSH key set up.&lt;/p&gt;
&lt;p&gt;In general, a remote like &lt;code&gt;https://gitlab.com/foo/bar.git&lt;/code&gt; will become
&lt;code&gt;git@gitlab.com:foo/bar.git&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To update the git repository's remote origin, use:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git remote set-url origin git@gitlab.com:foo/bar.git&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;...and it wouldn't hurt to verify that this change has taken place, with another &lt;code&gt;git remote -v&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;&lt;a name="test-your-connection"&gt;Test Your Connection&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;You can do a simple test wtih:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ssh -T git@gitlab.com&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Or a more verbose test wtih:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ssh -Tv git@gitlab.com&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If these fail, try this before heading to Google:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SSH_AUTH_SOCK=0 ssh git@gitlab.com&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;What does this do? What's &lt;code&gt;SSH_AUTH_SOCK&lt;/code&gt;? It's an environment variable that holds the path to a Unix file socket that the &lt;code&gt;ssh-agent&lt;/code&gt; process uses. If the top two tests failed, and this one passed, the GNOME Keyring SSH Agent is getting in the way of your terminal-based SSH agent. Let's disable it.&lt;/p&gt;
&lt;h3&gt;&lt;a name="test-your-connection"&gt;Disable the GNOME Keyring SSH Agent&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;You'll only want to try this if the first two tests in the &lt;a href="#test-your-connection"&gt;last step&lt;/a&gt; failed, but the third one worked.&lt;/p&gt;
&lt;p&gt;Disabling this key agent on Ubuntu is rather easy. To begin, use the &lt;code&gt;Super&lt;/code&gt; key, and type in "startup". Wait a moment, and you'll see an entry called &lt;strong&gt;Startup Applications&lt;/strong&gt;. Launch that.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Disable GNOME Keyring SSH Agent" src="https://blog.contracode.com/images/general/definitive-guide-to-ssh-on-gitlab/disable-gnome-keyring-ssh-agent.png"&gt;&lt;/p&gt;
&lt;p&gt;Untick the checkbox, and select the &lt;strong&gt;Close&lt;/strong&gt; button. After this, log out and log back in, and you should be in business.&lt;/p&gt;
&lt;p&gt;Happy coding, everyone!&lt;/p&gt;</content><category term="templates"></category><category term="generic"></category></entry></feed>