LinkedIn Sourceforge

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

Hardening FreeBSD SSH with Certificates and TOTP Multi-Factor Authentication

Posted on 2026-11-24 22:37:00 from Vincent in OpenBSD FreeBSD

Give a shoutout to  Jason Dent on unsplash.com In today's strict regulatory environment, security standards often demand Multi-Factor Authentication (MFA) across all administrative access points—including remote shell sessions. While SSH public key certificates and source IP restrictions provide robust initial protection, adding a Time-based One-Time Password (TOTP) layer satisfies strict security compliance without sacrificing key-based security. Achieving this dual-layer defense on FreeBSD requires coordinating OpenSSH configuration with the Pluggable Authentication Module (PAM) architecture. In this guide, we will walk step-by-step through configuring OpenSSH and PAM on FreeBSD to seamlessly enforce both valid SSH certificates and TOTP challenges simultaneously.


Technical Context and Prerequisites

When configuring SSH for high-security environments, combining something you have (an SSH certificate) with another thing you have (a dynamic TOTP token generated by an authenticator app) offers defense-in-depth against credential theft.

To achieve this flow on FreeBSD, sshd(8) must be configured with AuthenticationMethods publickey,keyboard-interactive. This instructs the daemon that an incoming connection is only authorized if it completes both authentication stages sequentially. Additionally, the FreeBSD PAM configuration (pam.conf(5)) must be adjusted so that the keyboard-interactive step prompts exclusively for the TOTP code rather than falling back to standard UNIX system passwords.

So, you already have you public key listed in the ~/.ssh/authorized_keys file located on the target FreeBSD machine. With this setup you can directly ssh via certificates on this machine. We will now see how to add the Multi-Factor authentication.

Step 1: Installing the Google Authenticator PAM Module

Log into your remote FreeBSD server as root (or escalate privileges using doas, su or sudo and install the Google Authenticator PAM library from the official package repository using pkg(8):

pkg install pam_google_authenticator

Verify that the module has been placed in the FreeBSD local library path:

ls -la /usr/local/lib/pam_google_authenticator.so

Step 2: Configuring OpenSSH (/etc/ssh/sshd_config)

Open sshd_config(5) (/etc/ssh/sshd_config) in your preferred editor. You must instruct OpenSSH to require both public key authentication and a keyboard-interactive challenge.

Add or update the following directives in the global section of the file (above any Match blocks):

# Ensure Public Key / Certificate Auth is Enabled
PubkeyAuthentication yes

# Disable Plain Password Logins
PasswordAuthentication no

# Enable PAM and Interactive Challenges
UsePAM yes
KbdInteractiveAuthentication yes

# Strictly Require BOTH Certificate/Key AND Keyboard-Interactive (TOTP)
AuthenticationMethods publickey,keyboard-interactive

Note: If you maintain source IP restrictions using Match Address blocks, keep those blocks intact. Place the global AuthenticationMethods directive above your Match blocks so it applies server-wide by default.

Before restarting SSH, test the syntax for errors using sshd(8):

sshd -t

Step 3: Streamlining the PAM Authentication Stack (/etc/pam.d/sshd)

By default, FreeBSD's PAM configuration runs pam_unix(8) during the interactive authentication phase. This causes SSH to ask for the account's UNIX password before asking for the TOTP code.

To ensure the server prompts only for the TOTP token after validating your SSH certificate, edit /etc/pam.d/sshd (see pam.conf(5)) and adjust the auth section:

# Standard FreeBSD PAM configuration for SSH
# Comment out standard UNIX and OPIE password modules:
#auth           sufficient      pam_opie.so             no_warn no_fake_prompts
#auth           requisite       pam_opieaccess.so       no_warn allow_local
#auth           required        pam_unix.so             no_warn try_first_pass

# Enable Google Authenticator TOTP verification:
auth            required        /usr/local/lib/pam_google_authenticator.so

Leave the account, session, and password blocks below the # auth section unchanged. Because PAM reads this file dynamically on every incoming request, you do not need to restart PAM services.

Step 4: Generating User TOTP Tokens

Each user who logs into the FreeBSD system must generate their unique TOTP secret.

  1. Switch to the target user account via su:

bash su - username

  1. Run the generator utility:

bash google-authenticator

  1. Respond to the interactive prompts:

  2. Make tokens time-based: y

  3. Scan QR Code: Scan the terminal-rendered QR code using Aegis or your preferred TOTP app.

  4. Emergency Scratch Codes: Record the emergency recovery codes in a secure password manager.

  5. Update secret file: y

  6. Disallow multiple uses of the same code: y

  7. Permit time-skew window (rate limiting defense): n (unless system clocks drift)

  8. Enable rate limiting: y

This process creates a restricted configuration file at ~/.google_authenticator owned by the user. Return to the root shell by typing exit.

Step 5: Service Reload and Operational Testing

  1. Restart the OpenSSH service via service(8) to apply your changes:

bash service sshd restart

  1. Open a new terminal window on your local machine and initiate an ssh session using your SSH certificate:

bash ssh -v username@your-freebsd-server

  1. Observe the authentication workflow in the verbose output:

  2. OpenSSH validates your signed SSH certificate against the configured CA.

  3. Upon key acceptance, the server triggers the PAM stack.

  4. The client presents the prompt Verification code:.

  5. Entering the 6-digit TOTP code from your mobile device grants instant access to the shell.

Conclusion

Combining SSH public key certificates, IP whitelisting, and TOTP-based Multi-Factor Authentication creates a resilient security posture for remote FreeBSD systems. By customizing FreeBSD's PAM authentication stack to skip redundant UNIX password prompts, administrators achieve seamless compliance with CISO security requirements without compromising user experience or workflow efficiency. With this setup fully deployed across your fleet, unauthorized access attempts are blocked at multiple boundaries, keeping remote FreeBSD infrastructure securely locked down.



👍 0, 👎 0
displayed: 35



What is the last letter of the word Python?