Skip to content

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

  1. Create two users

    • Create alice and bob, each with a home directory.
    • Set passwords for both.

    πŸ’‘ Commands: useradd, passwd


  1. Create a group

    • Create a group called projectteam.

    πŸ’‘Command: groupadd


  1. Assign users to groups

    • Add both alice and bob to projectteam as supplementary members.
    • Change alice’s primary group to projectteam.

    πŸ’‘ Command: usermod -aG, usermod -g


  1. Verify memberships

    • Show which groups alice and bob belong to.

    πŸ’‘ Commands: id, groups


Part 2: File and Directory Permissions

  1. Create a project directory

    • As root, create /home/projectX.
    • Change its group ownership to projectteam.

    πŸ’‘ Commands: mkdir, chgrp


  1. Set permissions on the project directory

    • Set permissions so that:
      • User (owner) has rwx
      • Group has rwx
      • Others have no access

    πŸ’‘ Command: chmod 770 or chmod u=rwx,g=rwx,o=---


  1. 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.

  1. Work with scripts
    • As alice, create a simple script hello.sh inside /home/projectX:
      #!/bin/bash
      echo "Hello from Alice's script!"
    • Check permissions with ls -l hello.sh.
    • Try to run it as alice without x 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.

  1. Experiment with directories
    • Inside /home/projectX, create a subdirectory reports/.
    • Set its permissions to 770.
    • As bob, try to:
      • Enter the directory (cd reports).
      • Create a new file inside.
      • Delete that file again.
    • Then remove write permissions for the group and try the same steps again -> note what changes.

  1. 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


Part 3: Cleanup

  1. Delete users and group
  • Delete alice and bob (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.sh
Output: (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.