The Raspberry Pi is everywhere now, which is why it’s caught the eye of threat actors and cybercriminals. We’ll show you how to secure your Pi with two-factor authentication.
The Amazing Raspberry Pi
The Raspberry Pi is a single-board computer. It launched in the U.K. in 2012 with the intent of getting children to tinker with, create, and learn code. The original form factor was a credit-card-sized board, powered by a phone charger.
It provides HDMI output, USB ports, network connectivity, and runs Linux. Later additions to the line included even smaller versions designed to be incorporated in products or run as headless systems. Prices range from $5 for the minimalist Pi Zero, to $75 for the Pi 4 B/8 GB.
Its success has been incredible; over 30 million of these tiny computers have sold worldwide. Hobbyists have done amazing and inspiring things with them, including floating one to the edge of space and back on a balloon.
Alas, once a computing platform becomes sufficiently widespread it inevitably attracts the attention of cybercriminals. It’s dreadful to think of how many Pi’s are using the default user account and password. If your Pi is public-facing and accessible from the internet by Secure Shell (SSH), it must be secure.
Even if you don’t have any valuable data or software on your Pi, you need to protect it because your Pi isn’t the actual target—it’s just a way to get into your network. Once a threat actor has a foothold in a network, he’ll pivot to the other devices in which he’s actually interested.
Authentication—or gaining access to a system—requires one or more factors. Factors are categorized as the following:
- Something you know: Such as a password or -phrase.
- Something you have: Like a cell phone, physical token, or dongle.
- Something you are: A biometric reading, like a fingerprint or retinal scan.
Multifactor authentication (MFA) requires a password, and one or more items from the other categories. For our example, we’re going to use a password and cell phone. The cell phone will run a Google authenticator app, and the Pi will run a Google authentication module.
A cell phone app is linked to your Pi by scanning a QR code. This passes some seed information to your cell phone from the Pi, ensuring their number-generation algorithms produce the same codes simultaneously. The codes are referred to as time-based, one-time passwords (TOTP).
When it receives a connection request, your Pi generates a code. You use the authenticator app on your phone to see the current code, and then your Pi will ask you for your password and authentication code. Both your password and the TOTP must be correct before you’re allowed to connect.
Configuring the Pi
If you usually SSH onto your Pi, it’s likely it’s a headless system, so we’ll configure it over an SSH connection.
It’s safest to make two SSH connections: one to do the configuring and testing, and another to act as a safety net. This way, if you lock yourself out of your Pi, you’ll still have the second active SSH connection active. Changing SSH settings won’t affect an in-progress connection, so you can use the second one to reverse any changes and remedy the situation.
If the worst happens and you’re completely locked out via SSH, you’ll still be able to connect your Pi to a monitor, keyboard, and mouse, and then log in to a regular session. That is, you can still sign in, as long as your Pi can drive a monitor. If it can’t, however, you really need to keep the safety net SSH connection open until you’ve verified that two-factor authentication is working.
The ultimate sanction, of course, is to reflash the operating system onto the Pi’s micro SD card, but let’s try to avoid that.
First, we need to make our two connections to the Pi. Both commands take the following form:
The name of this Pi is “watchdog,” but you’ll type the name yours instead. If you’ve changed the default username, use that, too; ours is “pi.”
Remember, for safety, type this command twice in different terminal windows so you have two connections to your Pi. Then, minimize one of them, so it’s out of the way and won’t be closed accidentally.
After you connect, you’ll see the greeting message. The prompt will show the username (in this case, “pi”), and the name of the Pi (in this case, “watchdog”).
You need to edit the “sshd_config” file. We’ll do so in the nano text editor:
sudo nano /etc/ssh/sshd_config
Scroll through the file until you see the following line:
Replace the “no” with “yes.”
Press Ctrl+O to save your changes in nano, and then press Ctrl+X to close the file. Use the following command to restart the SSH daemon:
sudo systemctl restart ssh
You need to install the Google authenticator, which is a Pluggable Authentication Module (PAM) library. The application (SSH) will call the Linux PAM interface, and the interface finds the appropriate PAM module to service the type of authentication being requested.
Type the following:
sudo apt-get install libpam-google-authenticator
Installing the App
The Google Authenticator app is available for iPhone and Android, so just install the appropriate version for your cell phone. You can also use Authy and other apps that support this type of authentication code.
Configuring Two-Factor Authentication
In the account you’ll be using when you connect to the Pi via SSH, run the following command (do not include the
You’ll be asked if you want the authentication tokens to be time-based; press Y, and then hit Enter.
A Quick Response (QR) code is generated, but it’s scrambled because it’s wider than the 80-column terminal window. Drag the window wider to see the code.
You’ll also see some security codes beneath the QR code. These are written to a file called “.google_authenticator,” but you might want to make a copy of them now. If you ever lose the ability to obtain a TOTP (if you lose your cell phone, for example), you can use these codes to authenticate.
You must answer four questions, the first of which is:
Do you want me to update your "/home/pi/.google_authenticator" file? (y/n)
Press Y, and then hit Enter.
The next question asks whether you want to prevent multiple uses of the same code within a 30-second window.
Press Y, and then hit Enter.
The third question asks whether you want to widen the window of acceptance for the TOTP tokens.
Press N in answer to this, and then press Enter.
The last question is: “Do you want to enable rate-limiting?”
Type Y, and then hit Enter.
You’re returned to the command prompt. If necessary, drag the terminal window wider and/or scroll up in the terminal window so you can see the entire QR code.
On your cell phone open the authenticator app, and then press the plus sign (+) at the bottom-right of the screen. Select “Scan a QR Code,” and then scan the QR code in the terminal window.
A new entry will appear in the authenticator app named after the hostname of the Pi, and a six-digit TOTP code will be listed beneath it. It’s displayed as two groups of three digits to make reading it easier, but you must type it as one, six-digit number.
An animated circle beside the code indicates how much longer the code will be valid: a full circle means 30 seconds, a half-circle means 15 seconds, and so on.
Linking It All Together
We’ve got one more file to edit. We have to tell SSH which PAM authentication module to use:
sudo nano /etc/pam.d/sshd
Type the following lines near the top of the file:
#2FA auth required pam_google_authenticator.so
You can also choose when you want to be asked for the TOTP:
- After you’ve entered your password: Type the previous lines below “@include common-auth,” as shown in the image above.
- Before you’re asked for your password: Type the previous lines above “@include common-auth.”
Note the underscores (_) used in “pam_google_authenticator.so,” rather than the hyphens (-) we used earlier with the
apt-get command to install the module.
Press Ctrl+O to write the changes to the file, and then press Ctrl+X to close the editor. We need to restart SSH one final time, and then we’re done:
sudo systemctl restart ssh
Close this SSH connection, but leave the other safety net SSH connection running until we’ve verified this next step.
Make sure the authenticator app is open and ready on your cell phone, and then open a new SSH connection to the Pi:
You should be asked for your password, and then for the code. Type the code from your cell phone without any spaces between the numbers. Like your password, it’s not echoed on the screen.
If everything goes according to plan, you should be allowed to connect to the Pi; if not, use your safety net SSH connection to review the previous steps.
Better Safer Than Sorry
Did you notice the “r” in “safer” above?
Indeed, you’re now safer than you were previously when connecting to a Raspberry Pi, but nothing is ever 100 percent safe. There are ways to circumvent two-factor authentication. These rely on social engineering, man-in-the-middle and man-at-the-endpoint attacks, SIM swapping, and other advanced techniques that, obviously, we’re not going to describe here.
So, why bother with all this if it’s not perfect? Well, for the same reason you lock your front door when you leave, even though there are people who can pick locks—most can’t.
- › How to SSH Into Your Raspberry Pi
- › ExpressVPN Review: An Easy-to-Use and Secure VPN for Most People
- › Why Do I See “FBI Surveillance Van” in My Wi-Fi List?
- › 10 Things Blocking Your Wi-Fi Signal at Home
- › 4 Ways to Ruin Your Smartphone’s Battery
- › Here’s How Mozilla Thunderbird Is Making a Comeback in 2022
- › What Can You Do With the USB Port on Your Router?