Unix - Creating Users in Unix Using useradd?
In Linux and Unix-like systems, user management is a fundamental administrative task. The useradd command is used to create new user accounts and define their default settings such as home directory, shell, and group membership.
Basic User Creation
To create a new user with default settings, use:
sudo useradd username
This creates an account but does not set a password. You must assign one separately:
sudo passwd username
Common useradd Options
The useradd command provides several options to customize user creation:
-
Create a home directory:
sudo useradd -m username
-
Specify a custom home directory:
sudo useradd -d /home/customdir username
-
Set a default shell:
sudo useradd -s /bin/bash username
-
Add user to a supplementary group:
sudo useradd -G wheel,developers username
-
Set a specific user ID (UID):
sudo useradd -u 1050 username
Understanding What useradd Does Internally
When a user is created, Linux updates several system files:
-
/etc/passwd– user account details -
/etc/shadow– encrypted password information -
/etc/group– group memberships -
/etc/gshadow– secure group data
Default settings are controlled by:
/etc/default/useradd
/etc/login.defs
Verifying the User
To confirm user creation:
id username
or
getent passwd username
Best Practices
-
Always create a home directory using
-m -
Assign strong passwords immediately
-
Use groups instead of direct permission changes
-
Avoid using the root account for regular tasks
In summary, useradd is a powerful and flexible command for creating and managing user accounts in Linux, giving administrators fine-grained control over system access and security.