Adding 2FA to SSH provides an extra layer of security beyond keys or passwords.
Install Google Authenticator
apt install libpam-google-authenticator -yConfigure for Your User
google-authenticatorAnswer yes to time-based tokens, update the .google_authenticator file, disallow reuse, and allow time skew.
Configure PAM
Edit /etc/pam.d/sshd, add at the top:
auth required pam_google_authenticator.soConfigure SSH
Edit /etc/ssh/sshd_config:
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactivesystemctl restart sshdNow SSH requires both your key and a TOTP code.