Skip to content
Follow
Ubuntu Linux

Secure SSH Access on Ubuntu with Google Authenticator

Richard
Written by
Richard
Feb 25, 2021 Updated Jul 14, 2026 3 min read
How to Enable or Disable Microsoft Defender Cloud Protection
How to Enable or Disable Microsoft Defender Cloud Protection

Securing SSH access on Ubuntu with Google Authenticator adds a second step to logging in, making it much harder for unauthorized people to get onto your computer.

This second step uses a temporary code from your phone, provided by an app like Google Authenticator, along with your normal password. This is called two-factor authentication (2FA).

Using 2FA is a smart way to protect your Ubuntu server, especially if you access it from the internet. You can set this up on versions like Ubuntu 20.04 and 18.04.

⚡ 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

Installing Google Authenticator on Ubuntu is the first step to adding SSH two-factor authentication. This app creates codes on your phone that you’ll use along with your password to log in securely. You’ll need to set it up on your phone first, then link it to your Ubuntu system.

We’ve already covered installing Google Authenticator on Ubuntu and setting it up on your mobile device. Please refer to the guide linked below to avoid repetition.

How to install Google Authenticator on Ubuntu Linux

After setting up the steps above, continue below.

Configure Two-factor SSH

To configure SSH for two-factor authentication, you need to edit its main settings file on Ubuntu. This tells your SSH server to ask for the code from your Google Authenticator app after you enter your password. Opening the file with the right command is the first step to adding this extra security layer.

The Secure Shell (SSH) daemon configuration file, `sshd_config`, on Ubuntu opens using the `sudo nano /etc/ssh/sshd_config` command, allowing you to change SSH settings for better security.

🐧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 the file.

💻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.

Terminal window showing Google Authenticator configuration for Ubuntu SSH access
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
⚠️Warning
Then ensure this is included in the PAM SSH rule file at /etc/pam.d/sshd file.
💻Code
auth   required   pam_google_authenticator.so
⚠️Warning
Exit both files and save your changes, then restart SSH.
🐧Bash / Shell
sudo systemctl restart sshd

That should do it!

Conclusion:

Configuring SSH to accept two-factor authentication with Google Authenticator significantly boosts your server’s security. This process adds an extra layer of protection, requiring both a password and a time-sensitive code from your phone to log in.

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.

📚 Related Tutorials

Setup SSH Key Authentication on Ubuntu
Ubuntu Linux Setup SSH Key Authentication on Ubuntu
Enable Google Authentication on Ubuntu
Ubuntu Linux Enable Google Authentication on Ubuntu
How to Install GNOME Desktop on Ubuntu 24.04
Ubuntu Linux How to Install GNOME Desktop on Ubuntu 24.04
How to Install KDE Desktop on Ubuntu 24.04
Ubuntu Linux How to Install KDE Desktop on Ubuntu 24.04

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

Leave a Comment

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