When managing user accounts in Linux, the useradd
command is essential for creating new users. The -K
or --key KEY=VALUE
option allows administrators to override default settings during user creation by specifying key-value pairs. This flexibility is particularly useful for tailoring user configurations to specific needs directly from the command line.
Here are several examples illustrating the use of the -K
option with useradd
:
Example 1: Setting User Home Directory
To create a new user john
with a custom home directory:
useradd -K HOME=/home/customhome john
This command sets the home directory of user john
to /home/customhome
.
Verification: Verify the user’s home directory using:
grep john /etc/passwd
Example 2: Specifying User Shell
Creating a user mary
with a specific shell (/bin/bash
):
useradd -K SHELL=/bin/bash mary
This assigns /bin/bash
as the login shell for user mary
.
Verification: Check the user’s shell setting:
grep mary /etc/passwd
Example 3: Setting User UID
Assigning a custom UID (User ID) 1500
to user alex
:
useradd -K UID=1500 alex
This command sets the UID of user alex
to 1500
.
Verification: Ensure the UID is correctly applied:
grep alex /etc/passwd
Example 4: Setting User Group
Adding user sarah
to a secondary group developers
:
useradd -K GROUPS=developers sarah
This command adds user sarah
to the developers
group.
Verification: Confirm group membership:
groups sarah
Example 5: Disabling User Login
Creating a system user serviceuser
with login disabled:
useradd -K LOGIN=0 serviceuser
This command prevents serviceuser
from logging into the system.
Verification: Verify login status:
grep serviceuser /etc/passwd
Example 6: Setting Account Expiry Date
Setting an expiry date (2025-12-31
) for user peter
:
useradd -K EXPIRE=2025-12-31 peter
This command ensures user peter
‘s account expires on December 31, 2025.
Verification: Check the expiry setting:
chage -l peter
Example 7: Setting User Comment
Adding a comment ("John Doe, IT Support"
) for user jane
:
useradd -K COMMENT="John Doe, IT Support" jane
This adds a descriptive comment to user jane
‘s account.
Verification: View the user’s comment field:
grep jane /etc/passwd
Example 8: Specifying User Password
Setting an initial password for user guest
(not recommended for production):
useradd -K PASSWORD="password123" guest
This sets a non-encrypted password for user guest
.
Verification: Attempt to login with the provided password.
Example 9: Setting User Default Group
Defining staff
as the default group for user mark
:
useradd -K GROUP=staff mark
This makes staff
the primary group for user mark
.
Verification: Check the user’s primary group:
id mark
Example 10: Customizing User GECOS Field
Setting a custom GECOS field ("Jane Doe,Developer,555-1234"
) for user dave
:
useradd -K GECOS="Jane Doe,Developer,555-1234" dave
This command customizes the general user information field for dave
.
Verification: Verify the GECOS field contents:
grep dave /etc/passwd
Discussion about this post