29.2 Account Authentication Security
FreeBSD implements account security management through the /etc/master.passwd master password file and the pam_unix.so authentication module. This section covers the password database generation process, account locking and nologin-based login prevention, SHA-512 hash verification, and pam_passwdqc password policy configuration.
Password File Directory Structure
/etc/master.passwd is the master password file, readable only by root, containing users' encrypted passwords.
/etc/passwd is retained only for compatibility, automatically generated by pwd_mkdb(8), with the password field replaced by *.
/etc/master.passwd
| |
| pwd_mkdb(8)
v
----------------------------
Database generation process
----------------------------
| |
| |
v v v
/etc/pwd.db /etc/spwd.db /etc/passwd
(db(3) format) (db(3) format) (ASCII password compatibility file)
(no password) (contains password) (no password)To ensure data consistency, all password files should not be manually edited; use tools such as vipw(8), chpass(1), or pw(8) to make modifications.
Preventing Login
The primary measure to protect a system is to audit accounts and disable all accounts that do not need to log in.
Discussion
Ensure that
roothas a strong password, rotate it regularly, and never share this password.This advice is popular but carries security risks:
Strong passwords are complex and difficult to remember, causing users to rely on insecure storage methods (such as paper notes or text files), which paradoxically increases security risks.
Regular password changes increase management difficulty, and the trustworthiness of "password management tools" themselves is also difficult to guarantee.
Key-based authentication provides some security assurance, but if a key is lost, identity verification becomes more complex.
Multi-factor authentication (MFA) can enhance security, but if the dependent device is lost or the application is uninstalled, similar problems arise.
There are two methods to deny account login, as described below.
First, lock the account. The following demonstrates how to lock the account ykla:
After locking, user ykla attempts to log in:
Another user attempts to switch to ykla:
Warning
Never attempt to lock the root account.
Otherwise, you may need to enter single-user mode to recover.
Unlock user ykla:
Second, set the shell to /usr/sbin/nologin to prevent login. When a user attempts to log in, the nologin shell prevents the system from assigning a shell.
Only root has the authority to change other users' shells:
User ykla is prevented from logging in, with the same effect as the previous example.
To restore login access, reassign the sh shell to user ykla:
Warning
Never attempt to assign /usr/sbin/nologin to the root account.
Password Hashing
Since passwords must be used, they should be sufficiently complex and encrypted with a strong hashing mechanism before being stored in the password database. FreeBSD's crypt() library supports multiple algorithms including SHA-256, SHA-512, and Blowfish.
The default SHA-512 should not be downgraded to a weaker hashing algorithm, nor is it recommended to replace it with Blowfish. The Blowfish algorithm is not part of the AES family and does not comply with Federal Information Processing Standards (FIPS) requirements; some environments may fail compliance audits.
Viewing the Hash Algorithm Used for the Current User's Password
To confirm the hash algorithm used by a user, the root user can check the corresponding hash value in the FreeBSD password database. Each hash value begins with a symbol indicating the type of hashing mechanism used to encrypt the password:
DES
No number
MD5
$1$
bcrypt
$2a / $2b / $2y
NT-Hash
$3$
SHA-256
$5$
SHA-512
$6$
Number 4 is unused.
In this example, ykla's password starts with $6$, indicating the use of the default SHA-512 algorithm.
Note that the password database stores encrypted hash values, not the original passwords. The output should resemble the following:
Setting the Default Password Hash in the Login Class
The default value for passwd_format is sha512. Valid values include blf, sha256, sha512 (recommended), as well as des, md5, nth (used only for compatibility with legacy password files and should not be actively chosen; see crypt(3)).
Run the following command to check the current hashing mechanism:
The output should resemble the following:
For example, to switch to Blowfish, modify the line to:
Then, you must run cap_mkdb to update the login.conf database:
Note that this change does not affect existing password hashes. That is, all users need to run passwd to change their password once for the new algorithm to take effect.
Password Policy Enforcement
Implementing strong password policies for local accounts is a fundamental system security configuration. FreeBSD uses the built-in Pluggable Authentication Modules (PAM) to control password length, strength, and complexity.
This section uses the pam_passwdqc module (which takes effect when users change their passwords) to set minimum and maximum password lengths and enforce mixed character types.
Discussion
Mandatory requirements for mixed character passwords, password rotation, and disallowing common words as complexity rules are no longer considered best security practices.
Analyze these two security philosophies.
To configure, first switch to the root user, then uncomment the line containing pam_passwdqc.so in the /etc/pam.d/passwd file.
Then edit the line according to the policy: disable the first through third password classes (including passphrases), only allow four-character-class passwords (>=10 characters) or three-character-class passwords (>=12 characters); new and old passwords must not be similar; allow a maximum of 3 retries; enforce only for regular users (root is not constrained).
Parameter meanings:
min=disabled,disabled,disabled,12,10 corresponds to five password complexity levels:
1
Only one character class
\u2014
Disabled
2
Only two character classes
\u2014
Disabled
3
Passphrase
\u2014
Disabled
4
Includes three character classes
12 characters
Enabled
5
Includes four character classes
10 characters
Enabled
Character classes include: digits, lowercase letters, uppercase letters, and other characters. Non-ASCII characters that cannot be classified are treated as non-digit classes.
After saving the file, users will see a prompt similar to the following when changing their password:
The output should resemble the following:
If the password does not comply with the policy, the system rejects it and displays a warning, for example:
Users can make multiple attempts within the configured retry count.
If an organization requires passwords to expire periodically, FreeBSD can set the passwordtime parameter in the user's login class in /etc/login.conf.
The default login class includes an example:
To set a 90-day expiration for this login class, remove the comment character (#), save, and then run:
To set a password change date (password aging, the password must be changed before the specified date) for a single user ykla, pass both the date (or number of days until expiration) and the username to pw:
The date is set in the format day, month, year (dd-mmm-yy[yy], where the month can be a number or letter abbreviation).
To remove the password change date restriction, set it to 0:
Note
-psets the password change date (password aging), which is a different concept from account expiration. To set an account expiration time, use the-eparameter.
From today, set the specified user ykla's account to expire in 30 days:
From today, set the specified user ykla's account to expire in one year:
Exercises
In FreeBSD, the password hash value stored in the /etc/master.passwd file begins with the
$6$prefix. Research and explain the hash algorithm corresponding to this prefix, and compare its security with the$2b$prefix.After locking a test account using the
pw lockcommand, attempt to log in via both a local terminal and SSH, and record the differences in the error messages from the two attempts.
Last updated