Software Development Process

How to Set Up Two-Factor Authentication on a Raspberry Pi

 tháng 6 16, 2020     No comments   

A Raspberry Pi sitting on a laptop keyboard.
Kiklas/Shutterstock

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.

Two-Factor Authentication

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:

ssh pi@watchdog.local

ssh pi@watchdog.local in a terminal window.

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").

An SSH connection to a Raspberry Pi in a terminal window.

You need to edit the "sshd_config" file. We'll do so in the nano text editor:

sudo nano /etc/ssh/sshd_config

sudo nano /etc/ssh/sshd_config in a terminal window.

Scroll through the file until you see the following line:

ChallengeResponseAuthentication no

Replace the "no" with "yes."

sshd_config file opened in the nano editorwith the ChallengeResponseAuthentication line highlighted, in a terminal window.

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

sudo systemctl restart ssh in a terminal window.

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

sudo apt-get install libpam-google-authenticator in a terminal window.

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.

Google Authenticator App icon on an Android cell phone.

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 sudo prefix):

google-authenticator

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.

Do you want me to update your "/home/pi/.google_authenticator" file? (y/n) in a terminal window.

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.

Do you want to disallow multiple uses of the same authentication token? (y/n) in a terminal window.

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.

Do you want to do so? (y/n) in a terminal window.

The last question is: "Do you want to enable rate-limiting?"

Type Y, and then hit Enter.

Do you want to enable rate-limiting? (y/n) in a terminal window.

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

sudo nano /etc/pam.d/sshd in a terminal window.

Type the following lines near the top of the file:

#2FA
  
  auth required pam_google_authenticator.so

auth required pam_google_authenticator.so added to the sshd file in an editor, in a terminal window.

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

sudo systemctl restart ssh in a terminal window.

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:

ssh pi@watchdog.local

ssh pi@watchdog.local in a terminal window.

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.



See details

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Gửi email bài đăng nàyBlogThis!Chia sẻ lên XChia sẻ lên Facebook

Related Posts:

  • How to Mute Yourself on a Zoom Call While taking part in a video conference using Zoom, sometimes you need to mute your microphone to cough, suppress background noises, or to simpl… Read More
  • ESET NOD-32 ANTIVIRUS Genuine KEY 2 year 1 pc 2020 Digital downlod FULL VERSION ESET NOD-32 ANTIVIRUS Genuine KEY 2 year 1 pc 2020 Digital downlod FULL VERSION Price : 4.75 Ends on : 2 days View on eBay … Read More
  • 69" Portable Clothes Closet Wardrobe Storage Organizer Non-Woven Fabric Cover 69″ Portable Clothes Closet Wardrobe Storage Organizer Non-Woven Fabric Cover Price : 23.69 Ends on : 3 weeks View on eBay … Read More
  • 3-Tier 24" Microwave Stand Storage Kitchen Baker's Rack Utility Microwave Holder 3-Tier 24″ Microwave Stand Storage Kitchen Baker’s Rack Utility Microwave Holder Price : 39.49 Ends on : 3 weeks View on eBay … Read More
  • Genuine Activation Code for Win 10 Version home and link download fast process Genuine Activation Code for Win 10 Version home and link download fast process Price : 4.00 Ends on : 2 weeks View on eBay … Read More
Bài đăng Mới hơn Bài đăng Cũ hơn Trang chủ

0 nhận xét:

Đăng nhận xét

  • Gun Digest Book of the .22 Rifle
  • The Secret Relationship Between Blacks and Jews Volume 1 /2 /3 Physical Books!
  • The Little Book of Hygge: Danish Secrets to Happy Living [The Happiness Institut
  • Adult Color By Numbers Coloring Book: Easy Large Print Mega Jumbo Coloring ...
  • Herbs - A Concise Guide In Colour by Jirasek, Vaclay Hardback Book The Fast Free

Popular Posts

  • Smartphone Using At The Supermarket Can Add 41% To Your Shopping Bill
    It is safe to say that you are always looking at your telephone when you're and about? Do you experience difficulty opposing the bait of...
  • Windows 7 All in One ISO 32-64 Bit Free Download
    Windows 7 all in one ISO 32-64 bit genuine free is now available to download from the secure links provided below. The download comes w...
  • Forgot to post
    sorry travel day.  My bad! 
  • November Technology Updates
    So far, November has been a busy month of technology integration in all grade levels.  Teachers and students use a wide variety of devices i...
  • Morning Charts 04/30/2019 SPX
    Early post
  • Check Out The Science Behind Finding North Korea's Nuclear Weapons
    Arrangements over denuclearization of North Korea fallen at the beginning of today after North Korean despot Kim Jong Un demanded the United...
  • Morning Charts 04/10/2019 SPX
    Cause censorship is real. Our Orwellian really coming to life -  https://www.zerohedge.com/news/2019-04-09/leaked-google-docs-reveals-aggres...
  • Should You Use Hubitat to Automate Your Smarthome?
    The first step in building a smarthome is often choosing a hub, and there are many options. Hubitat is a unique cloud-independent hub. It...
  • Microsoft Staff Don't Use HoloLens For War
    Somewhere around 50 Microsoft representatives have requested the organization pull out of an arrangement with the US military to give expand...
  • Morning Charts 03/19/2019 SPX
    RC wants me to bring back the STB bracket challenge so look for a link to that later today and again tomorrow morning. If you’ve never heard...

Bài đăng nổi bật

How To Swim and Dive in ‘Animal Crossing: New Horizons’

Nintendo Animal Crossing: New Horizons has received a free update that allows players to swim and dive for sea creatures for the firs...



Work freely with Fiverr

Work freely with Fiverr

Money with Adfly

Money with Adfly
Được tạo bởi Blogger.

Make Money MyLead

Make Money MyLead

TẢI PHIM 18+ VỀ ĐIỆN THOẠI Ở ĐÂY >>

Copyright © 2025 Software Development Process | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates