{"id":3777,"date":"2023-06-13T11:12:28","date_gmt":"2023-06-13T11:12:28","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=3777"},"modified":"2023-06-13T11:12:28","modified_gmt":"2023-06-13T11:12:28","slug":"configure-ssh-key-for-git-to-use-with-gitlab","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=3777","title":{"rendered":"Configure SSH Key for Git to Use with GitLab"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Here&#8217;s a simple run through, let&#8217;s say you have a Git Repository (repo) that you want to clone to a development machine, in this case the machine is running Ubuntu Linux 22.0.4.2 LTS. A good way to authenticate with Git is with an SSH key, which is a public private key pair that can be used to authenticate you to the repo to push and pull code to and from it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First we&#8217;ll generate an SSH key, then we&#8217;ll add the SSH private key to the ssh-agent, after that we&#8217;ll add the public key to GitLab so this is ready for authentication. Once all that is done we&#8217;ll then perform a test connection and then clone a repo and push a file up to prove it all works fine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Generate SSH Key<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you already have generated an SSH key pair then you don&#8217;t need to do this step. Otherwise run the below to create your key, and ensure you set a passphrase.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -t rsa -b 4096 -C your@email.com<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll see two files created within ~\/.ssh, ensure you only ever share the one ending .pub, which is your public key, the other is your private key which must be kept private!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add SSH Key to ssh-agent<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The ssh-agent is a program that starts when you login and stores your private keys for easy use, you need it running and also for it to have your SSH private key loaded into it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First start it with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>eval \"$(ssh-agent -s)\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then load in your private key to ssh-agent with something like the below, you only need one or the other of these commands depending on the key type you have created.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-add ~\/.ssh\/id_rsa\nssh-add ~\/.ssh\/id_ed25519<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll be prompted for your passphrase, you only need to do this once per session, once its added to ssh-agent, you can use it many times within this session without needing to re-enter it, that is what the passphrase is for. You should see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ubuntu@development:~\/.ssh$ ssh-add ~\/.ssh\/id_ed25519\nIdentity added: \/home\/ubuntu\/.ssh\/id_ed25519 (your@email.com)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check the key is there with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-add -l<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see an entry for the key you just added.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add Public SSH Key to GitLab<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the key by using &#8220;cat&#8221; on your public key file (~\/.ssh\/id_rsa.pub) and then adding the new SSH key under the &#8220;Settings&#8221; page of your profile, just copy and paste that bad boy in.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"600\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/06\/image-7-1024x600.png\" alt=\"\" class=\"wp-image-3780\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/06\/image-7-1024x600.png 1024w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/06\/image-7-300x176.png 300w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/06\/image-7-768x450.png 768w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2023\/06\/image-7.png 1268w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Test Your SSH Access to GitLab<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s test our access to Gitlab, assuming you&#8217;ve got the ssh-agent running and your key inserted, you should be able to run the below command without being prompted for your SSH key passphrase.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ubuntu@development:~\/gitlab$ ssh -T git@gitlab.com\nWelcome to GitLab, @user.name!<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"repo-command-line-instructions\">Create a Test Project\/Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a test repository on Gitlab, in this example let&#8217;s create one called &#8220;test&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now on your development machine within the home directory (or wherever you want to have your clone of the project kept) run one of the following as per what you are trying to do.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Git Global Setup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The two commands below are a global setup, so you should only need to do this once (on your machine within your profile).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git config --global user.name \"User Name\"\ngit config --global user.email \"your@email.com\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create a new repository<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start by cloning the repo &#8220;test&#8221; that we just created:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git clone git@gitlab.com:user.name\/test.git\ncd test\ngit switch --create main\ntouch README.md\ngit add README.md\ngit commit -m \"add README\"\ngit push --set-upstream origin main<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now if you refresh the page showing your repository at GitLab and you&#8217;ll see the README.md file has been added, this shows that we are all good and ready to go!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">So, let&#8217;s recap what we&#8217;ve done. We&#8217;ve created ourselves an SSH public-private SSH keypair, shared the public key with GitLab so we can authenticate with them, created a repo in Gitlab, then cloned the repo to your local development machine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Information<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some other scenarios you might want to explore.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Push an existing folder<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">cd existing_folder\ngit init --initial-branch=main\ngit remote add origin git@gitlab.com:user.name\/test.git\ngit add .\ngit commit -m \"Initial commit\"\ngit push --set-upstream origin main\n\ngit push -u origin main<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Push an existing Git repository<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">cd existing_repo\ngit remote rename origin old-origin\ngit remote add origin git@gitlab.com:user.name\/test.git\ngit push --set-upstream origin --all\ngit push --set-upstream origin --tags<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/unsplash.com\/@bergerteam\" data-type=\"URL\" data-id=\"https:\/\/unsplash.com\/@bergerteam\" target=\"_blank\" rel=\"noreferrer noopener\">Image Attribution<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a simple run through, let&#8217;s say you have a Git Repository (repo) that you want to clone to a development machine, in this case the machine is running Ubuntu Linux 22.0.4.2 LTS. A good way to authenticate with Git is with an SSH key, which is a public private key pair that can be &#8230; <a title=\"Configure SSH Key for Git to Use with GitLab\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=3777\" aria-label=\"Read more about Configure SSH Key for Git to Use with GitLab\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3784,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-3777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3777"}],"version-history":[{"count":5,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3777\/revisions"}],"predecessor-version":[{"id":3785,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3777\/revisions\/3785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/3784"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}