Linux is a multi-user operating system, which means several people can log in and work on the same machine at the same time. Therefore for security purposes, it is wise to give users their own accounts with their respective privileges. Where in case users need to share resources, it is advisable to put them in a group. Groups also make it easy to assign certain permission or privileges all at once on a set of user accounts other than doing it individually.

For one to add or create user accounts, they should have root permissions making them a superuser. Each user is assigned a unique ID that is different from other users in the operation system. You can add users via the use of graphical tools or by commands on the terminal.

/etc/passwd file is the user database file in Linux. It has records of users on the system with information such as the username, password, home directory, user ID, and group ID.

/etc/group file contains records of groups on the system and details like the Group ID, Group password, and the Group Members.

In this guide, I will take you through the basic commands of creating a user with sudo and Group on Ubuntu|KDE Neon|Kubuntu systems.

1. Adding User Account on Ubuntu / KDE Neon / Kubuntu

Let’s add a new user named ‘technixleo‘. In all the distributions, use either of the commands useradd or adduser

sudo adduser technixleo

You might be prompted to input a password

Adding user `technixleo' ...
Adding new group `technixleo' (1002) ...
Adding new user `technixleo' (1001) with group `technixleo' ...
Creating home directory `/home/technixleo' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for technixleo
Enter the new value, or press ENTER for the default
        Full Name []: 
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n] y

When the user is created, a group is created and given the same name as the new user account.

2. Adding users to an existing group

We can use the options -aG and -G as shown below.

sudo usermod -aG sudo technixleo
sudo usermod -G  sudo technixleo

3. Changing User’s default shell

Use -shell followed by the path to the new shell.

You can list to see the shell the user is logged in to using either of the commands. The second command will show the shell of the current user.

grep "^$USER" /etc/passwd
echo "My Shell name is: $SHELL"

Sample output.

$ grep "^technixleo" /etc/passwd
technixleo:x:1001:1002:,,,:/home/technixleo:/bin/bash

Then change the shell using either of the commands below.

sudo usermod --shell /bin/ksh technixleo
sudo usermod -s /bin/ksh technixleo

The sample output shows the shell changed.

$ grep "^technixleo" /etc/passwd
technixleo:x:1001:1002:,,,:/home/technixleo:/bin/ksh

4. Lock or Unlock a user account

To lock a user, use the -L (uppercase) or -lock option

sudo usermod -L technixleo
sudo usermod --lock technixleo

To Unlock a user, use the -U (uppercase) or -unlock option.

sudo usermod -U technixleo
sudo usermod --unlock technixleo

5. Restrict service user accounts

We can restrict a user by changing the login shell.

sudo usermod -s /sbin/nologin technixleo

6. Working with sudoers file

The /etc/sudoers file instructs the system on how to handle the sudo command. You can also configure other users to be able to run the sudo commands by editing the sudoers file.

To open the file, use the text editor vi

$ sudo vim /etc/sudoers

###Output

# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
~                                                                                         

To edit the sudoers file, you are required to use visudo which makes sure the editing is done by one person at a time and it does syntax checks.

sudo visudo -f /etc/sudoers

7. Deleting a user account

To delete a user together with the home directory, use the -r or –remove flag as shown below

sudo userdel -r technixleo
sudo userdel --remove technixleo

8. Creating a Group on Ubuntu / KDE Neon / Kubuntu

To create a group, use

sudo groupadd tech_group

9. Add user to a group

Let us add our user ‘technixleo’ to the newly created group ‘tech_group’.

sudo usermod -aG tech_group technixleo

10. Using Groupmod to modify groups in Linux

groupmod command allows you to modify the contents of a group like changing the name or group ID.

To change the name of the group use the following command.

sudo groupmod -n geek_group tech_group

From the above command, the name of ‘tech_group’ will change to ‘geek_group’.

To check, use

$ grep geek_group /etc/group
#Output
geek_group:x:1004:technixleo

To change the group ID

sudo groupmod -g 1010 geek_group

You can view it using

$ grep geek_group /etc/group
#Output
geek_group:x:1010:technixleo

The ID has changed successfully.

11. Deleting a user group

To delete a group, use

sudo groupdel geek_group

Conclusion

We have gone through the basic commands of creating a sudo user and a group on Ubuntu|KDE Neon|Kubuntu systems. Managing users in Linux is important as it assures that files use and permissions are set according to the users’ privileges. It requires a root user or a superuser with root privileges.

More guides to check out in our website:

LEAVE A REPLY

Please enter your comment!
Please enter your name here