How to Create a New User on Ubuntu 22.04?


In Ubuntu (or any other Linux distribution), user management is one of the key tasks for an administrator. The users are created to carry out the specific task, i.e., network analyst analyzes the overall network performance. Based on their job descriptions, the administrator assigns dedicated permissions and roles to that specific user. Thus, it is the routine practice of the administrator to create new users whenever a new user/employee joins the system/organization.

Today’s guide will discuss the possible methods to create a user(s) on Ubuntu 22.04.

User Types in Ubuntu/Linux | Differences

The Ubuntu/Linux system has two major user categories, i.e., regular/normal and system. Let’s discuss both and the differences between them:

  • Regular Users: A regular user performs various primary/secondary tasks. Regular/Normal users are also referred to as “Human Users” as these are managed/created by the user itself. Regular users have UIDs, i.e., >=1000.
  • System Users: The system users have a specific system process/application to monitor. For instance, the “sshd” system user manages the SSH operations, and the “lightdm” system user is engaged with the light display manager. System users occupy the UIDs, i.e., 1-999. A system user cannot perform the regular user’s task and vice versa.

Root User:

While using the Linux system, you must be aware of the root user. Where does it fall? In the system or a regular user? Well, a root user is neither a system nor a regular user. The root user shares some operational similarities (but with super privileges) with the regular user. What a root user does, cannot be done by either the regular or the system user.

How to Create a New User on Ubuntu 22.04?

Ubuntu 22.04 is an enriched Linux distribution with a strong support of terminal as well as GUI. The useradd, adduser and the newusers commands are used to create a user using the terminal. Whereas the GNOME settings have the user management section to create a new user.

Let’s discuss all the possible methods:

useradd Command

The useradd command creates a new regular and system user. This command creates a user with some additional parameters (need to be used in the command). A simple useradd command adds a user with the following parameters:

  • Assign a default shell and a home directory.
  • Creates the user with the possible (next vacant) userid (UID) and the groupid (GID).

However, the useradd command does not generate the Password and the Full Name. Let’s first understand the syntax of the useradd command:

Syntax:

sudo useradd <options> <username>

The options supported by the useradd command can be retrieved as follows:

useradd --help

Let’s make use of this command to create a new user with various functionalities:

Example 1: With Default Parameters

Regular/Normal User

The command creates a normal/regular user with default parameters, i.e., does not add the Password, Full Name, and Home Directory.

sudo useradd genie

Let’s verify the creation:

Note: You can verify the user creation using the “id username” command as well. However, it just shows the basic details.

sudo cat /etc/passwd | grep genie

Here:

  • The extra details field is empty whereas the password field is encrypted.
  • The Home Directory is assigned only which does not exist physically on the system.
  • The encrypted password field does not tell us whether the user contains the password or not. However, we can verify whether the password is set or not using the command (switching to the newly created user):
su - genie

While switching, enter the password of the user (to whom you are switching). If you haven’t set the user, you won’t be able to switch to that user. In such a case, set the password of the user using the command:

sudo passwd genie

Now, you can switch to that user by providing the user password (set in the previous step).

System User

The useradd command provides the “–system” option to create a system user, as follows:

sudo useradd --system admgenie

Let’s verify:

Important: The creation of the system user is just an extension of the regular user. The system user created with the above method can be used as a regular user. Which violates the basic definition/purpose of the system user in Linux. Thus, it is not recommended to create a system user on your own.

Example 2: Create a User With a Home Directory

By default, the “useradd” command only assigns the directory and does not create it dedicatedly. The “-m” option of the useradd command creates a user alongside the home directory:

sudo useradd -m genie2

To verify, whether the directory is created or not:

  • Use the “sudo passwd” command to set the password.
  • Then, “su – genie2” to switch to that user.
  • Lastly, use “pwd” to check the current directory.

Note: If you use the “-M” flag, the “useradd” command will not create/assign a home directory.

Example 3: With Specific Home Directory

The “-d” option permits you to add the user with a specific home directory:

sudo useradd -d <path/to/home/directory> genie3

Note: You can choose any home directory by using its path.

The new home directory of the “genie3” is verified as follows:

sudo cat /etc/passwd | grep genie3

Example 4: Create a User With a Different Login Shell

By default, the useradd command assigns the user with the simple shell “sh”. We can change it using the “-s” option:

sudo useradd -s /bin/bash genie4

Now, the default shell of the “genie4” is “bash”. Here’s the verification:

Example 5: Create a User With a Different User ID

The “useradd” command acquires the lowest vacant UserID (after the last user). However, you can specify the UID while creating the user using the “-u” flag:

sudo useradd -u 1111 genie5

Verify it via the “id <username>” command as follows:

Example 6: Create Multiple Users | For Loop With useradd

By default, the useradd command adds only one user on one instance. However, the useradd when used with the For loop allows you to create multiple users. Let’s see how:

First, create a text file and add the usernames that you want to add:

Use the for loop and iterate over the text file with the loop variable. The loop variable fetches the users one by one and then adds them via the useradd command:

for u in `cat users.txt`; do sudo useradd $u; done

Important: You can use the useradd command to add the user with custom details in one command. To do so, use the relevant options alongside the recommended values.

Other Possible Use Cases of useradd Command:

Description Command / Syntax
Create and Add a user to the sudo group sudo useradd -G sudo <username>
Create a User With a Different Primary Group sudo useradd -g <group-id/group-name> <username>
Create a User Without any Group sudo useradd -N <username>
Add a User to Multiple (Secondary) Groups sudo useradd -G <group1>,<group2>,<group3> <username>
Add a User to Primary and Secondary Groups at once. $ sudo useradd -g <Primary> -G <Secondary1>,<Secondary2> <username>
Create a User With an Expiry Date sudo useradd -e YYYY-MM-DD <username>
Add Extra Details While Creating a User, i.e., Full Name, Any Comment, Ph No, Address. sudo useradd -c <“Details”> <username>

adduser Command

The adduser command works based on the useradd command. However, it offers extensive functionality (other than adding the user) and is easy to use as compared to the useradd command. Let’s understand its working/usage from its syntax:

For Normal User:

sudo adduser <Options> <username>

For System Users:

sudo adduser --system <Options> <username>

For more detailed usage, use the “–help” flag, below:

adduser --help

Let’s discuss its various use cases/examples:

Example 1: Create a User With Default Parameters

Regular User:

sudo adduser linuxgenie

The adduser asks to set the password for the new user, home directory, Full Name, Ph. No, and other information with a one-liner command.

System User:

sudo adduser --system sysgenie

To verify the above creations/additions:

sudo tail /etc/passwd

Note: The system user created by the “adduser” command has no login (like a normal user cannot log into it). Whereas the system user created by the “useradd” command does have the login shell (and regular users can log into it).

Example 2: Add a User With Custom Parameters

The command here adds a user with the custom details/parameters, such as:

  • “–home”: To add a different home directory.
  • “–shell”: To assign a custom shell.
  • “–uid”: To allocate a user-defined UID.
sudo adduser --home /home/genie2/ --shell /bin/zsh --uid 1020 linuxuser

Example 3: Create a User and Add it to the Sudo Group

Like the “useradd” command, the “adduser” also creates the user and adds it to the “sudo” group. For that, you need to use the “–ingroup” flag:

sudo adduser --ingroup sudo geniefox

Note that the user is only added to the “sudo” group.

Important: You can also use the for loop with the adduser command to create multiple users. The syntax is provided below:

for u in `cat <path/to/the/file>`; do sudo adduser $u; done

 

Where:

  • The “u” to read the users from the file. (You can use any other alphabet too).
  • <path/to/the/file> contains the path of the file where the usernames are stored.

newusers Command | Create Multiple Users

The newusers command reads the data from the file and appends it to the “/etc/passwd”. The syntax of the newusers command is as:

Syntax of the newusers command:

sudo newusers <file>

To make the operation successful, the file must contain the user details in the format of the “/etc/passwd” file, which is:

Syntax of the user details to be put in the file:

<username>:<passwd>:<UID>:<GID>:<GECOS>:<home-dir>:<default-shell>

GECOS refers to the additional information a user may have: Full Name, Ph. No, or any comment.

Example:

First, add the users’ details in a “.txt” file (by following the above syntax). Here, the file contains the details of a total of 4 users:

Now, use the newusers command on the file:

sudo newusers bulk.txt

Let’s verify the addition via the command:

sudo tail /etc/passwd

Bash Scripting | adduser, useradd, and newusers

Bash Scripting is an extension of the methods described above. With scripts, you can execute multiple commands at once. Let’s understand how to execute the adduser, useradd, and newusers commands in a single script:

A file is created for the “newusers” command where the user’s details are placed in the “/etc/passwd” file format:

Here’s the script code:

Make the script executable using “sudo chmod +x <path of the script>”. Then, run the script to add the users:

./script.sh

Let’s verify:

sudo tail /etc/passwd

Manually Creating a User in /etc/passwd File | Not Recommended

The “/etc/passwd” file contains the user-related information. One can follow the syntax of the entries in the “/etc/passwd” to create a new user. For instance, the following line follows the “/etc/passwd/” for a user “lgenie”:

lgenie:x:1003:1002:Linux Genie:/home/lgenie:/bin/bash

Open the “/etc/passwd” file in any editor and add the line:

Save the file and exit out from the editor. To verify the user creation, use the “id <username>” command:

id lgenie

Here you go! The user has been added instantly.

GNOME Settings | GUI Method

GNOME settings offer an interactive way to add a user (with, without a password, normal or administrator). To add a user via the GNOME settings, open it and navigate to the “users” tab:

Unlock the user management section:

Click on the “Add User” button/option:

Here, you can set the Full Name, username, administrator/standard account type, and password (or leave it and the user will be asked to set the password on the first login).

User authentication is required to complete the process. When all is done, you can view the user in the current user’s list:

Bonus Tip: How to Delete a User From Ubuntu 22.04?

The “deluser” command is used to delete a user from Ubuntu 22.04 or you can do it from the GUI as well. Let’s delete a user named “lgenie”:

sudo deluser lgenie

The command does not delete the home directory. To delete the user and its home directory as well, use the “–remove-home”:

sudo deluser --remove-home <username>

To delete a user via GUI, go to the user management section in the GNOME settings and unlock it:

Then, navigate to the user that you want to remove, click on it, and then on the Remove button:

Bottom Line

Ubuntu supports the useradd, adduser, and newusers commands to add a new user. GUI lovers can go to the GNOME settings to add a new user. When it comes to creating multiple users at a time, the newusers command is time/cost-effective. However, the adduser and the useradd command provide more control as they integrate the new user with all the system processes.

All in all, this post has listed the possible methods to create a new user on Ubuntu 22.04.

 

Print Friendly, PDF & Email
Categories