Configuring 2FA on your Linux Server
Tags: server, linux, yubikey, security
Published: 2021-06-16 21:36:03

## Recommended Setup

Update your server to the latest packages.

```shell
sudo apt update && apt upgrade && reboot
```

Add a non-root user with sudo privileges.

```shell
adduser username
usermod -aG sudo username
su username
```

## Time-based One-time Password Authentication

First, install the Google Authenticator PAM module.

```shell
sudo apt install libpam-google-authenticator
```

Run the `google-authenticator` binary to create a new secret key (keep this one safe).

```shell
google-authenticator
```

After completing the process, you can scan/enter the code in an Authenticator App.

I've chosen the following settings for this example.

```md
Do you want authentication tokens to be time-based (y/n) y

Do you want me to update your "/home/username/.google_authenticator" file? (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) n

By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) y
```

To finish the Setup, we need to add the authentication module to the OpenSSH daemon.

```shell
sudo vim /etc/pam.d/sshd
```

```diff
# Standard Un*x password updating.
@include common-password
+ auth required pam_google_authenticator.so
```

```shell
sudo vim /etc/ssh/sshd_config
```

```diff
// We created a non-root user with sudo privileges earlier, so we can disable
- PermitRootLogin yes
+ PermitRootLogin no

- ChallengeResponseAuthentication no
+ ChallengeResponseAuthentication yes
```

```shell
// Restart to pick up changes
sudo systemctl restart sshd
```

## Hardware-based Authentication with YubiKey

Check if OpenSSH 8.2 or higher is installed on your server for FIDO (Fast Identity Online) U2F support.

```shell
lsb_release -d && ssh -V
Description:	Ubuntu 20.04.2 LTS
OpenSSH_8.2p1 Ubuntu-4ubuntu0.2, OpenSSL 1.1.1f  31 Mar 2020
```

Create a new SSH key pair with `ecdsa-sk` or `ed25519-sk`. Please note that `ed25519-sk` only works if your YubiKey runs on Firmware `>=5.2.3`.
To determine your firmware, you can use `ykman` or `YubiKey manager`.

```shell
brew install ykman
ykman list
Firmware version: 5.2.7
```

```shell
// Using ed25519-sk since Firmware of my YubiKey >=5.2.3
// macOS may throw "unknown key type ed25519-sk", run "brew install openssh" to fix this issue
ssh-keygen -t ed25519-sk -f ~/.ssh/$(hostname)-yk_id
```

If you didn't set up a FIDO PIN, the first thing you should see is:

> You may need to touch your authenticator to authorize key generation

Touch the YubiKey disk and finish the Setup.

For the last step, you'll add the generated public key to your server's `~/.ssh/authorized_keys`.

```shell
ssh-copy-id -i ~/.ssh/hostname-yk_id username@IP
```

Logging into the server will now ask you for a password (if defined) and a physical touch on your YubiKey.

```shell
ssh -i ~/.ssh/hostname-yk_id username@IP
```

You can repeat the above steps for multiple keys.