This article describes how to enable and disable two-factor authentication for SSH on an unmanaged server.
By default, when users access your unmanaged VPS or dedicated server using SSH, they type a username and password to log in.
Two-factor authentication provides an extra layer of security because, in addition to knowing the correct username and password, users must provide another piece of information. This piece of information is a temporary, numeric password generated independently on the server and on a mobile device, such as a smartphone or tablet.
As a result, in order for a potential attacker to access your server, he or she would not only need to obtain something you know (your username and password), but also something you have (your mobile device). This two-factor approach to logins significantly enhances your server's security and makes brute-force attacks much more difficult.
To enable two-factor authentication with SSH on your server, do the following steps in the order in which they are presented.
If you haven't done so already, you should disable root SSH logins on your server and create a normal user account. For information about how to do this, please see this article.
You use an authenticator app on your smartphone or tablet to generate a one-time password that you use to log in (this is the “second factor” for authentication, in addition to your username and password).
To enable two-factor authentication on the server, follow these steps:
apt-get install libpam-google-authenticator
For AlmaLinux and Fedora, type the following command:
yum install google-authenticator
At the command prompt, type the following command:
google-authenticator
The server generates a QR code image and emergency codes. On your mobile device, scan the QR code image and configure the account.
At the By default, tokens are good for 30 seconds… prompt, type n and then press Enter.
As the root user, open the /etc/pam.d/sshd file in your preferred text editor.
auth required pam_google_authenticator.so nullok
As the root user, open the /etc/ssh/sshd_config file in your preferred text editor.
ChallengeResponseAuthentication no
Modify the line as follows:
ChallengeResponseAuthentication yes
As the root user, restart the SSH service:
service ssh restart
For AlmaLinux and Fedora, type the following command:
service sshd restart
While still logged in as the root user, in a separate window log in as the normal user and test the new configuration:
If you decide that you no longer want to use two-factor authentication with SSH, you can disable it. To do this, follow these steps:
auth required pam_google_authenticator.so nullok
Open the /etc/ssh/sshd_config file in your preferred text editor.
ChallengeResponseAuthentication yes
Modify the line as follows:
ChallengeResponseAuthentication no
Restart the SSH service:
service ssh restart
For AlmaLinux and Fedora, type the following command:
service sshd restart
Two-factor authentication is now disabled for SSH.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.