Pre-requisites:
A system with CentOS Stream 9 installed and running
Access to a terminal or command line
Root or sudo privileges
In this article, we will discuss how to use the useradd command to create new user accounts in Linux and Unix systems.
It’s common linux security practice to disable root logins and create a separate administrative account with sudo privileges to run commands with root privileges as attackers usually target the SSH and MySQL root account.
What are the types of Linux user accounts?
Root users: has unrestricted access to all commands and files on the system
Regular user accounts: has moderate privileges and cannot change or delete other users’ files
Create new Linux users with the useradd command
useradd username
The command adds an entry to /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow
Next, you need to set the user password. Type the following command
passwd username
If you add your users to the wheel group, they’ll have the power of a root user.
usermod -aG wheel username
You can verify that the user was created. Run the following command to show the user and group names and numeric IDs.
id username
The users’ and groups’ sudo privileges are configured in the /etc/sudoers file.
The cat command outputs the entire file with all the users on the system
cat -n /etc/passwd
The -n cat option numbers all output lines
Use the awk command to list the usernames only
awk -F':' '{ print $1}' /etc/passwd
A simplified command to find out whether a user exists in the Linux server
getent passwd {username}
Get the user accounts count on your system
getent passwd | wc -l
To display help, we type the following command
useradd --help
To set an expiry date for a specific user, you can use the usermod command followed by the -e flag, then the expiry date in yyyy-mm-dd format.
usermod -e 2024-10-08 username
Type the following command to verify if the expiration date is set correctly.
chage -l username
Delete user account. The -r option removes the home directory and mail spool of given user account.
userdel -r username
Verify that the user is successfully deleted
id username