Sitemap

Linux — users and groups

4 min readSep 25, 2022

--

Press enter or click to view image in full size
Photo by Gabriel Heinzer on Unsplash

The Unix operating system like Ubuntu supports multiuser, i.e we can have several users use the same machine. This however creates a challenge whereby any user can modify the contents on the machine e.g delete a document or file needed by another user. A practical way of separating different users was devised to solve this problem. To understand the solution to this problem we need to understand:

  • how users and groups work (covered in this article)
  • how file and directory permissions work (covered in the next article)

In this guide we’ll look at:

  1. How to create create, modify and delete a user
  2. How to create, modify and delete a group

How to create, modify and delete a user

Before we create a user, let's find out the name of the user on our local machine. Type whoami on your terminal to check the name of the user you are logged in as. e.g in my case I am logged in as user -> jamal

jamal@jamal-hp:/$ whoami
jamal

The list of all users on your Linux system can be found in /etc/passwd.You can use a text editor to view the contents of the file as shown below.

jamal@jamal-hp:/$ vim /etc/passwd

The output of the /etc/passwd will be of this format

Get Jamal Guyo’s stories in your inbox

Join Medium for free to get updates from this writer.

jamal:x:1000:1000:Jamal,,,:/home/jamal:/bin/bash

  • jamal — is the username
  • x — means your password is stored in a hidden file called shadow. The path to the file is /etc/shadow
  • 1000 — is the user id.
  • 1000 — is the user group id. By default, you are added to a new user group with the same name as your username.
  • jamal/,,, — is the information on the user.
  • /home/jamal — is the home directory of the user.
  • /bin/bash — is the default shell you are given to interact with the O.S.
  1. To modify the user information — we use the usermod command. e.g
    - sudo usermod jamal --shell /bin/sh — to change the shell to /bin/sh
    - sudo usermod -l jayjay jamal — to change the username to jayjay
  2. To check the id of the user — type id -u <username> .
  3. To view a list of all users — just type:
    - getent passwd or
    - access the /etc/passwd file.
  4. To create a new user — To create a new user simply type the command:
    - sudo adduser <username> : The command will prompt you to create the new user's password, and additional information such as the full name, phone number …
    - sudo useradd <username> : This is a lazy version of the previous command. It’ll create a new user without a password and additional information. It won’t create a home directory as well. add the flag -m to create a home directory for the user. You can also create a password for the user using the next step below.
  5. To change the password of the user — to change the password of the user you created simply type sudo passwd <username>
  6. To switch to the new user — to switch to the user we just created you can use:
    - su <username> — this will require you to input the password of the user you want to switch to.
    - sudo su <username> — this will require the root user's password.
    NOTE: not all users can have the sudo privilege. To access the file that manages the users who can do the sudo command typesudo visudo .
  7. delete a user — to delete a user simply type sudo userdel <username>

How to create, modify and delete a group

To understand the need for groups let’s take a scenario where a system admin is changing file or directory permissions for students and teachers. Without grouping, he/she will have to change the permissions of each and every user manually. Groups make it easier to change the permissions of a number of users very quickly.

By default, when we create a new user, a new group is automatically created with the same name and the user is added to that group. e.g user jamal will be added to a group called jamal

The information of all the groups on your machine will be found in the path /etc/group . To access it simply open it using a text editor of your choice for example vim /etc/group

  1. To view the list of groups — just type:
    - groups on your terminal. or
    - getent group or
    - access /etc/group file
  2. to create a new group— type sudo groupadd <groupname> e.g sudo groupadd marketing.
  3. To add a user to a group — type:
    - sudo adduser <username> <groupname> e.g sudo adduser jamal marketing or
    - sudo usermod -aG <groupname> <username> e.g sudo usermod -aG marketing jamal
  4. To delete a user from a group — type sudo deluser <username> <groupname> e.g sudo deluser jamal marketing
  5. To delete a group — type sudo groupdel <groupname> e.g sudo groupdel marketing

--

--