Creating Users and Groups, Setting Access Rights, Managing Processes
1. Problem statement
Alright, we've got some intense hands-on work ahead, where you'll be able to apply all the knowledge from the previous lectures. We'll be creating users, grouping them, setting up unique access permissions, and managing processes in the system. And if you're wondering, "Why do I even need all this?", imagine you're a system administrator or a DevOps engineer, and you're tasked with configuring a server for team collaboration. Or maybe you're just trying to save your computer from chaos! In the real world, situations like this happen all the time.
Your goal is to set up the system for the following users and processes:
- Create two users:
user1
anduser2
. They'll be collaborating in thedevelopers
group. - Configure a file (for example,
project.txt
) so that only members of thedevelopers
group can edit it. - Create a few processes (for instance, using the
sleep
command) and learn how to terminate them if necessary. - View active processes to determine which ones are consuming the most resources.
Sounds like a plan? Let's make it happen step by step.
2. Step 1: Creating Users
1. Creating Users
First, let's create two users:
sudo adduser user1
sudo adduser user2
When you run these commands, the system will ask you to set a password and provide a description for each user. We won't fill in unnecessary details here, you can just press Enter.
Each user automatically gets a home directory in /home
. You can check it with:
ls /home
If you're working locally, don't forget to use sudo
to gain admin rights. And if you're on WSL, be prepared for some quirks with users. For example, in WSL, the default user is already a system administrator.
3. Step 2: Creating a Group and Adding Users
1. Creating a Group
To let users collaborate on projects, let's add them to a group. First, we'll create a group called developers
:
sudo groupadd developers
2. Adding Users to the Group
Now, let's add users user1
and user2
to the developers
group. We'll use the usermod
command for this:
sudo usermod -aG developers user1
sudo usermod -aG developers user2
3. Checking Group Membership
To make sure the users were added to the group correctly, run:
groups user1
groups user2
You should see that user1
and user2
are members of the developers
group.
4. Step 3: Working with Files and Permissions
1. Creating a File for the Project
Let's create a file project.txt
in the home directory of one of the users (e.g., user1
):
sudo -u user1 touch /home/user1/project.txt
The sudo -u
command lets us perform actions on behalf of another user.
2. Changing File Owner and Group
We'll assign user1
as the owner, and the group as developers
, so both users have the same access permissions:
sudo chown user1:developers /home/user1/project.txt
3. Setting File Permissions
Now, let's change the file permissions. We'll use the chmod
command:
sudo chmod 660 /home/user1/project.txt
What did we do? We gave the owner user1
and the group developers
read and write permissions rw-
, while everyone else has no access.
You can check the file permissions with the ls
command:
ls -l /home/user1/project.txt
Expected output:
-rw-rw---- 1 user1 developers 0 date time /home/user1/project.txt
5. Step 4: Running Processes
1. Running Processes
For practice, let's create a couple of "long-living" processes using the sleep
command:
sleep 300 &
sleep 400 &
sleep 500 &
Each sleep
command runs in the background thanks to the &
symbol.
To check the list of processes, use the command ps
:
ps
You can see more info using ps aux
:
ps aux | grep sleep
6. Step 5: Terminating Processes
1. Killing a process by PID
Identify the PID of one of the processes (for example, sleep
) and terminate it:
kill PID
Where PID
is the process identifier, which can be found using ps
.
2. Killing a process by name
If you want to kill all sleep
processes, use the killall
command:
killall sleep
3. Using pkill
If you don't remember the exact process name, pkill
can help terminate the process by partial name match:
pkill slee
7. Step 6: Monitoring Processes
To monitor system load, use:
top
— for real-time monitoring:top
Here you can check CPU load, memory usage, and active processes. Use the
k
key combination to terminate a process directly from the interface.htop
— more user-friendly interface (if you have it installed):
To exit, presshtop
F10
.
8. Checklist
After completing all the steps, you should make sure that:
- Two users
user1
anduser2
are created. - A group
developers
is created and configured, and both users are added to it. - A file
project.txt
is created and is accessible only to thedevelopers
group. - Several processes are created and terminated using commands like
kill
,killall
, andpkill
. - You successfully checked the list of processes using
ps
,top
, orhtop
.
If everything is done — congrats! You've gone through all the key stages of user, group, access rights, and process management in a Linux system. May the console power be with you!
GO TO FULL VERSION