Home Linux Server How to configure SSH Two Factor Authentication on Ubuntu 22.04

How to configure SSH Two Factor Authentication on Ubuntu 22.04

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

Configuring SSH Two Factor Authentication on Ubuntu 22.04 with 3 steps.

Introduction

With SSH Two Factor Authentication, users can increase the security of their servers from the risk of attacks via SSH protocol. And in this article, AZDIGI will guide you on how to install Google Authenticator and configure Two Factor Authentication when SSH into a system using Ubuntu 22.04.

Implementation Guide

Step 1: Install and Configure Google Authenticator

  • To install Google Authenticator, go to your Ubuntu 22.04 server and run the command:
AZDIGI Tutorial
sudo apt install libpam-google-authenticator -y
    
How to configure SSH Two Factor Authentication on Ubuntu 22.04
  • Generate security code

To generate a security key, you enter the following command:

AZDIGI Tutorial
google-authenticator
    

After running the above command, the system will display a link and QR code as shown below:

How to configure SSH Two Factor Authentication on Ubuntu 22.04

If the QR code image is too large, you can copy the link and paste it into the browser to show the QR code easier to see as below:

How to configure SSH Two Factor Authentication on Ubuntu 22.04

Take your phone or device with the Google Authenticator app pre-installed and scan the above QR code. After scanning, you will receive a 6-digit code, and these 6 numbers will be continuously changed every 30 seconds. And the number is only valid when it is blue. Otherwise, if it is red, it will have no effect.

How to configure SSH Two Factor Authentication on Ubuntu 22.04

After having the above 6 numbers, you proceed to enter those 6 numbers into the server setup to complete the installation.

How to configure SSH Two Factor Authentication on Ubuntu 22.04

After successfully entering the code, you will receive an emergency code and need to verify some of the information below:

Code confirmed
Your emergency scratch codes are:
  32788307
  11480031
  78992160
  60171886
  58985147

Do you want me to update your "/root/.google_authenticator" file? (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) y

Step 2: Configure OpenSSH to use 2FA

  • Edit the configuration file at /etc/pam.d/sshd

First, backup the file with the command:

AZDIGI Tutorial
sudo cp /etc/pam.d/sshd /etc/pam.d/sshd.bak
    

Next, you edit the file with the command:

AZDIGI Tutorial
sudo nano /etc/pam.d/sshd 
    

Add the content below at the end of the file and save it.

auth required pam_google_authenticator.so nullok
auth required pam_permit.so
How to configure SSH Two Factor Authentication on Ubuntu 22.04

  • Edit the configuration file at /etc/ssh/sshd_config

First, backup the file with the command:

AZDIGI Tutorial
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
    

Next, you edit the file with the command:

AZDIGI Tutorial
sudo nano /etc/ssh/sshd_config
    

Then you find the ChallengeResponseAuthentication value and set it to yes; if not, you can add it manually.

How to configure SSH Two Factor Authentication on Ubuntu 22.04

Restart the service

AZDIGI Tutorial
sudo systemctl restart sshd.service
    

Step 3: SSH into the server to check

After the configuration completes the steps, you now proceed to SSH into your Ubuntu 22.04 server to check the results. And as shown below, you can see that after you enter the VPS root password, the system will ask you to enter the 2-factor authentication code. So now you just need to go to the Google Authenticator app to get the code and enter it to be able to SSH successfully.

How to configure SSH Two Factor Authentication on Ubuntu 22.04

You can refer to other instructions at the link below:

If you need support, you can contact support in the ways below:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign