The useradd
command in Linux is used to create new user accounts. With the -D
or --defaults
option, you can manage the default settings used when creating new users.
Example 1: Setting default options for user creation:
useradd -D -m -s /bin/bash
Explanation: This command sets the default behavior for user creation to create a home directory (-m
) and set the default shell to /bin/bash
(-s /bin/bash
).
To verify: Check the output of grep "^USERADD" /etc/default/useradd
to see the default options.
Example 2: Modifying default options for user creation:
useradd -D -s /usr/sbin/nologin
Explanation: This command changes the default shell for new users to /usr/sbin/nologin
, which prevents them from logging into the system.
To verify: Check the output of grep "^SHELL" /etc/default/useradd
to confirm the default shell.
Example 3: Disabling user home directory creation by default:
useradd -D -m
Explanation: This command ensures that user home directories are created by default (-m
), which is the default behavior.
To verify: Check the output of grep "^CREATE_HOME" /etc/default/useradd
to ensure home directories are being created.
Example 4: Setting default expiration for new user accounts:
useradd -D -e 2025-12-31
Explanation: This command sets the default expiration date for new user accounts to December 31, 2025.
To verify: Check the output of grep "^EXPIRE" /etc/default/useradd
to see the default expiration date.
Example 5: Configuring default UID and GID for new users:
useradd -D -u 2000 -g 2000
Explanation: This command sets the default UID and GID for new users to 2000.
To verify: Check the output of grep "^UID_MIN" /etc/login.defs
and grep "^GID_MIN" /etc/login.defs
to confirm the default UID and GID.
Example 6: Restricting default password aging settings:
useradd -D -f 30 -I 7
Explanation: This command sets the default maximum password age to 30 days and the default minimum password age to 7 days.
To verify: Check the output of grep "^PASS_MAX_DAYS" /etc/login.defs
and grep "^PASS_MIN_DAYS" /etc/login.defs
to verify the default password aging settings.
Example 7: Setting default group membership for new users:
useradd -D -G developers
Explanation: This command adds new users to the developers
group by default.
To verify: Check the output of grep "^GROUPADD" /etc/default/useradd
to confirm the default group membership.
Example 8: Configuring default skel directory for new user profiles:
useradd -D -k /etc/skel
Explanation: This command sets the default skeleton directory (/etc/skel
) for new user profiles.
To verify: Check the output of grep "^SKEL" /etc/default/useradd
to confirm the default skel directory.
Example 9: Specifying default umask settings for new users:
useradd -D -U 0022
Explanation: This command sets the default umask for new users to 0022
.
To verify: Check the output of grep "^UMASK" /etc/login.defs
to verify the default umask settings.
Example 10: Setting default login classes for new users:
useradd -D -c staff
Explanation: This command assigns new users to the staff
login class by default.
To verify: Check the output of grep "^LOGIN_CLASSES" /etc/default/useradd
to confirm the default login classes.
Discussion about this post