The useradd
command is used to create new user accounts in Linux. This command allows system administrators to quickly and easily add new users, set up their home directories, and define their default settings.
Initial Example
To create a new user account named john
, you would use:
useradd josh
USERADD Parameters
Parameter | Description |
---|---|
-b, --base-dir BASE_DIR | Specify the base directory for the user’s home directory if the -m option is used. |
-c, --comment COMMENT | Add a comment (usually the user’s full name) to the new user account. |
-d, --home HOME_DIR | Specify the home directory for the new user account. |
-D, --defaults | Display or change the default values for user creation. |
-e, --expiredate EXPIRE_DATE | Set the account expiry date in YYYY-MM-DD format. |
-f, --inactive INACTIVE | Set the number of days after a password expires until the account is permanently disabled. |
-g, --gid GROUP | Set the primary group for the new user account. |
-G, --groups GROUPS | Specify a comma-separated list of supplementary groups for the new user account. |
-k, --skel SKEL_DIR | Specify an alternative skeleton directory to use when creating the user’s home directory. |
-K, --key KEY=VALUE | Override /etc/login.defs defaults. |
-l, --no-log-init | Do not add the user to the lastlog and faillog databases. |
-m, --create-home | Create the user’s home directory if it does not exist. |
-M | Do not create the user’s home directory, even if the default behavior is to create it. |
-N, --no-user-group | Do not create a user group for the new user account. |
-o, --non-unique | Allow the creation of a user with a non-unique user ID (UID). |
-p, --password PASSWORD | Set the initial password for the new user account. |
-r, --system | Create a system account instead of a regular user account. |
-s, --shell SHELL | Specify the login shell for the new user account. |
-u, --uid UID | Set the user ID (UID) for the new user account. |
-U, --user-group | Create a group with the same name as the user account. |
-Z, --selinux-user SEUSER | Specify the SELinux user for the new user account. |
--help | Display help information and exit. |
--version | Display version information and exit. |
Examples
1. Creating a User with a Home Directory
To create a new user and automatically create a home directory, use the -m
option:
useradd -m josh
2. Specifying the User’s Shell
To create a new user with a specific login shell, use the -s
option:
useradd -s /bin/bash josh
3. Setting the User’s UID and GID
To create a new user with a specific user ID (UID) and group ID (GID), use the -u
and -g
options:
useradd -u 1001 -g 100 josh
4. Adding the User to Supplementary Groups
To add the new user to supplementary groups, use the -G
option:
useradd -G wheel,developers josh
5. Setting an Expiry Date for the User Account
To set an expiry date for the user account, use the -e
option followed by the date in YYYY-MM-DD
format:
useradd -e 2025-12-31 josh