How to Set Up Two-Factor Authentication in Ubuntu

Two Factor Authentication Ubuntu

Two-factor authentication is a great answer to the many problems with traditional passwords. It’s a great way to add an additional level of security to your accounts. Now you can apply two-factor authentication to your Linux desktop.

Install Google Authenticator on Android

To start, you’re going to need the Google Authenticator app for your Android device. It’s a simple app for generating authentication codes that correspond to linked devices.

Install Google Authenticator on Linux

Install Google Authenticator Linux

With Google Authenticator installed on your phone, it’s time to set it up on your Linux desktop. There’s a package that you need to install in order to integrate Linux’s existing authentication system with the Google Authenticator.

sudo apt install libpam-google-authenticator

Configure Linux

Linux Google Authenticator Configuration

Now that you have everything in place, you can start configuring it all to work together. Open up the file at “/etc/pam.d/common-auth” with sudo in your favorite text editor. Locate the line below.

auth    [success=1 default=ignore] nullok_secure

Right above that line, add in this one:

auth required

Save that and close the file.

Every user on the computer will need to run the google-authenticator command. This will run you through the process of setting up the authentication for the user. If you don’t do it, the user won’t be able to log in. After you set up the first one, you can set up the others with sudo su username.


After you run the command, it will begin asking you questions about how you want your authentication set up. The questions explain themselves fairly thoroughly. For security reasons, the recommendation of this guide is to answer: yes, yes, no, no, yes. Of course, you can choose something else, if it’s a better fit for you.

After you answer all the questions, the script will present you with a series of codes and a key. The key is what you will use to set up the Google Authenticator app on Android. The “scratch codes” are one-time-use codes that you can use to access your computer if you don’t have your phone. Print or write those down. You can use the other code immediately, if you need to.

Configure Android

Google Authenticator Android

Open up your Android app and tap on “Begin.” Then, select “Enter a provided key.” Create a name for your account and type in the secret key that the Linux script provided.

Google Authenticator Setup

The Results

After that you’ll need to open up the Google Authenticator app on Android and generate a code every time you’re asked to provide your password on Linux. Your Linux PC is more secure against password-based attacks.  Take a look at what the login screen looks like with two-factor authentication enabled.

Two factor login Linux

You’ll even need the authenticator to use root privileges with sudo, too.

two factor sudo

All of this amounts to much more security for your system, and that’s a good thing.  Of course, you have to decide what the right balance is for you.

Nick Congleton
Nick Congleton

Nick is a freelance tech. journalist, Linux enthusiast, and a long time PC gamer.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox