Two-factor authentication
Two-factor authentication (2FA) provides an additional level of security to your GitLab account. After being enabled, in addition to supplying your username and password to sign in, you'll be prompted for a code generated by your one-time password authenticator (for example, a password manager on one of your devices).
By enabling 2FA, the only way someone other than you can sign in to your account is to know your username and password and have access to your one-time password secret.
Overview
TIP: Tip: When you enable 2FA, don't forget to back up your recovery codes!
In addition to time-based one time passwords (TOTP), GitLab supports U2F (universal 2nd factor) and WebAuthn (experimental) devices as the second factor of authentication. Once enabled, in addition to supplying your username and password to log in, you'll be prompted to activate your U2F / WebAuthn device (usually by pressing a button on it), and it will perform secure authentication on your behalf.
It is highly recommended that you set up 2FA with both a one-time password authenticator or use FortiAuthenticator and a U2F device or a WebAuthn device, so you can still access your account if you lose your U2F / WebAuthn device.
Enabling 2FA
There are multiple ways to enable two-factor authentication: via a one time password authenticator or a U2F / WebAuthn device.
One-time password
To enable 2FA:
-
In GitLab:
- Sign in to your GitLab account.
- Go to your Profile settings.
- Go to Account.
- Select Enable Two-factor Authentication.
-
On your device (usually your phone):
- Install a compatible application, like:
- Authenticator: open source app for iOS devices.
- andOTP: feature rich open source app for Android which supports PGP encrypted backups.
- FreeOTP: open source app for Android.
- Google Authenticator: proprietary app for iOS and Android.
- SailOTP: open source app for SailFish OS.
- In the application, add a new entry in one of two ways:
- Scan the code presented in GitLab with your device's camera to add the entry automatically.
- Enter the details provided to add the entry manually.
- Install a compatible application, like:
-
In GitLab:
- Enter the six-digit pin number from the entry on your device into the Pin code field.
- Select Submit.
If the pin you entered was correct, you'll see a message indicating that two-factor authentication has been enabled, and you'll be presented with a list of recovery codes. Be sure to download them and keep them in a safe place.
One-time password via FortiAuthenticator
- Introduced in GitLab 13.5
- It's deployed behind a feature flag, disabled by default.
- To use it in GitLab self-managed instances, ask a GitLab administrator to enable it.
You can use FortiAuthenticator as an OTP provider in GitLab. Users must exist in both FortiAuthenticator and GitLab with the exact same username, and users must have FortiToken configured in FortiAuthenticator.
You'll also need a username and access token for FortiAuthenticator. The
access_token
in the code samples shown below is the FortAuthenticator access
key. To get the token, see the REST API Solution Guide
at
Fortinet Document Library
.
GitLab 13.5 has been tested with FortAuthenticator version 6.2.0.
First configure FortiAuthenticator in GitLab. On your GitLab server:
-
Open the configuration file.
For Omnibus GitLab:
sudo editor /etc/gitlab/gitlab.rb
For installations from source:
cd /home/git/gitlab sudo -u git -H editor config/gitlab.yml
-
Add the provider configuration:
For Omnibus package:
gitlab_rails['forti_authenticator_enabled'] = true gitlab_rails['forti_authenticator_host'] = 'forti_authenticator.example.com' gitlab_rails['forti_authenticator_port'] = 443 gitlab_rails['forti_authenticator_username'] = '<some_username>' gitlab_rails['forti_authenticator_access_token'] = 's3cr3t'
For installations from source:
forti_authenticator: enabled: true host: forti_authenticator.example.com port: 443 username: <some_username> access_token: s3cr3t
-
Save the configuration file.
-
Reconfigure or restart GitLab for the changes to take effect if you installed GitLab via Omnibus or from source respectively.
Enable FortiAuthenticator integration
This feature comes with the :forti_authenticator
feature flag disabled by
default.
To enable this feature, ask a GitLab administrator with Rails console access to run the following command:
Feature.enable(:forti_authenticator, User.find(<user ID>))
U2F device
Introduced in GitLab 8.9.
GitLab officially only supports YubiKey U2F devices, but users have successfully used SoloKeys or Google Titan Security Key.
The U2F workflow is supported by the following desktop browsers:
- Chrome
- Edge
- Firefox 67+
- Opera
NOTE: Note:
For Firefox 47-66, you can enable the FIDO U2F API in
about:config.
Search for security.webauth.u2f
and double click on it to toggle to true
.
To set up 2FA with a U2F device:
- Log in to your GitLab account.
- Go to your Profile settings.
- Go to Account.
- Click Enable Two-Factor Authentication.
- Connect your U2F device.
- Click on Set up New U2F Device.
- A light will start blinking on your device. Activate it by pressing its button.
You will see a message indicating that your device was successfully set up. Click on Register U2F Device to complete the process.
WebAuthn device
- Introduced in GitLab 13.4.
- It's deployed behind a feature flag, disabled by default.
- It's disabled on GitLab.com.
- It's not recommended for production use.
- To use it in GitLab self-managed instances, ask a GitLab administrator to enable it. (CORE ONLY)
The WebAuthn workflow is supported by the following desktop browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari
and the following mobile browsers:
- Chrome for Android
- Firefox for Android
- iOS Safari (since iOS 13.3)
To set up 2FA with a WebAuthn compatible device:
- Sign in to your GitLab account.
- Go to your Profile settings.
- Go to Account.
- Select Enable Two-Factor Authentication.
- Plug in your WebAuthn device.
- Select Set up New WebAuthn Device.
- Depending on your device, you might need to press a button or touch a sensor.
You will see a message indicating that your device was successfully set up. Recovery codes are not generated for WebAuthn devices.
Recovery codes
NOTE: Note: Recovery codes are not generated for U2F / WebAuthn devices.
CAUTION: Caution: Each code can be used only once to log in to your account.
Immediately after successfully enabling two-factor authentication, you'll be
prompted to download a set of generated recovery codes. Should you ever lose access
to your one-time password authenticator, you can use one of these recovery codes to log in to
your account. We suggest copying and printing them, or downloading them using
the Download codes button for storage in a safe place. If you choose to
download them, the file will be called gitlab-recovery-codes.txt
.
If you lose the recovery codes or just want to generate new ones, you can do so from the two-factor authentication account settings page or using SSH.
Logging in with 2FA Enabled
Logging in with 2FA enabled is only slightly different than a normal login. Enter your username and password credentials as you normally would, and you'll be presented with a second prompt, depending on which type of 2FA you've enabled.
Log in via a one-time password
When asked, enter the pin from your one time password authenticator's application or a recovery code to log in.
Log in via U2F device
To log in via a U2F device:
- Click Login via U2F Device.
- A light will start blinking on your device. Activate it by touching/pressing its button.
You will see a message indicating that your device responded to the authentication request and you will be automatically logged in.
Log in via WebAuthn device
In supported browsers you should be automatically prompted to activate your WebAuthn device (e.g. by touching/pressing its button) after entering your credentials.
You will see a message indicating that your device responded to the authentication request and you will be automatically logged in.
Disabling 2FA
If you ever need to disable 2FA:
- Log in to your GitLab account.
- Go to your Profile settings.
- Go to Account.
- Click Disable, under Two-Factor Authentication.
This will clear all your two-factor authentication registrations, including mobile applications and U2F / WebAuthn devices.
Personal access tokens
When 2FA is enabled, you can no longer use your normal account password to authenticate with Git over HTTPS on the command line or when using GitLab's API. You must use a personal access token instead.
Recovery options
To disable two-factor authentication on your account (for example, if you have lost your code generation device) you can:
- Use a saved recovery code.
- Generate new recovery codes using SSH.
- Regenerate 2FA recovery codes.
- Ask a GitLab administrator to disable two-factor authentication on your account.
Use a saved recovery code
Enabling two-factor authentication for your account generated several recovery codes. If you saved these codes, you can use one of them to sign in.
To use a recovery code, enter your username/email and password on the GitLab sign-in page. When prompted for a two-factor code, enter the recovery code.
Once you use a recovery code, you cannot re-use it. You can still use the other recovery codes you saved.
Generate new recovery codes using SSH
Users often forget to save their recovery codes when enabling two-factor authentication. If an SSH key is added to your GitLab account, you can generate a new set of recovery codes with SSH:
-
Run:
ssh git@gitlab.example.com 2fa_recovery_codes
-
You will then be prompted to confirm that you want to generate new codes. Continuing this process invalidates previously saved codes:
Are you sure you want to generate new two-factor recovery codes? Any existing recovery codes you saved will be invalidated. (yes/no) yes Your two-factor authentication recovery codes are: 119135e5a3ebce8e 11f6v2a498810dcd 3924c7ab2089c902 e79a3398bfe4f224 34bd7b74adbc8861 f061691d5107df1a 169bf32a18e63e7f b510e7422e81c947 20dbed24c5e74663 df9d3b9403b9c9f0 During sign in, use one of the codes above when prompted for your two-factor code. Then, visit your Profile Settings and add a new device so you do not lose access to your account again.
-
Go to the GitLab sign-in page and enter your username/email and password. When prompted for a two-factor code, enter one of the recovery codes obtained from the command-line output.
After signing in, visit your Profile settings > Account immediately to set up two-factor authentication with a new device.
Regenerate 2FA recovery codes
To regenerate 2FA recovery codes, you need access to a desktop browser:
- Navigate to GitLab.
- Sign in to your GitLab account.
- Go to your Profile settings.
- Select {account} Account > Two-Factor Authentication (2FA).
- If you've already configured 2FA, click Manage two-factor authentication.
- In the Register Two-Factor Authenticator pane, click Regenerate recovery codes.
NOTE: Note: If you regenerate 2FA recovery codes, save them. You won't be able to use any previously created 2FA codes.
Ask a GitLab administrator to disable two-factor authentication on your account
If you cannot use a saved recovery code or generate new recovery codes, ask a GitLab global administrator to disable two-factor authentication for your account. This will temporarily leave your account in a less secure state. Sign in and re-enable two-factor authentication as soon as possible.
Note to GitLab administrators
-
You need to take special care to that 2FA keeps working after restoring a GitLab backup.
-
To ensure 2FA authorizes correctly with TOTP server, you may want to ensure your GitLab server's time is synchronized via a service like NTP. Otherwise, you may have cases where authorization always fails because of time differences.
-
The GitLab U2F implementation does not work when the GitLab instance is accessed from multiple hostnames, or FQDNs. Each U2F registration is linked to the current hostname at the time of registration, and cannot be used for other hostnames/FQDNs. The same applies to WebAuthn registrations.
For example, if a user is trying to access a GitLab instance from
first.host.xyz
andsecond.host.xyz
:- The user logs in via
first.host.xyz
and registers their U2F key. - The user logs out and attempts to log in via
first.host.xyz
- U2F authentication succeeds. - The user logs out and attempts to log in via
second.host.xyz
- U2F authentication fails, because the U2F key has only been registered onfirst.host.xyz
.
- The user logs in via
-
To enforce 2FA at the system or group levels see Enforce Two-factor Authentication.
Enable or disable WebAuthn (CORE ONLY)
Support for WebAuthn is under development and not ready for production use. It is deployed behind a feature flag that is disabled by default. GitLab administrators with access to the GitLab Rails console can enable it.
To enable it:
Feature.enable(:webauthn)
To disable it:
Feature.disable(:webauthn)
Troubleshooting
If you are receiving an invalid pin code
error, this may indicate that there is a time sync issue between the authentication application and the GitLab instance itself.
Most authentication apps have a feature in the settings for syncing the time for the codes themselves. For Google Authenticator for example, go to Settings > Time correction for codes
.