Improving instance security using two-factor authentication with Google Authentication (2FA)

Modified: 17 May 2023 01:11 UTC

In addition to protecting your Triton cloud account in the portal with two-factor authentication, you can also protect your instances with two-factor authentication when connecting to your instance through SSH. SmartOS instances and Linux instances support one-time passwords (OTP) using any app that supports the Google Authenticator protocol. Other options may be supported if they provide a Pluggable Authentication Module (PAM) that is compatible with libpam provided by operating environment you are running. This document describes how to add TOTP/HOTP to SmartOS using the Google Authenticator PAM module. For Linux refer to the documentation for your distro or the google-authenticator-libpam source.

What is two-factor authentication for SSH and how does it work on SmartOS?

SmartOS libpam allows adding additonal methods of verifcation when connecting using SSH. The default method of verifying identity for SSH is using a public and private key pair. This allows for passwordless authentication as SSH keys are much harder to guess than passwords and are less susceptible to brute force attacks. Private keys can be further protected with a passphrase so that if your private key is compromised or stolen, it cannot be used without knowing the passphrase. Two-factor authentication does not require the use of keys, you can use a normal password in addition to a one-time-password, however as stated earlier passwords are not as secure as using keys.

Google Authenticator Library for two-factor authentication

Using the Google Authenticator PAM library can add another layer of security to your instance. The following setup method will use the default key verification method as well as adding another factor using one-time-passwords (OTP) generated through applications that support Google Authentication OTP including Google Authenticator, Microsoft Authenticator, and 1Password. 1Password has the advantage of being available as a desktop app, a browser extension (Chrome, Firefox, Microsoft Edge, and Safari are all supported), and as a mobile app.

Installing Google Authenticator Library on SmartOS using pkgsrc and configuration for SSH/PAM

The Google Authenticator library and binary are available in pkgsrc on SmartOS. The command pkgin install google-authenticator-compat32 will install the Google Authenticator library, the Google Authenticator binary, and the qrencode library and binary to generate the QR code for the authentication application to use. After installation via pkgsrc, to configure ssh to use one-time-password authentication the following steps need to be taken:

Many other configurations are possible. Some examples include:

User set up

Once 2FA is available on the system, each user needs to run a one-time set up to configure their account. It is highly recommended to use the nullok option until all users are configured to avoid getting locked out.

Each user must perform the following steps:

The instance you have configured is now protected with two-factor authentication! If you prefer building from source, use the instructions below to protect your instance.

To assist with installation I have created a bash script that includes some sed commands that will set the PAM and SSH confugration discussed here. This script will check if the lines exist before appending them. The helper script can be found here: Helper Script Download