{"id":1789,"date":"2023-03-31T19:52:01","date_gmt":"2023-03-31T17:52:01","guid":{"rendered":"https:\/\/hobbykeller.spdns.de\/?p=1789"},"modified":"2023-03-31T19:54:24","modified_gmt":"2023-03-31T17:54:24","slug":"ssh-access-to-your-gitlab-server","status":"publish","type":"post","link":"https:\/\/hobbykeller.spdns.de\/?p=1789","title":{"rendered":"SSH access to your GitLab server"},"content":{"rendered":"\n<p>The most common way of pulling and pushing code between your development machine and your GitLab server is by means of SSH keys. Most tutorials on GitLab tell you something along the following lines:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>cat<\/code> the public SSH key of your workstation, which is normally located at <code>~\/.ssh\/id_rsa.pub<\/code><\/li>\n\n\n\n<li>Log into your GitLab server, click on your avatar (upper right corner), click <strong>Preferences<\/strong> in the drop down menu that appears<\/li>\n\n\n\n<li>The navigation bar of the GitLab GUI on the RHS adapts to show <strong>User Settings<\/strong>, click on <strong>SSH keys<\/strong>.<\/li>\n\n\n\n<li>Copy the output from the cat command in step 1 and paste it into the key box.<\/li>\n<\/ol>\n\n\n\n<p>Most of these tutorials demonstrate connecting to GitLab (either the SaaS version or your own server) from a <strong>freshly installed<\/strong> Ubuntu Desktop <strong>virtual machine<\/strong>. Your real life situation is different though:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Unlike a virgin default Ubuntu Desktop Virtual Box machine or similar, your real life workstation <strong>already has an SSH key pair<\/strong>. The question therefore is: Do I need to create another pair that is dedicated to connect to GitLab or can\/should I use the existing key pair.<\/li>\n\n\n\n<li>In my case, GitLab runs in a Docker stack on a (physically) different host machine. To gain access to the host machine&#8217;s OS, I can ssh into the host machine through the standard port 22. This however means, that the GitLab Docker stack which has another independent SSH daemon for pulling\/pushing code <strong>cannot use the same default port 22.<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Getting a public SSH key<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Allowed Ciphers and Standards<\/h3>\n\n\n\n<p>Generally speaking any SSH key pair is acceptable for GitLab as long as it is either <strong>RSA<\/strong> or <strong>ED25519<\/strong>. GitLab recommends the ED25519 standard pretending that it is more secure than RSA.<\/p>\n\n\n\n<p>As I already have a standard RSA key pair in place, I will stick with that RSA mechnanism.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I build a dedicated pair for GitLab?<\/h3>\n\n\n\n<p>As most GitLab tutorials start with a &#8216;naked&#8217; Ubuntu machine or similar, most of these machines don&#8217;t have any SSH key pair at all, and so by default, a pair must be created for GitLab.<\/p>\n\n\n\n<p>If you already have a key pair which you use for logging into remote servers, you can keep things simple by not creating a second pair on top of the existing one: no need to backup two key pairs, remember two passwords, tell your workstation when to use which key etc. Just <strong>stick with your existing key pair<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Port redirection<\/h3>\n\n\n\n<p>If you run GitLab from a docker stack, port forwarding is easy: Just edit the ports section of your docker-compose.yml file as shown below:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"height-set:true width-set:true lang:default mark:13 decode:true \" title=\"docker-compose.yml for GitLab stack\">version: '3.6'\nservices:\n  web:\n    image: 'gitlab\/gitlab-ee:latest'\n    restart: always\n    hostname: 'gitlab.example.com'\n    environment:\n      GITLAB_OMNIBUS_CONFIG: |\n        external_url 'http:\/internal_router.box'\n    ports:\n      - '1080:80'\n      - '1443:443'\n      - '1022:22'\n    volumes:\n      - '\/srv\/gitlab\/config:\/etc\/gitlab'\n      - '\/srv\/gitlab\/logs:\/var\/log\/gitlab'\n      - '\/srv\/gitlab\/data:\/var\/opt\/gitlab'\n    shm_size: '8g'\n<\/pre><\/div>\n\n\n\n<p>In my case, any incoming traffic on port 1022 is redirected to port 22 of the GitLab Docker container.<\/p>\n\n\n\n<div class=\"wp-block-simple-alerts-for-gutenberg-alert-boxes sab-alert sab-alert-info\" role=\"alert\">Tricky question: Do we have to open port 1022 explicitly (e.g. by means of <code>iptables<\/code> or <code>ufw<\/code>) or will Docker take care of opening port 1022 for listening automatically on the host machine?<br>The answer (at least in my case) is that port 1022 is opened automatically by Docker, which we can check with the following command on the host: <code>netstat -an | grep 1022 | grep -i listen<\/code><br>The response confirming that the host is actually listening on that port should look like:<br><code>tcp        0      0 0.0.0.0:1022            0.0.0.0:*               LISTEN     <br>tcp6       0      0 :::1022                 :::*                    LISTEN <\/code><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">\u00d7<\/span><\/button><\/div>\n\n\n\n<div class=\"wp-block-simple-alerts-for-gutenberg-alert-boxes sab-alert sab-alert-warning\" role=\"alert\">If you want to allow access to your GitLab from outside and sit behind a NAT wall (vulgo: your router), you also have to instruct your router to forward incoming traffic on port 1022 to port 1022 of your GitLab host machine.<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">\u00d7<\/span><\/button><\/div>\n\n\n\n<div class=\"wp-block-simple-alerts-for-gutenberg-alert-boxes sab-alert sab-alert-warning\" role=\"alert\">Final Note on &#8216;alternatives&#8217; for SSH port forwarding: There has been some discussion if port forwarding can also be achieved by Traefik or Nginx Proxy Manager. As these instruments are intended to forward <strong>HTTP<\/strong> traffic, it is not possible to forward the SSH port. You can set up NPM to redirect external https (not ssh!) enquiries to your GitLab domain to be forwarded to port 1080 of your host machine where GitLab will happily listen to redirect any HTTP traffic to the internal port 80 of its container.<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">\u00d7<\/span><\/button><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Checking if the key is working<\/h3>\n\n\n\n<p>After having pasted your public SSH key into GitLab&#8217;s GUI (see step 4 above), you can  test if your SSH key is working properly. On your workstation, issue the following command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \" >ilek@i7:~$ ssh -T git@yourgitserverurl.com -p 1022\nWelcome to GitLab, @ilek!<\/pre><\/div>\n\n\n\n<div class=\"wp-block-simple-alerts-for-gutenberg-alert-boxes sab-alert sab-alert-primary\" role=\"alert\">Although you have a different user name (ilek), you must always connect to <code><strong>git<\/strong>@&lt;GitLab-URL><\/code>, not to ilek@&lt;GitLab URL>. (This is just like in gitolite where all connections have to log into a restricted SSH environment through a central user. Authorization and authentication are then performed on the basis of the SSH key presented.)<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">\u00d7<\/span><\/button><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The most common way of pulling and pushing code between your development machine and your GitLab server is by means of SSH keys. Most tutorials on GitLab tell you something<span class=\"more-button\"><a href=\"https:\/\/hobbykeller.spdns.de\/?p=1789\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\">SSH access to your GitLab server<\/span><\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[338,369,347],"class_list":["post-1789","post","type-post","status-publish","format-standard","hentry","category-linux","tag-docker","tag-gitlab","tag-port-forwarding"],"_links":{"self":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts\/1789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1789"}],"version-history":[{"count":10,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts\/1789\/revisions"}],"predecessor-version":[{"id":1800,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts\/1789\/revisions\/1800"}],"wp:attachment":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}