Two-Factor Authentication
Today we will learn about Two-Factor Authentication (2FA), its importance, and how to set it up for enhanced security.
Explanation
Two-Factor Authentication (2FA) adds an extra layer of security to your accounts by requiring two forms of verification: something you know (password) and something you have (phone, hardware token).
Key Concepts:
Factors: Knowledge (password), Possession (phone/app), Inherence (biometrics).
Methods: SMS, authenticator apps (e.g., Google Authenticator), hardware keys (e.g., YubiKey).
Time-Based One-Time Passwords (TOTP): Codes that expire every 30 seconds.
Technical:
How 2FA Works:
After entering password, the system prompts for a second factor. For TOTP, the app generates a code based on a shared secret and current time.
Security Benefits:
Protects against password breaches, phishing, and brute-force attacks. Even if passwords are stolen, accounts remain secure.
How to setup one properly:
General Best Practices
- Use authenticator apps over SMS (SMS can be intercepted).
- Enable 2FA on all critical accounts.
- Back up recovery codes securely.
- Use hardware keys for maximum security.
Client (Enabling 2FA)
Set up 2FA on your accounts.
Linux/macOS
- Install an authenticator app like Aegis or Authy.
- Ubuntu:
sudo apt install aegis-authenticatoror download from app store. - macOS: Download from App Store.
- For accounts: Go to security settings, enable 2FA, scan QR code with app.
- For hardware: Use YubiKey with Yubico Authenticator.
Windows
- Install Microsoft Authenticator or Google Authenticator from Microsoft Store.
- Enable 2FA in account settings (e.g., Microsoft, Google).
- For hardware: Use YubiKey with Windows Hello or dedicated apps.
Note: Store backup codes in a password manager.
Server/Account Setup
For systems or services you manage:
Linux
- For SSH: Use Google Authenticator PAM module.
- Install:
sudo apt install libpam-google-authenticator - Run
google-authenticatorto set up. - Edit
/etc/pam.d/sshdto include auth required pam_google_authenticator.so - Restart SSH:
sudo systemctl restart ssh
Windows
- Enable 2FA in Active Directory or Azure AD.
- For servers: Use Windows Hello for Business or third-party MFA.
Note: Test 2FA thoroughly to avoid lockouts.
Samples:
Example TOTP Code
123456
(Generated by app, valid for 30 seconds.)
Example QR Code URL (for setup)
otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example
(Scan with authenticator app.)
Example PAM Configuration (/etc/pam.d/sshd)
auth required pam_google_authenticator.so
auth required pam_unix.so
Recommended
- Use apps like Authy or Aegis.
- Prefer hardware keys like YubiKey.
- Avoid SMS-based 2FA.
- Regularly rotate secrets if possible.