Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Use Two-Factor Authentication with Ubuntu - DesignLinux

Feb 24 2021
designlinux 0 Comments

Over time, the traditional username and password authentication has proven inadequate in providing robust security to applications and systems. Usernames and passwords can easily be cracked using a plethora of hacking tools, leaving your system vulnerable to breaches. For this reason, any company or entity that takes security seriously needs to implement 2-Factor authentication.

Colloquially known as MFA (Multi-Factor Authentication), 2-Factor authentication provides an extra layer of security that requires users to provides certain details such as codes, or OTP (One Time Password) before or after authenticating with the usual username and password.

Nowadays multiple companies such as Google, Facebook, Twitter, and AWS, to mention a few provide users the choice of setting up MFA to further protect their accounts.

In this guide, we demonstrate how you can use Two-Factor Authentication with Ubuntu.

Step 1: Install Google’s PAM Package

First off, install the Google PAM package. PAM, an abbreviation for Pluggable Authentication Module, is a mechanism that provides an extra layer of authentication on the Linux platform.

The package is hosted on the Ubuntu repository, so proceed and use the apt command to install it as follows:

$ sudo apt install libpam-google-authenticator

When prompted, hit 'Y' and press ENTER to continue with the installation.

Install Google PAM in Ubuntu
Install Google PAM in Ubuntu

Step 2: Install Google Authenticator App on Your Smartphone

Additionally, you need to install the Google Authenticator application on your tablet or smartphone. The app will present you with a 6 digit OTP code that auto-renews every 30 seconds.

Google Authenticator
Google Authenticator

Step 3: Configure Google PAM in Ubuntu

With the Google Authenticator app in place, we will proceed and configure the Google PAM package on Ubuntu by modifying the /etc/pam.d/common-auth file as shown.

$ sudo vim /etc/pam.d/common-auth

Append the line below to the file as indicated.

auth required pam_google_authenticator.so
Configure PAM in Ubuntu
Configure PAM in Ubuntu

Save the file and exit.

Now, run the command below to initialize PAM.

$ google-authenticator

This will evoke a couple of questions on your terminal screen. First, you will be asked if you want authentication tokens to be time-based.

Time-based authentication tokens expire after a certain time. By default, this is after 30 secs, upon which a new set of tokens is generated. These tokens are considered more secure than non-time-based tokens, and therefore, type 'y' for yes and hit ENTER.

Configure Google Authenticator
Configure Google Authenticator

Next, a QR code will be displayed on the terminal as shown below and right below it, some information will be displayed. The information displayed includes:

  • Secret key
  • Verification code
  • Emergency scratch codes

You need to save this information to a vault for future reference. The emergency scratch codes are extremely useful in the event you lose your authenticator device. Should anything happen to your authentication device, use the codes.

Launch the Google Authenticator App on your smart device and select ‘Scan QR code’ to scan the QR code presented.

Scan QR Code
Scan QR Code

NOTE: You need to maximize the terminal window in order to scan the entire QR code. Once the QR code is scanned, a six-digit OTP that changes every 30 seconds will be displayed on the App.

QR Codes
QR Codes

Thereafter, Select 'y' to update the Google authenticator file in your home folder.

In the next prompt, restrict login to just one log in every 30 seconds in order to prevent attacks that might arise due to man-in-the-middle attacks. So select 'y'

Update Google Authenticator-File
Update Google Authenticator-File

In the next prompt, Select 'n' to disallow the extension of the time duration which addresses time-skew between server and client. This is the more secure option unless you are experiencing challenges with poor time synchronization.

And finally, enable rate-limiting to only 3 login attempts.

Enable Rate Limiting
Enable Rate Limiting

At this point, we have finished implementing the 2-factor authentication feature. In fact, if you run any sudo command, you will be prompted for a verification code which you can obtain from the Google Authenticator app.

Verify Google Authentication
Verify Google Authentication

You can further verify this by rebooting and once you get to the login screen, you will be requested to provide your verification code.

Ubuntu Google Authentication Login
Ubuntu Google Authentication Login

After you have provided your code from the Google Authenticator app, just provide your password to access your system.

Ubuntu Login Password
Ubuntu Login Password

Step 4: Integrate SSH with Google Authenticator

If you intend to use SSH with the Google PAM module, you need to integrate the two. There are two ways you can achieve this.

For SSH password Authentication

To enable SSH password authentication for a regular user, first, open the default SSH configuration file.

$ sudo vim /etc/ssh/sshd_config

And set the following attributes to ‘yes’ as shown

Enable SSH Password Authentication
Enable SSH Password Authentication

For the root user, set the ‘PermitRootLogin‘ attribute to 'yes'.

PermitRootLogin yes
Permit Root Login
Permit Root Login

Save the file and exit.

Next, modify the PAM rule for SSH

$ sudo vim /etc/pam.d/sshd

Then append the following line

auth   required   pam_google_authenticator.so
SSH PAM Authentication
SSH PAM Authentication

Lastly, restart the SSH service for the changes to come into effect.

$ sudo systemctl restart ssh

In the example below, we are logging in to the Ubuntu system from the Putty client.

Ubuntu SSH Login
Ubuntu SSH Login

For SSH Public-Key Authentication

If you are using public-key authentication, repeat the above steps and add the line shown at the bottom of the /etc/ssh/sshd_config file.

AuthenticationMethods publickey,keyboard-interactive

Once again, edit the PAM rule for the SSH daemon.

$ sudo vim /etc/pam.d/sshd

Then add the following line.

auth   required   pam_google_authenticator.so
SSH Public Key Authentication
SSH Public Key Authentication

Save the file and restart the SSH service as we saw earlier.

$ sudo systemctl restart ssh

And this brings us to the end of this article. We will be glad to hear how it went.

Related

Tags: SSH Tips, Ubuntu Tips

How To Prevent PHP-FPM From Consuming Too Much RAM in Linux

Prev Post

Duf – A Better Linux Disk Monitoring Utility

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 261
  • 574,751

DesignLinux.com © All rights reserved

Go to mobile version