DUO-SSH

Linux OpenSSH + PAM: system password followed by one Telegram code or TOTP code (Google Authenticator compatible). Protection is selected per account by membership in the duo-ssh group. Incorrect codes are rejected; with the SSH settings below the connection closes after the first failed attempt. Telegram codes last five minutes and are tied to one login attempt. TOTP replay is prevented by persistent state.

Status: administrator-operated preview. The console installer installs program files only. Activation requires the manual PAM and sshd configuration below. Do not run on a production server until you have checked your PAM stack and tested an isolated SSH instance. This project has not had an independent security audit. The instructions are for Linux with OpenSSH, Linux-PAM and Debian/Ubuntu-style common-auth, not all Linux distributions or managed panels.

Before you start

Keep a working root console or out-of-band recovery, and keep the current SSH session open. Activation requires system passwords: protected accounts cannot log in using only a public key, and key-based automation for them will break. A locked account password cannot authenticate. Your SSH client must support keyboard-interactive and two prompts. Audit the existing Match, Include and PAM rules; other authentication paths must not bypass the factor. Enforce network rate limits separately: closing one connection does not stop new attempts. Do not share secrets between users. Telegram codes and server identity are sent to Telegram; TOTP requires synchronized clocks. Loss of the configured factor blocks SSH access.

Install program files from a trusted checkout

Use root on the server. Inspect the repository and scripts before running them. Ensure root-owned parents and source checkout; never execute code from a location writable by untrusted users. Dependencies: git, python3, C compiler, install, sshd, Linux-PAM and pam_succeed_if.so.

sudo -i
cd /root
git clone https://git.155-103-194-118.sslip.io/sogerien/duo-ssh.git duo-ssh-src &&
cd duo-ssh-src &&
./install.sh

If Git is unavailable, download the release archive. The project page publishes its SHA-256 and a complete download command. Before extracting, compare the downloaded file's SHA-256 with the value on that page. For example, as root:

mkdir -m 700 /root/duo-ssh-download &&
cd /root/duo-ssh-download &&
curl -fLO https://sogerien.top/releases/duo-ssh/duo-ssh-preview.tar.gz &&
sha256sum duo-ssh-preview.tar.gz

Continue only if the checksum matches the project page, then run:

tar -xzf duo-ssh-preview.tar.gz &&
cd duo-ssh &&
./install.sh

The archive requires curl, tar, and sha256sum in addition to the build and runtime dependencies above. Use a new download directory; if it already exists, review it instead of overwriting its contents. The checksum detects corruption and is published on the same website; it is not an independent signature.

The script refuses existing installation paths and leaves SSH/PAM untouched. It installs /usr/local/lib/security/pam_duo_ssh.so, /usr/local/libexec/duo-ssh/duo_ssh.py, and creates root-only /etc/duo-ssh and /var/lib/duo-ssh. It does not install packages, configure users, enable 2FA, or update an existing installation. On partial failure inspect and remove only files created by the script before retrying. For upgrades and uninstallation use a reviewed change plan; never erase or roll back TOTP state during normal operation.

Configure factors and activate

  1. Create /etc/duo-ssh/config.json as root with owner root:root and mode 0600 in the existing root-only directory. Insert your own values using a protected editor, not shell history or command arguments. Provide either totp_secret, or both Telegram fields, or both factors. The bot must be able to send messages to the chat. Use a distinct high-entropy Base32 TOTP secret per user and enroll it in the authenticator.

    {"users":{"alice":{"telegram_token":"YOUR_BOT_TOKEN","telegram_chat_id":"YOUR_CHAT_ID","totp_secret":"YOUR_BASE32_SECRET"}}}
    

    Omit unused fields. Replace alice with an existing account; a protected account without a working factor is denied. Confirm directory and file are owned by root and are not symbolic links; no parent may be writable by group or others. /var/lib/duo-ssh must persist on a local filesystem supporting locks, atomic rename and fsync. Do not delete or roll back its contents; doing so can allow replay of a previously used TOTP.

  2. Create the group with getent group duo-ssh || groupadd duo-ssh, but do not add users yet. Back up /etc/pam.d/sshd and all affected SSH configuration files before editing. Verify that the actual PAM password path requires a password and stops immediately on failure. After that authentication step add the two lines in pam-auth.duo-ssh.conf to the SSH auth stack. For standard Debian/Ubuntu this may be immediately after @include common-auth, but inspect every included file and its control flags first. Do not install this rule blindly on a custom PAM stack.

  3. Configure SSH using the example sshd_config.duo-ssh.conf. Ensure UsePAM yes is effective globally and the fragment is included before conflicting settings. OpenSSH uses the first obtained value for many options; a different Match or Include may override your intent. LoginGraceTime 360 affects all accounts. Add only the configured user to duo-ssh after the rules are in place: usermod -aG duo-ssh alice. Unprotected users keep their existing methods only if your effective config confirms it.

  4. Before reload, run /usr/sbin/sshd -t and sshd -T -C user=alice,host=localhost,addr=127.0.0.1. Verify usepam yes, authenticationmethods keyboard-interactive:pam, kbdinteractiveauthentication yes, passwordauthentication no, maxauthtries 1 and logingracetime 360 for the user and relevant source addresses. Compare effective settings of unprotected users with the previous configuration. If anything differs from intent, restore the backups and do not reload. Reload the distribution's SSH service only after the checks pass.

  5. From a separate terminal, verify password then Telegram code and/or TOTP, wrong password, wrong code, used TOTP, and that a public key alone cannot enter. Keep the recovery session open until verified. If a check fails, restore the saved SSH/PAM files and reload SSH from the console. If you remove a user from the group, that user returns to the ordinary SSH policy: this disables their second factor.

sshd -T does not prove that your PAM stack requires a password or that all real network paths are protected. Test on the actual system and audit panel or package updates which could replace the configuration. The factor is only as safe as the root-owned configuration, host, bot account, and recovery channels.

Development checks

Run python3 -m unittest discover -s tests -p test_duo.py -v and python3 tests/pam_check.py. tests/ssh_e2e.py uses a private mount namespace and overlayfs, temporary users and an isolated sshd; it requires root, mount and UID/GID capabilities and Python pexpect. Run it only in an appropriate disposable test environment. No real secrets belong in the checkout.

License

The project code and documentation are available under the MIT License. External dependencies, including Linux-PAM and the optional pexpect test dependency, are distributed separately under their own licenses.

S
Description
Linux OpenSSH second factor - preview
Readme MIT
45 KiB
0 Stars 1 Watchers 0 Forks
Languages
Python 71.6%
C 19.8%
Shell 7.4%
Makefile 1.2%