DEV Community

Cover image for Configuring 2FA on your Linux Server
Florentin / 珞辰
Florentin / 珞辰

Posted on • Originally published at ecklf.com

Configuring 2FA on your Linux Server

Recommended Setup

Update your server to the latest packages.

sudo apt update && apt upgrade && reboot
Enter fullscreen mode Exit fullscreen mode

Add a non-root user with sudo privileges.

adduser username
usermod -aG sudo username
su username
Enter fullscreen mode Exit fullscreen mode

Time-based One-time Password Authentication

First, install the Google Authenticator PAM module.

sudo apt install libpam-google-authenticator
Enter fullscreen mode Exit fullscreen mode

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

google-authenticator
Enter fullscreen mode Exit fullscreen mode

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

I've chosen the following settings for this example.

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
Enter fullscreen mode Exit fullscreen mode

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

sudo vim /etc/pam.d/sshd
Enter fullscreen mode Exit fullscreen mode
# Standard Un*x password updating.
@include common-password
+ auth required pam_google_authenticator.so
Enter fullscreen mode Exit fullscreen mode
sudo vim /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode
// We created a non-root user with sudo privileges earlier, so we can disable
- PermitRootLogin yes
+ PermitRootLogin no

- ChallengeResponseAuthentication no
+ ChallengeResponseAuthentication yes
Enter fullscreen mode Exit fullscreen mode
// Restart to pick up changes
sudo systemctl restart sshd
Enter fullscreen mode Exit fullscreen mode

Hardware-based Authentication with YubiKey

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

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

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.

brew install ykman
ykman list
Firmware version: 5.2.7
Enter fullscreen mode Exit fullscreen mode
// 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
Enter fullscreen mode Exit fullscreen mode

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.

ssh-copy-id -i ~/.ssh/hostname-yk_id username@IP
Enter fullscreen mode Exit fullscreen mode

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

ssh -i ~/.ssh/hostname-yk_id username@IP
Enter fullscreen mode Exit fullscreen mode

You can repeat the above steps for multiple keys.

Top comments (0)