Tutorial: Install and secure a single node GitLab instance

Tier: Free, Premium, Ultimate Offering: Self-managed

In this tutorial you will learn how to install and securely configure a single node GitLab instance that can accommodate up to 20 RPS or 1,000 users.

To install a single node GitLab instance and configure it to be secure:

  1. Secure the server
  2. Install GitLab
  3. Configure GitLab
  4. Next steps

Before you begin

  • A domain name, and a correct setup of DNS.
  • A Debian-based server with the following minimum specs:
    • 8 vCPU
    • 7.2 GB memory
    • Enough hard drive space for all your repositories. Read more about the storage requirements.

Secure the server

Before installing GitLab, start by configuring your server to be a bit more secure.

Configure the firewall

You need to open ports 22 (SSH), 80 (HTTP), and 443 (HTTPS). You can do this by either using your cloud provider’s console, or at the server level.

In this example, you’ll configure the firewall using ufw. You’ll deny access to all ports, allow ports 80 and 443, and finally, rate limit access to port 22. ufw can deny connections from an IP address that has attempted to initiate 6 or more connections in the last 30 seconds.

  1. Install ufw:

    sudo apt install ufw
    
  2. Enable and start the ufw service:

    sudo systemctl enable --now ufw
    
  3. Deny all other ports except the required ones:

    sudo ufw default deny
    sudo ufw allow http
    sudo ufw allow https
    sudo ufw limit ssh/tcp
    
  4. Finally, activate the settings. The following needs to run only once, the first time you install the package. Answer yes (y) when prompted:

    sudo ufw enable
    
  5. Verify that the rules are present:

    $ sudo ufw status
    
    Status: active
    
    To                         Action      From
    --                         ------      ----
    80/tcp                     ALLOW       Anywhere
    443                        ALLOW       Anywhere
    22/tcp                     LIMIT       Anywhere
    80/tcp (v6)                ALLOW       Anywhere (v6)
    443 (v6)                   ALLOW       Anywhere (v6)
    22/tcp (v6)                LIMIT       Anywhere (v6)
    

Configure the SSH server

To further secure your server, configure SSH to accept public key authentication, and disable some features that are potential security risks.

  1. Open /etc/ssh/sshd_config with your editor and make sure the following are present:

    PubkeyAuthentication yes
    PasswordAuthentication yes
    UsePAM yes
    UseDNS no
    AllowTcpForwarding no
    X11Forwarding no
    PrintMotd no
    PermitTunnel no
    # Allow client to pass locale environment variables
    AcceptEnv LANG LC_*
    # override default of no subsystems
    Subsystem       sftp    /usr/lib/openssh/sftp-server
    # Protocol adjustments, these would be needed/recommended in a FIPS or
    # FedRAMP deployment, and use only strong and proven algorithm choices
    Protocol 2
    Ciphers aes128-ctr,aes192-ctr,aes256-ctr
    HostKeyAlgorithms ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
    KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
    Macs hmac-sha2-256,hmac-sha2-512
    
  2. Save the file and restart the SSH server:

    sudo systemctl restart ssh
    

    If restarting SSH fails, check that you don’t have any duplicate entries in /etc/ssh/sshd_config.

Ensure only authorized users are using SSH for Git access

Next, ensure that users cannot pull down projects using SSH unless they have a valid GitLab account that can perform Git operations over SSH.

To ensure that only authorized users are using SSH for Git access:

  1. Add the following to your /etc/ssh/sshd_config file:

    # Ensure only authorized users are using Git
    AcceptEnv GIT_PROTOCOL
    
  2. Save the file and restart the SSH server:

    sudo systemctl restart ssh
    

Make some kernel adjustments

Kernel adjustments do not completely eliminate the threat of an attack, but they add an extra layer of security.

  1. Open a new file with your editor under /etc/sysctl.d, for example /etc/sysctl.d/99-gitlab-hardening.conf, and add the following.

    note
    The naming and source directory decide the order of processing, which is important because the last parameter processed might override earlier ones.
    ##
    ## The following help mitigate out of bounds, null pointer dereference, heap and
    ## buffer overflow bugs, use-after-free etc from being exploited. It does not 100%
    ## fix the issues, but seriously hampers exploitation.
    ##
    # Default is 65536, 4096 helps mitigate memory issues used in exploitation
    vm.mmap_min_addr=4096
    # Default is 0, randomize virtual address space in memory, makes vuln exploitation
    # harder
    kernel.randomize_va_space=2
    # Restrict kernel pointer access (for example, cat /proc/kallsyms) for exploit assistance
    kernel.kptr_restrict=2
    # Restrict verbose kernel errors in dmesg
    kernel.dmesg_restrict=1
    # Restrict eBPF
    kernel.unprivileged_bpf_disabled=1
    net.core.bpf_jit_harden=2
    # Prevent common use-after-free exploits
    vm.unprivileged_userfaultfd=0
    
    ## Networking tweaks ##
    ##
    ## Prevent common attacks at the IP stack layer
    ##
    # Prevent SYNFLOOD denial of service attacks
    net.ipv4.tcp_syncookies=1
    # Prevent time wait assassination attacks
    net.ipv4.tcp_rfc1337=1
    # IP spoofing/source routing protection
    net.ipv4.conf.all.rp_filter=1
    net.ipv4.conf.default.rp_filter=1
    net.ipv6.conf.all.accept_ra=0
    net.ipv6.conf.default.accept_ra=0
    net.ipv4.conf.all.accept_source_route=0
    net.ipv4.conf.default.accept_source_route=0
    net.ipv6.conf.all.accept_source_route=0
    net.ipv6.conf.default.accept_source_route=0
    # IP redirection protection
    net.ipv4.conf.all.accept_redirects=0
    net.ipv4.conf.default.accept_redirects=0
    net.ipv4.conf.all.secure_redirects=0
    net.ipv4.conf.default.secure_redirects=0
    net.ipv6.conf.all.accept_redirects=0
    net.ipv6.conf.default.accept_redirects=0
    net.ipv4.conf.all.send_redirects=0
    net.ipv4.conf.default.send_redirects=0
    
  2. On the next server reboot, the values will be loaded automatically. To load them immediately:

    sudo sysctl --system
    

Great work, you’ve completed the steps to secure your server! Now you’re ready to install GitLab.

Install GitLab

Now that your server is set up, install GitLab:

  1. Install and configure the necessary dependencies:

    sudo apt update
    sudo apt install -y curl openssh-server ca-certificates perl locales
    
  2. Configure the system language:

    1. Edit /etc/locale.gen and make sure en_US.UTF-8 is uncommented.
    2. Regenerate the languages:

      sudo locale-gen
      
  3. Add the GitLab package repository and install the package:

    curl "https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh" | sudo bash
    

    To see the contents of the script, visit https://packages.gitlab.com/gitlab/gitlab-ee/install.

  4. Install the GitLab package. Provide a strong password with GITLAB_ROOT_PASSWORD and replace the EXTERNAL_URL with your own. Don’t forget to include https in the URL, so that a Let’s Encrypt certificate is issued.

    sudo GITLAB_ROOT_PASSWORD="strong password" EXTERNAL_URL="https://gitlab.example.com" apt install gitlab-ee
    

    To learn more about the Let’s Encrypt certificate or even use your own, read how to configure GitLab with TLS.

    If the password you set wasn’t picked up, read more about resetting the root account password.

  5. After a few minutes, GitLab is installed. Sign in using the URL you set up in EXTERNAL_URL. Use root as the username and the password you set up in GITLAB_ROOT_PASSWORD.

Now it’s time to configure GitLab!

Configure GitLab

GitLab comes with some sane default configuration options. In this section, we will change them to add more functionality, and make GitLab more secure.

For some of the options you’ll use the Admin area UI, and for some of them you’ll edit /etc/gitlab/gitlab.rb, the GitLab configuration file.

Configure NGINX

NGINX is used to serve up the web interface used to access the GitLab instance. For more information about configuring NGINX to be more secure, read about hardening NGINX.

Configure emails

Next, you’ll set up and configure an email service. Emails are important for verifying new sign ups, resetting passwords, and notifying you of GitLab activity.

Configure SMTP

In this tutorial, you’ll set up an SMTP server and use the Mailgun SMTP provider.

First, start by creating an encrypted file that will contain the login credentials, and then configure SMTP for the Linux package:

  1. Create a YAML file (for example smtp.yaml) that contains the credentials for the SMTP server.

    Your SMTP password must not contain any string delimiters used in Ruby or YAML (for example, ') to avoid unexpected behavior during the processing of configuration settings.

    user_name: '<SMTP user>'
    password: '<SMTP password>'
    
  2. Encrypt the file:

    cat smtp.yaml | sudo gitlab-rake gitlab:smtp:secret:write
    

    By default, the encrypted file is stored under /var/opt/gitlab/gitlab-rails/shared/encrypted_configuration/smtp.yaml.enc.

  3. Remove the YAML file:

    rm -f smtp.yaml
    
  4. Edit /etc/gitlab/gitlab.rb and set up the rest of the SMTP settings. Make sure gitlab_rails['smtp_user_name'] and gitlab_rails['smtp_password'] are not present, as we’ve already set them up as encrypted.

    gitlab_rails['smtp_enable'] = true
    gitlab_rails['smtp_address'] = "smtp.mailgun.org" # or smtp.eu.mailgun.org
    gitlab_rails['smtp_port'] = 587
    gitlab_rails['smtp_authentication'] = "plain"
    gitlab_rails['smtp_enable_starttls_auto'] = true
    gitlab_rails['smtp_domain'] = "<mailgun domain>"
    
  5. Save the file and reconfigure GitLab:

    sudo gitlab-ctl reconfigure
    

You should now be able to send emails. To test that the configuration worked:

  1. Enter the Rails console:

    sudo gitlab-rails console
    
  2. Run the following command at the console prompt to make GitLab send a test email:

    Notify.test_email('<email_address>', 'Message Subject', 'Message Body').deliver_now
    

If you’re unable to send emails, see the SMTP troubleshooting section.

Enable the email verification

Account email verification provides an additional layer of GitLab account security. When some conditions are met, for example, if there are three or more failed sign-in attempts in 24 hours, an account is locked.

This feature is behind a feature flag. To enable it:

  1. Enter the Rails console:

    sudo gitlab-rails console
    
  2. Enable the feature flag:

    Feature.enable(:require_email_verification)
    
  3. Check if it’s enabled (should return true):

    Feature.enabled?(:require_email_verification)
    

For more information, read about account email verification.

Sign outgoing email with S/MIME

Notification emails sent by GitLab can be signed with S/MIME for improved security.

A single pair of key and certificate files must be provided:

  • Both files must be PEM-encoded.
  • The key file must be unencrypted so that GitLab can read it without user intervention.
  • Only RSA keys are supported.
  • Optional. You can provide a bundle of Certificate Authority (CA) certs (PEM-encoded) to include on each signature. This is typically an intermediate CA.
  1. Buy your certificate from a CA.
  2. Edit /etc/gitlab/gitlab.rb and adapt the file paths:

    gitlab_rails['gitlab_email_smime_enabled'] = true
    gitlab_rails['gitlab_email_smime_key_file'] = '/etc/gitlab/ssl/gitlab_smime.key'
    gitlab_rails['gitlab_email_smime_cert_file'] = '/etc/gitlab/ssl/gitlab_smime.crt'
    
  3. Save the file and reconfigure GitLab:

    sudo gitlab-ctl reconfigure
    

For more information, read about signing outgoing email with S/MIME.

Next steps

In this tutorial, you learned how to set up your server to be more secure, how to install GitLab, and how to configure GitLab to meet some security standards. Some other steps you can take to secure GitLab include:

  • Disabling sign ups. By default, a new GitLab instance has sign up enabled by default. If you don’t plan to make your GitLab instance public, you should to disable sign ups.
  • Allowing or denying sign ups using specific email domains.
  • Setting a minimum password length limit for new users.
  • Enforcing two-factor authentication for all users.

There are many other things you can configure apart from hardening your GitLab instance, like configuring your own runners to leverage the CI/CD features that GitLab has to offer, or properly backing up your instance.

You can read more about the steps to take after the installation.