The useradd
command is used to create a new user account on a Linux system. It allows the system administrator to specify various attributes for the new user, such as home directory, shell, and group memberships.
-
Syntax:
useradd [options] username
-
Examples:
sudo useradd newuser
- Creates a new user account named
newuser
with default settings.
sudo useradd -m -s /bin/bash newuser
- Creates a new user account named
newuser
, with a home directory and Bash as the default shell.
sudo useradd -G sudo newuser
- Creates a new user account named
newuser
and adds them to thesudo
group.
- Creates a new user account named
-
-m
or--create-home
:- Creates the user's home directory if it does not exist.
sudo useradd -m newuser
- Creates a home directory
/home/newuser
for the usernewuser
.
-
-s [shell]
or--shell [shell]
:- Specifies the user's login shell.
sudo useradd -s /bin/bash newuser
- Sets
/bin/bash
as the default shell fornewuser
.
-
-G [group,...]
or--groups [group,...]
:- Adds the user to one or more supplementary groups.
sudo useradd -G sudo,users newuser
- Adds
newuser
to thesudo
andusers
groups.
-
-d [dir]
or--home [dir]
:- Specifies the home directory for the user.
sudo useradd -d /custom/home newuser
- Sets
/custom/home
as the home directory fornewuser
.
-
-c [comment]
or--comment [comment]
:- Adds a comment (often the full name of the user) to the account.
sudo useradd -c "John Doe" newuser
- Sets "John Doe" as the GECOS (comment) field for
newuser
.
-
-e [date]
or--expiredate [date]
:- Sets the account expiration date.
sudo useradd -e 2024-12-31 newuser
- Expires the account
newuser
on December 31, 2024.
-
-f [days]
or--inactive [days]
:- Specifies the number of days after a password expires until the account is disabled.
sudo useradd -f 30 newuser
- Disables
newuser
's account 30 days after the password expires.
-
--help
:- Displays help information about the
useradd
command.
useradd --help
- Shows usage information and available options.
- Displays help information about the
-
Creating a User with Default Settings:
- Simply use
useradd
followed by the username.
sudo useradd username
- Creates the user with default settings, but without a home directory or password.
- Simply use
-
Creating a User with a Home Directory and Shell:
- Use the
-m
and-s
options to create a more fully configured user.
sudo useradd -m -s /bin/zsh username
- Creates a user with a home directory and sets the Zsh shell as default.
- Use the
-
Adding a User to Groups:
- Use the
-G
option to add the user to multiple groups during account creation.
sudo useradd -G wheel,docker username
- Adds the user to the
wheel
anddocker
groups.
- Use the
-
Setting an Expiration Date:
- Use
-e
to specify when the user account should expire.
sudo useradd -e 2025-01-01 username
- Expires the account on January 1, 2025.
- Use
The useradd
command is a powerful tool for creating new user accounts on a Linux system. It allows for significant customization during account creation, including setting home directories, shells, group memberships, and more. Understanding the key options of useradd
can help efficiently manage users in a Linux environment.