Secure SSH Access on Ubuntu with Google Authenticator

Richard
Written by
Richard
Feb 25, 2021 Updated Mar 18, 2026 2 min read
How to Enable or Disable Microsoft Defender Cloud Protection

You secure SSH access on Ubuntu with Google Authenticator by implementing two-factor authentication (2FA).

Two-factor authentication adds an extra layer of security beyond just your username and password, requiring a time-sensitive code generated by an authenticator app.

This method is crucial for protecting your Ubuntu servers, especially when accessed remotely. For example, you can set this up on Ubuntu 20.04 or 18.04.

Google Authenticator provides these one-time passwords (OTPs) directly from your smartphone.

⚡ Quick Answer

Secure SSH on Ubuntu by enabling two-factor authentication with Google Authenticator. After installing the app, edit /etc/ssh/sshd_config to enable ChallengeResponseAuthentication and UsePAM. Then, edit /etc/pam.d/sshd to add auth required pam_google_authenticator.so, and restart the SSH service.

Install Google Authenticator

Before configuring the SSH server to enable two-factor or multi-factor access, you must install Google Authentication.

Since we’ve already shown you how to install Google Authenticator Ubuntu and set it up on your mobile device, please reference the post below so we don’t write it again.

How to install Google Authenticator on Ubuntu Linux

After setting up the steps above, continue below.

Configure Two-factor SSH

Now that you have installed Google Authenticator on Ubuntu and your mobile device, continue below to configure the SSH server to use it.

To set up SSH, run the commands below to open its default configuration file on Ubuntu.

🐧Bash / Shell
sudo nano /etc/ssh/sshd_config

Next, make the highlighted changes in the file to make this work.

💻Code
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes              
#StrictModes yes
MaxAuthTries 3

#MaxSessions 10

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes


# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

Save and exit.

Next, run the commands below to open Ubuntu’s PAM SSH configuration file.

🐧Bash / Shell
sudo nano /etc/pam.d/sshd

Then append the highlighted changes below and save.

💻Code
# PAM configuration for the Secure Shell service

# Standard Un*x authentication.
@include common-auth
# Disallow non-root logins when /etc/nologin exists.
account    required     pam_nologin.so

auth   required   pam_google_authenticator.so

Save the file and exit.

After making the changes above, restart the SSH service.

🐧Bash / Shell
sudo systemctl restart sshd

Now, go and test it out. You should be prompted for a one-time code every time you attempt to sign in.

ubuntu google authenticator ssh

If you set up SSH public key authentication, you’ll want to add this line to the main SSH configuration file at the/etc/ssh/sshd_config file.

💻Code
AuthenticationMethods publickey,keyboard-interactive

Then ensure this is included in the PAM SSH rule file at /etc/pam.d/sshd file.

💻Code
auth   required   pam_google_authenticator.so

Exit both files and save your changes, then restart SSH.

🐧Bash / Shell
sudo systemctl restart sshd

That should do it!

Conclusion:

This post showed you how to configure SSH to accept two-factor authentication using Google Authenticator.

If you find any error above, please use the form below to report.

You may also like the post below:

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *

Exit mobile version