Sitemap

USERS, GROUPS AND DIRECTORIES ON LINUX

4 min readJun 3, 2024

--

Step-by-step guide to create users and groups, and then add those users to their respective groups and create a directory for each group in Linux:

Creating Groups

You need to be a privelege user mode to execute these commande

To create a new group, use the `groupadd` command. In this example, we will create 3 groups( Developpers, Engineers and Administrators)

sudo groupadd Developers

Press enter or click to view image in full size

Type the same command with the appropriate group’s name and then hit enter for the rest of the groups.

sudo groupadd Engineers

sudo groupadd Administrators

2. Verify the groups were created by checking the ‘etc/group’ file:
grep ‘Developers\|Engineers\|Administrators’ /etc/group

Press enter or click to view image in full size

Creating Users in a spefic group

  1. To create a new user and add them to a specific group, use the `useradd` command with the `-g` option for the primary group ( e.g, Jerry, Tom and Bob).

sudo useradd -g Developers Jerry

sudo useradd -g Engineers Tom
sudo useradd -g Administrators Bob

Press enter or click to view image in full size

2. Set passwords for the new users:

sudo passwd Jerry
sudo passwd Tom
sudo passwd Bob

Press enter or click to view image in full size

3. Verify the group memberships with the `id` command:
id Jerry

id Tom

id Bob

Press enter or click to view image in full size

Creating Group Directories

  1. Create a directory for each group and set the appropriate group ownership:

Directories ( make sure you’re in the home directory)

Get Azizou GNANKPE’s stories in your inbox

Join Medium for free to get updates from this writer.

sudo mkdir {Developers,Engineers,Administrators}

Press enter or click to view image in full size

Ownership ( make sure you’re in the home directory)

sudo chgrp Developers Developers

sudo chgrp Engineers Engineers

sudo chgrp Administrators Administrators

Press enter or click to view image in full size

Type ls -ls command to see each directory’s group belonging

Press enter or click to view image in full size

The `chmod g+s` command sets the “setgid” bit on a directory in Unix-like systems, including Linux. When the setgid bit is enabled on a directory

New subdirectories created within that directory inherit the group ownership of the parent directory instead of the user’s primary group.

New files and directories created within that directory inherit the group permissions of the parent directory, ensuring consistent group access.

sudo chmod g+s Developers

sudo chmod g+s Engineers

sudo chmod g+s Administrators

Press enter or click to view image in full size

Set the sticky bit on each directory, with the ‘chmod o+t path/to/directory’ command. The sticky bit is commonly used on world-writable directories like /tmp to allow anyone to create files there, but prevent deletion of files by anyone other than the file owner or root.

sudo chmod o+t /home/Developers

sudo chmod o+t /home/Engineers

sudo chmod o+t /home/Administrators

Press enter or click to view image in full size

Type the ‘ls -l’ command to see how all your directories will change in color. Protection and inheritance have been enabled on them all.

Press enter or click to view image in full size

2. Set the permissions for the group directories to allow read/write access for group members:
sudo chmod 770 /home/Developers
sudo chmod 770 /home/Engineers
sudo chmod 770 /home/Administrators

Press enter or click to view image in full size

Now you have created the groups (Developesr, Engineers, Administrators), users (Jerry, Tom, Bob) with their respective primary groups and created a directory for each group with the proper permissions for group members to read and write files.

Congrats!!

--

--

Azizou GNANKPE
Azizou GNANKPE

Written by Azizou GNANKPE

Microsoft MVP & MIE Expert | Cloud & Systems Engineer | Azure Security & Solutions