Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The useradd
command in Linux is used to create new user accounts on a system. It sets up various aspects of a user's account, such as their home directory, user ID, default shell, and more. In this tutorial, we will cover how to use the useradd
command and its various options.
Adding a new user
To create a new user, simply run the useradd
command followed by the desired username:
sudo useradd newuser
Replace newuser
with the desired username. This command creates a new user with default settings, which usually includes the following:
/home/newuser
/bin/bash
or /bin/sh
)Creating a new user with a specific home directory
To create a user with a custom home directory, use the -d
flag followed by the desired directory path:
sudo useradd -d /custom/home newuser
Creating a new user with a specific user ID (UID)
To create a user with a specific user ID (UID), use the -u
flag followed by the desired UID:
sudo useradd -u 1005 newuser
Creating a new user with a specific group ID (GID)
To create a user with a specific group ID (GID), use the -g
flag followed by the desired GID:
sudo useradd -g 1005 newuser
Creating a new user with a specific default shell
To create a user with a specific default shell, use the -s
flag followed by the desired shell:
sudo useradd -s /bin/tcsh newuser
Creating a new user with a specific comment
To add a comment (e.g., the full name of the user) to the user's account, use the -c
flag followed by the comment text:
sudo useradd -c "New User" newuser
Creating a new user without a default group
By default, a new group with the same name as the user is created. To disable this behavior and add the user to a pre-existing group, use the -N
flag:
sudo useradd -N newuser
Creating a user account with multiple options
You can combine multiple options to create a user account with specific settings:
sudo useradd -d /custom/home -u 1005 -g 1005 -s /bin/tcsh -c "New User" newuser
Setting a password for the new user
After creating a new user, you should set a password for them using the passwd
command:
sudo passwd newuser
You will be prompted to enter the new password twice.
In conclusion, the useradd
command is an essential tool for creating and managing user accounts in Linux. By combining various options, you can create user accounts tailored to specific requirements and settings.
How to use the Linux useradd
command:
useradd
command is used to add new user accounts in Linux.# Example: Adding a new user sudo useradd newuser
Adding a new system user in Linux:
-r
option can be used to create a system user.# Example: Adding a system user sudo useradd -r sysuser
Setting user properties during user creation:
# Example: Setting user properties during creation sudo useradd -u 1001 -g 1001 newuser
Assigning a home directory and shell to a new user:
-m
option ensures the creation of the user's home directory, and the -s
option sets the login shell.# Example: Assigning home directory and shell sudo useradd -m -s /bin/bash newuser
Adding users to specific groups with useradd
:
-G
option allows adding users to specific groups during user creation.# Example: Adding a user to a specific group sudo useradd -G group1,group2 newuser
Creating a user with an expiration date in Linux:
-e
option sets an expiration date for the user account.# Example: Creating a user with expiration date sudo useradd -e 2023-12-31 newuser
Password management while using useradd
:
passwd
command after creating the user. Alternatively, the -p
option can be used to set an encrypted password.# Example: Setting a password for a new user sudo passwd newuser
useradd
examples for various scenarios in Linux:
useradd
can be customized for different scenarios, combining options to suit specific requirements.# Example: Creating a user with specific options sudo useradd -m -G group1 -s /bin/bash newuser
Troubleshooting common issues with useradd
:
# Example: Troubleshooting useradd issues sudo useradd newuser