Ubuntu

How To Add and Delete Users on Ubuntu 24.04

add or delete user in ubuntu 24.04

When a Linux operating system like Ubuntu 24.04 is to be accessed by multiple users who have to perform various tasks simultaneously, it urges the need to create multiple user accounts. Further, by creating individual accounts different sets of permissions can be assigned to each user which ensures system integrity and security. Furthermore, users can also be allocated to specific groups based on their role which helps in organizing and creating a proper hierarchical structure. Adding or removing a user in Ubuntu 24.04 can be done in multiple ways, which will be discussed briefly in this guide.

Outline:

How To Add a User in Ubuntu 24.04

On Ubuntu 24.04 and other Linux distributions, there are primarily two types of users one who has the administrator rights to perform different tasks while the other type doesn’t have such permissions. In other words, the second type of user is known as a non-sudo user which has limited access when it comes to the execution of different commands. There are two ways to add and delete users on Ubuntu, which are:

Through useradd Command

The useradd is a command line utility responsible for creating new system users on Ubuntu. It can be used with a number of different options that serve the purpose of assigning permissions to the user benign created. Further to create a non-sudo user execute:

useradd -m <user-name>

Here the m flag is used for creating the home directory for the new user, if no option is mentioned while creating a new user then the useradd command will create an account using the default settings. You can access these default settings file by navigating to /etc/defualt/useradd file:

After creating a user, you need to set its password for login and for that execute:

sudo passwd <User-name>

Here you will receive a warning either if you set a short password or any easy-to-guess password so create a strong password to keep the system secure:

Once you are done with setting the password verify the user created by using the awk utility which serves the purpose of searching the system.

awk -F':' '{ print $1}' /etc/passwd

Here, the above command searches passwd file and lists down all the system users:

Alternatively, you can create a user and set its password at the same time by executing the below command that uses the p flag for the password and m flag for a separate directory:

sudo useradd -m <user-name> -p <user-password>

Another way to verify the user creation is by logging in to the user account and executing the whoami command which will show the username of the user. Since we have created a user that does not have administrator privileges it will show a username not in the sudoers file:

sudo whoami

To add or create a user with administrator rights on Ubuntu 24.04 you need to add it to the sudoers file or you can add it to the administrator group names as sudo. So to add a user in Ubuntu 24.04 with admin rights by adding it to sudo group execute:

sudo useradd -m -G sudo <User-name>

Here, G flag is used for adding the user to a group:

Once the user is added to Ubuntu 24.04 set its password by executing:

sudo passwd Tim

Alternatively, you can manually add the user in the sudoers file and grant it access to execute all types of commands and tasks by adding the following code line:

%<user-name> ALL=(ALL:ALL) ALL

Once you have added the user in sudoers file now use the useradd command to add the user in Ubuntu 24.04:

sudo useradd -m Tim

Once the user is added to Ubuntu 24.04 set its password by executing:

sudo passwd <user-name>

After the user is added and its password is created login to it and execute the whoami command. You will root in the output which means that this user has admin rights:

sudo whoami

Another way to see the permissions assigned to the user by listing them and for that execute:

sudo -l

Through the System User Settings

The easiest way to add a user in Ubuntu, either with or without administrator rights, is by navigating to system settings. This method is easy to follow because one doesn’t need to remember any specific commands or need to set the permissions separately. First launch the settings app and from there click on the system option in the left pane, next navigate to user settings from there:

Now to add the user in Ubuntu 24.04 first unlock the settings by clicking on the Unlock button and authenticating it with the account password:

Now click on the Add user option at the bottom and enter the details for the new user which includes its name, username, administrator rights option, and password:

Here you need to set a strong password as Ubuntu won’t allow you to set a weak or short password. Moreover, you can leave it for the user to set it at the time of its login by selecting User sets password on first login. After the user is created it is displayed at the bottom in the other users tab:

Adding a User to a Group

Groups in Ubuntu play a significant role as they can be created based on the roles assigned to several users. This helps ease the way to assigning the same permissions to several users. To add a user to a specific group use the groupmod command along with a flag that adds the user to the specified group and U flag updates the group database:

sudo groupmod -a -U <user-name> <group-name>

How to Delete User in Ubuntu 24.04

Following the addition of user in Ubuntu 24.04 there are two ways to delete users from Ubuntu which are fairly simple: one is by using the userdel command and the other is by using the System settings:

Through deluser Command

To delete the user account along with its associated files through the command line of Ubuntu 24.04 the userdel utility can be used. So to delete a user on Ubuntu execute:

sudo userdel -r Tim

Here, the r flag removes the user directory along with its email if it was added after the user creation. The userdel command modifies the file like /etc/passwd, /etc/shadow, and /etc/group so that can verify the user deletion by navigating to these files:

 

If the user you want to delete is logged in then you will receive an error of user is currently used by the process. So in that case you can either log it out or kill its processes by using killall command:

sudo killall -u <user-name>

After that again use the userdel command to delete the use, also use the f flag to force the deletion as sometimes the user deletion is not successful:

sudo userdel -f <user-name>

Through the System User Settings

To remove or delete a user in Ubuntu via the settings app navigate to system settings, from there click on the Users tab, and unlock the settings. Next, click on the user you want to delete and then click on Remove User option at the bottom

If you don’t want to keep the data of the user, then simply turn on the option of Delete Files and Settings.

How To Modify the User Details in Ubuntu 24.04

Once the user is created you can still modify its information, permissions, and other options which include shell type and its ID. To change the shell type for the user use the usemod command, s flag for shell and the name of the shell you want to shift:

sudo usermod -s <shell-name> <user-name>

Remember that, the shell you are going to set for the user should be installed as I tried changing the shell from bash to zsh but it wasn’t installed:

If the desired shell type is missing then first install it and then change it:

The next thing you can modify is the userID whose primary purpose is to make it easy to identify the user. However, it can be changed sometimes if you want to reorganize the user accounts or manage the permissions and system resources allocation:

sudo usermod -u <new-UID> <user-name>

To segregate the users in Ubuntu or in any other Linux distribution based on their roles you can add information about it and for that use the following syntax:

sudo usermod -c "<info-to-be-added>" <user-name>

Here, c is for updating the GECOS field for the respective user account in the /etc/passwd file. You can also add further details about the user as well:

Conclusion

Creating user accounts in Ubuntu 24.04 holds significant importance when it is to be accessed by multiple users. To create or add a user in Ubuntu 24.04, either use the useradd command or the add user option in the system user settings. Furthermore, to remove or delete a user in Ubuntu you can either use the userdel command or the Remove User option in the system user settings.

Similar Posts