How to Create Users in Linux

Adding users in Linux allows you to set up individual accounts dedicated to specific tasks on a system. Regular users operate without special administrative permissions, while privileged users (sudo users) have full rights to execute administrative operations. Managing users helps maintain security, proper organization, and controlled access in a multi-user setup where each user has distinct credentials, permissions, and disk space.

This guide outlines the steps for creating both standard and sudo users in Linux, while also verifying user details to perform operations on your workstation.

Prerequisites

Before starting, ensure you have:

  • Access to a Linux instance either as root or as a user with sudo privileges.

Create a User in Linux

To add users in Linux, you need superuser privileges. Depending on your distribution, you can use useradd or adduser. Below are the methods to create a non-root standard account using the useradd command.

Run the following command to create a user named example-user:

$ sudo useradd -m example-user

This sets up a new user with a home directory.

On Debian-based systems like Ubuntu, use adduser for an interactive process:

$ sudo adduser example-user

This command automatically generates a new group and a home directory. You will be prompted to enter a secure password.

To create a user without a home directory or shell login, use:

$ sudo usermod -M -s /usr/sbin/nologin example-user

You can later edit /etc/passwd to assign a shell manually.

Set the new user password with:

$ sudo passwd example-user

Enter and confirm a strong password. Example:

New password:
Retype new password:
passwd: password updated successfully

Check if the home directory exists:

Output should include:

example-user  ....

Verify user details in /etc/passwd:

$ cat /etc/passwd | grep example-user

Example output:

example-user:x:1001:1001::/home/example-user:/bin/sh

Explanation:

  • example-user: Username
  • x: Placeholder for encrypted password in /etc/shadow
  • 1001: User ID (UID)
  • 1001: Group ID (GID)
  • ::: User description
  • /home/example-user: Home directory
  • /bin/sh: Default shell

Create a Sudo User in Linux

A sudo user is a standard account with elevated privileges to run administrative tasks via sudo. This approach minimizes risk compared to using the root account directly. Follow these steps to create or convert a user into a sudo-enabled account.

Create a new user, for example linuxuser:

$ sudo useradd -m linuxuser

On Debian-based distributions:

Grant sudo privileges using the usermod command:

On Debian/Ubuntu:

$ sudo usermod -aG sudo linuxuser

On RHEL-based systems (CentOS, Rocky Linux, Alma Linux):

$ sudo usermod -aG wheel example-user

On OpenSuse:

$ sudo usermod -aG wheel example-user

In the above:

  • -aG: Adds user to a group without removing from others
  • sudo/wheel: Target group for administrative rights

Switch Users in Linux

You can switch accounts using the su (substitute user) command. Adding the - option loads the user’s environment and home directory. Non-sudo users require the password of the target account, while sudo su bypasses password prompts.

Switch to linuxuser with:

Enter password when asked:

Password:

Details of the command:

  • su: Switches user
  • : Loads profile and environment variables

Check sudo rights by listing /root directory:

Enter the sudo password:

[sudo] password for linuxuser:

If the account has sudo rights, the command executes successfully. Otherwise, the attempt is logged and reported in system logs.

View User Information in Linux

In Linux, you can check user details such as username, groups, and ID values to help with tasks like modifying file permissions. Follow these steps to display the currently active user information on your workstation.

Run the whoami command to display the name of the logged-in user:

Sample output:

linuxuser

Use the groups command to list all groups associated with a user:

Example output:

linuxuser : linuxuser sudo

This confirms that the user linuxuser belongs to both the linuxuser and sudo groups.

Run the id command to view the user’s default group and related IDs:

This command shows the UID, GID, and all groups associated with the user. The GID indicates the default group.

Example output:

uid=1001(linuxuser) gid=1001(linuxuser) groups=1001(linuxuser),27(sudo)

Conclusion

You have successfully created users in Linux, including both standard and sudo accounts, secured with password authentication. You also verified user details such as username, groups, and IDs. Managing users effectively is key to ensuring security and organization on a Linux system. Always set strong passwords and grant sudo rights only to trusted individuals responsible for performing administrative operations like software installation.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: