Linux Exercise 2 - File and Directory Permissions
π Linux Exercise: File and Directory Permissions
π― Objective
Students will practice creating users and groups, assigning memberships, and setting file and directory permissions using both symbolic and octal syntax.
They will also experiment with creating, deleting, modifying, and executing files as well as creating, deleting, and entering directories under different permission setups.
They will document (protocol) every step they executed, including commands, outputs, and explanations.
π¨Task Instructions
Part 1: User and Group Management
-
Create two users
- Create
alice
andbob
, each with a home directory. - Set passwords for both.
π‘ Commands:
useradd
,passwd
- Create
-
Create a group
- Create a group called
projectteam
.
π‘Command:
groupadd
- Create a group called
-
Assign users to groups
- Add both
alice
andbob
toprojectteam
as supplementary members. - Change
alice
βs primary group toprojectteam
.
π‘ Command:
usermod -aG
,usermod -g
- Add both
-
Verify memberships
- Show which groups
alice
andbob
belong to.
π‘ Commands:
id
,groups
- Show which groups
Part 2: File and Directory Permissions
-
Create a project directory
- As root, create
/home/projectX
. - Change its group ownership to
projectteam
.
π‘ Commands:
mkdir
,chgrp
- As root, create
-
Set permissions on the project directory
- Set permissions so that:
- User (owner) has
rwx
- Group has
rwx
- Others have no access
- User (owner) has
π‘ Command:
chmod 770
orchmod u=rwx,g=rwx,o=---
- Set permissions so that:
- Create and test files
- As
alice
, create a file/home/projectX/plan.txt
with some text. - Check default permissions using
ls -l
. - Try to open and modify the file as
bob
. - Change permissions to allow
bob
to write as well (chmod 664 plan.txt
). - Try again as
bob
and record what happens.
- As
- Work with scripts
- As
alice
, create a simple scripthello.sh
inside/home/projectX
:#!/bin/bashecho "Hello from Alice's script!" - Check permissions with
ls -l hello.sh
. - Try to run it as
alice
withoutx
permission (expect error). - Add execute permission for the user (
chmod u+x hello.sh
). - Run it again successfully.
- Try running it as
bob
before and after adding group execute rights.
- As
- Experiment with directories
- Inside
/home/projectX
, create a subdirectoryreports/
. - Set its permissions to
770
. - As
bob
, try to:- Enter the directory (
cd reports
). - Create a new file inside.
- Delete that file again.
- Enter the directory (
- Then remove write permissions for the group and try the same steps again -> note what changes.
- Inside
-
Recursive permissions
- Apply permissions so the entire
/home/projectX
tree is accessible by owner and group, hidden from others.
π‘ Command:
chmod -R 770 /home/projectX
- Apply permissions so the entire
Part 3: Cleanup
- Delete users and group
- Delete
alice
andbob
(including home directories). - Delete the group
projectteam
.
π‘ Commands: userdel -r
, groupdel
π Protocol (What to Hand In)
Each student must create a protocol (log) that contains:
- Command executed
- Output received
- Explanation (1-2 sentences) of what the command did
- Observations when testing file/directory access with different users
Example:
Command: sudo chmod u+x hello.shOutput: (no output)Explanation: Gave user execute rights to the script hello.sh.Observation: Alice could run the script after adding +x, before that she got a "Permission denied" error.
β Completion Criteria
- Users and group created, assigned, and deleted correctly.
- File and directory permissions set with both symbolic and octal syntax.
- Students tested file creation, deletion, modification, and execution under different permissions.
- Students tested directory creation, deletion, and entering (cd) under different permissions.
- Recursive permissions tested.
- Protocol includes commands, outputs, explanations, and observations.
- Student can explain differences in
r
,w
,x
for files vs directories.