Passwordless sudo using a YubiKey
21 Nov 2024Introduction
YubiKeys are excellent multi-factor authentication (MFA) devices that can enhance your online security while simplifying your daily workflows on Linux.
In this article, we’ll walk through the process of configuring a YubiKey for secure authentication, including setting up passwordless sudo
or enabling two-factor authentication (2FA) for elevated privileges.
Setup
Prerequisites
First, ensure you have the libpam-u2f
package (or its equivalent for your Linux distribution) installed. On Debian-based systems, use the following command:
U2F (Universal 2nd Factor) is an open standard for hardware MFA keys, and integration with Linux is made possible through Yubico’s pam-u2f module.
Adding Your YubiKey
To link your YubiKey with your system, follow these steps:
-
Connect your YubiKey: Insert the device into your computer.
-
Create the configuration directory: If it doesn’t already exist, create the directory
~/.config/Yubico
:
- Register your YubiKey: Add the key to the list of accepted devices by running:
If you’ve set a PIN for your YubiKey, you may be prompted to enter it.
- Add additional keys (optional): If you have other YubiKeys, you can add them as follows:
Ensure there are no extra newlines between entries in the ~/.config/Yubico/u2f_keys
file.
Configuring sudo
After setting up your key(s), you can configure sudo
to use them for authentication.
Enabling Passwordless sudo
To make sudo
passwordless:
- Edit your
/etc/sudoers
file: Add a line like this:
Ensure your user is part of the wheel
group.
- Modify
/etc/pam.d/sudo
: Add the following line before@include common-auth
:
This configuration makes YubiKey authentication sufficient for sudo
, bypassing the need for a password.
Enabling 2FA for sudo
To enable 2FA, where both your password and YubiKey are required:
- Edit
/etc/pam.d/sudo
: Add the following line after@include common-auth
:
This ensures the usual password authentication is followed by YubiKey verification.
Troubleshooting
Before closing the terminal window where you’re editing /etc/pam.d/sudo
, always confirm that your changes work as expected.
Enable Debugging
If something isn’t working, add debug
to the auth
line in /etc/pam.d/sudo
to enable detailed logging during authentication:
The additional logs can help identify configuration issues.
Conclusion
Adding a YubiKey to your Linux authentication setup enhances security and can simplify your workflow by reducing the need to frequently enter passwords. Whether you choose passwordless authentication or 2FA, YubiKeys are a valuable tool for improving your overall security posture.