# Two-factor Authentication

## **Enable two-factor Authentication**

**Two-factor authentication (2FA)** adds an extra layer of security to your Ubuntu system by requiring two forms of identification before granting access. In addition to the usual username and password, 2FA requires an additional piece of information, such as a one-time password (OTP) generated by an authenticator app.

First of all, download the Google Authenticator app on your mobile device

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FbOSO2MnBqdTOr6IEqR4X%2Fimage.png?alt=media&#x26;token=cdba9f9b-4bbe-471f-b2bb-e4ce69657e9b" alt=""><figcaption></figcaption></figure>

Here are some methods to enable 2FA on Ubuntu:&#x20;

### Install the Google Authenticator PAM plugin:

<pre><code><strong>sudo apt-get update -y
</strong></code></pre>

```
sudo apt-get install libpam-google-authenticator -y
```

Run the google-authenticator command as the user you want to log in (**not** as root):

```
sudo google-authenticator 
```

Follow the prompts to set up 2FA. You’ll need an authenticator app (e.g., Google Authenticator or Authy) on your mobile device to scan the QR code.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FH7ndXcXd2vsz1UAio1TA%2Fimage.png?alt=media&#x26;token=9cb00fae-727b-47af-94ae-eaa44d3fcb51" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FAzsblfKjdqb5Wt3XsJu1%2Fimage.png?alt=media&#x26;token=347938ad-b7d4-4424-8cc5-66841a632689" alt=""><figcaption></figcaption></figure>

### Edit the pam.d file

Edit the **/etc/pam.d/sshd** file and add the following line at the end.

```
vi /etc/pam.d/sshd
```

**auth required pam\_google\_authenticator.so**

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FDpd5OL0zRBinY2D5K1fK%2Fimage.png?alt=media&#x26;token=e4c7e9e8-c7be-46fc-ace1-07fb20bc4312" alt=""><figcaption></figcaption></figure>

save the file and exit

Edit the **/etc/ssh/sshd\_config** file

```
vi /etc/ssh/sshd_config
```

**KbdInteractiveAuthentication no > Yes**&#x20;

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FDvl7pGwbu4pkRTolc5We%2Fimage.png?alt=media&#x26;token=d00014b9-2d0a-4134-89ae-2add973ec384" alt=""><figcaption></figcaption></figure>

### Restart the service

Edited both the file restart service

```
systemctl restart sshd.service
```

Open the Command prompt to access the server&#x20;

**ssh user\@IP\_Addresss**

Enter the password and the verification code from your Google Authenticator app on your mobile device.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FpkTCOk3aY6EusIwbOfTz%2Fimage.png?alt=media&#x26;token=54d29817-0037-44f8-a422-6778100a7882" alt=""><figcaption></figcaption></figure>
