
Creating users in Linux lets you set up specific user accounts responsible for specific tasks on a Linux system. Standard users are non-privileged users without any special administrative permissions, while privileged users (also known as sudo users) have full permissions to perform multiple tasks on a Linux system. Creating users ensures efficient security, organization, and efficient access control in a multi-user environment where each user has unique credentials, permissions, and file system space.
This article explains how to create users in Linux. You will create both standard user accounts and sudo users while verifying the user information to perform tasks on your Linux workstation.
Prerequisites
Before you begin, you need to:
- Have access to a Linux-based instance as root or a non-root user with sudo privileges.
Create a User in Linux
Creating users in Linux requires superuser (sudo) privileges. You can create a new user using the useradd
or adduser
commands, depending on your Linux distribution. Follow the steps below to create a non-root standard user account using the useradd
command in Linux.
Run the
useradd
command, specifying the user to create, such asexample-user
.console$ sudo useradd -m example-user
The above command creates a new user
example-user
, with a home directoryUse the
adduser
command on Debian-based systems such as Ubuntu to create a new user with an interactive list of options.console$ sudo adduser example-user
The above command automatically creates a new user group and home directory. Enter a strong user password when prompted.
Use the
-M -s /usr/sbin/nologin
options with theuseradd
command to create a user without a home directory and a login shell.console$ sudo usermod -M -s /usr/sbin/nologin example-user
You can later modify the
/etc/passwd
as a sudo user to manually assign the user a login shell.
Run the
passwd
command to set the new user password.console$ sudo passwd example-user
Enter a strong user password and confirm it when prompted.
New password: Retype new password: passwd: password updated successfully
List the
/home
directory and verify that the user's home directory is available.console$ ls /home/
Your output should be similar to the one below.
example-user ....
View the
/etc/passwd
contents and verify that user information.console$ cat /etc/passwd | grep example-user
Output:
example-user:x:1001:1001::/home/example-user:/bin/sh
The above string specifies the user information, IDs, home directory, and the default shell, including:
example-user
: The username.x
: A placeholder for an encrypted password stored in the/etc/shadow
file.1001
: User ID (UID).1001
: Group ID (GID).::
User description./home/example-user
: The user's home directory./bin/sh
: The user's default shell environment./bin/sh
specifies the defaultsh
shell, while a value such as/bin/bash
specifies the bash shell.
Create a Sudo User in Linux
A sudo user is a regular user in Linux with super user do (sudo) privileges. A sudo user can perform administrative tasks on a Linux system using the sudo
command, enabling access to the entire file system. Accessing a Linux workstation as root
exposes the system to security threats associated with super user tasks. Creating a sudo user in Linux lets you perform administrative tasks without logging in as the root
user. Follow the steps below to create a sudo user or convert an existing user into a sudo user in Linux.
Create a new user such as
linuxuser
to grant sudo privileges.console$ sudo useradd -m linuxuser
Use
adduser
on Debian-based systems.console$ adduser linuxuser
Use the
usermod
command with the-aG
option, specifying the sudo users group and the target user to grant sudo privileges:On Debian-based systems such as Ubuntu, use the
adduser
command and thesudo
group, granting the user sudo privileges.console$ sudo usermod -aG sudo linuxuser
On RHEL-based systems such as CentOS, Rocky Linux, and Alma Linux, use the
wheel
group, granting the user sudo privileges.console$ sudo usermod -aG wheel example-user
On other Linux systems such as OpenSuse, use the
wheel
group, granting the user sudo privileges.console$ sudo usermod -aG wheel example-user
Within the above command:
-aG
: Specifies the group to add the user without removing them from other groups.sudo
: The group to add the user.
Switch Users in Linux
You can switch users in Linux using the su
(substitute user) command, specifying the user to switch. The -
option switches to the user's home directory and loads the user's shell environment. Non-sudo users need the user's password to switch to the user, while using sudo su
does not prompt you for a password to switch to a user.
Use the
su -
command, specifying the user to switch to in Linux.console$ su - linuxuser
Enter the user's password when prompted.
Password:
Within the above command:
su
: Switches users in Linux.-
: Loads the user’s environment variables and profile.
List files in the
/root
directory to verify that a user has sudo privileges.console$ sudo ls /root
Enter the sudo user's password when prompted.
[sudo] password for linuxuser:
The command is executed if the user has the
sudo
privileges, or thesudo
action is logged and reported in the system logs if the user does not have sudo privileges.
View User Information in Linux
You can view user information in Linux, including the username, group, and ID information, to use with specific tasks such as changing file permissions. Follow the steps below to view the active user information on your Linux workstation.
Run the
whoami
command to view the username of the active logged-in user.console$ whoami
Verify the active username in your output, similar to the one below.
linuxuser
Use the
groups
command to view all groups a user belongs to.console$ groups linuxuser
Output:
linuxuser : linuxuser sudo
The above group information output confirms that the user
linuxuser
is a member of thelinuxuser
andsudo
groups.Run the
id
command to view the user's default group.console$ id linuxuser
The above command displays the user ID (UID), group ID (GID), and the groups the user belongs to. The GID corresponds to the default group.
uid=1001(linuxuser) gid=1001(linuxuser) groups=1001(linuxuser),27(sudo)
Conclusion
You have created users in Linux. You created regular and sudo users with secure password authentication and verified the user information. Managing Linux users is crucial for maintaining the security and organization of a Linux system. Ensure to use strong passwords and only grant sudo privileges to trusted users to perform administrative tasks such as installing applications on your Linux workstation.
No comments yet.