Lesson 3.2: Permission Exercises

Time to get hands-on with Linux permissions! In this lesson, we'll practice reading, changing, and managing file and directory permissions using real commands.

Practice Environment: Open a terminal and follow along with these exercises. Don't worry about making mistakes - that's how we learn!

Setting Up Our Practice Area

Let's create a safe space to practice permissions without affecting important system files:

$ cd ~ # Go to your home directory $ mkdir permission-practice $ cd permission-practice $ pwd /home/valente/permission-practice

Exercise 1: Creating and Examining Files

Let's create some files and examine their default permissions:

$ echo "Hello World" > hello.txt $ echo "#!/bin/bash" > script.sh $ mkdir testdir $ ls -l -rw-r--r-- 1 valente valente 12 Dec 10 15:45 hello.txt -rw-r--r-- 1 valente valente 13 Dec 10 15:45 script.sh drwxr-xr-x 2 valente valente 4096 Dec 10 15:45 testdir

Understanding Exercise

Answer these questions about the permissions you see:

  1. What permissions does the owner have on hello.txt?
  2. What permissions do others have on script.sh?
  3. Why does testdir have execute permission for everyone?
Click for answers
  1. The owner has read and write permissions (rw-)
  2. Others have read-only permissions (r--)
  3. Directories need execute permission for anyone to enter them (cd into them)

Exercise 2: Changing Permissions with chmod

The chmod command changes file permissions. There are two ways to use it: symbolic and octal.

Symbolic Method

Symbolic method uses letters to represent who gets what permissions:

$ chmod u+x script.sh # Add execute for owner $ chmod g-w hello.txt # Remove write from group $ chmod o+r script.sh # Add read for others $ chmod a+x script.sh # Add execute for all (a = u+g+o)

Try It Yourself

Practice these symbolic changes:

  1. Remove read permission from others on hello.txt
  2. Add write permission for group on script.sh
  3. Remove all permissions from others on testdir
Click for commands
$ chmod o-r hello.txt $ chmod g+w script.sh $ chmod o-rwx testdir

Octal Method

Octal method uses numbers to represent permissions:

$ chmod 755 script.sh # rwxr-xr-x $ chmod 644 hello.txt # rw-r--r-- $ chmod 700 testdir # rwx------
Memory Tip: Add the numbers: 7 = 4+2+1 (rwx), 6 = 4+2 (rw-), 5 = 4+1 (r-x), 4 = 4 (r--)

Octal Practice

Convert these permission requirements to octal numbers and apply them:

  1. Make hello.txt readable by everyone, writable only by owner
  2. Make script.sh executable by everyone, writable only by owner
  3. Make testdir accessible only by owner
Click for answers
$ chmod 644 hello.txt # rw-r--r-- $ chmod 755 script.sh # rwxr-xr-x $ chmod 700 testdir # rwx------

Exercise 3: Testing Permissions

Let's test what happens with different permission combinations:

$ chmod 000 hello.txt # Remove all permissions $ cat hello.txt # Try to read cat: hello.txt: Permission denied $ chmod 400 hello.txt # Read only for owner $ cat hello.txt # Now it works Hello World $ echo "New line" >> hello.txt # Try to write bash: hello.txt: Permission denied

Permission Testing

Test these scenarios:

  1. Set script.sh to read-only and try to execute it
  2. Set testdir to no permissions and try to enter it
  3. Set hello.txt to execute-only and try to read it
Click for expected results
  1. Script won't execute - needs read permission to be read by the system
  2. Can't enter directory - needs execute permission
  3. Can't read file - execute permission doesn't allow reading content

Exercise 4: Directory Permission Challenges

Directory permissions can be tricky. Let's explore some common scenarios:

$ mkdir -p project/{src,docs,tests} $ echo "source code" > project/src/main.py $ echo "documentation" > project/docs/readme.md $ chmod 700 project # Only owner can access $ ls project/ # This works src docs tests $ chmod 100 project # Only execute for owner $ ls project/ # This fails ls: cannot open directory 'project/': Permission denied $ cd project # This works! $ ls # This fails .
Important: Execute permission on a directory lets you enter it, but you need read permission to list its contents!

Directory Challenge

Set up these directory permission scenarios:

  1. Create a directory where you can enter but not list contents
  2. Create a directory where you can list but not enter
  3. Create a directory where you can enter and list but not create files
Click for solutions
$ mkdir test1 test2 test3 $ chmod 100 test1 # Enter only (execute) $ chmod 400 test2 # List only (read) - actually won't work as expected $ chmod 500 test3 # Enter and list, no write

Note: You can't list a directory without execute permission, so scenario 2 doesn't work as expected!

Exercise 5: Practical Web Server Scenario

Let's simulate setting up a basic web directory structure like you might use for the nginx project:

$ mkdir -p website/{public,private,logs} $ echo "

Welcome

" > website/public/index.html $ echo "secret data" > website/private/config.txt $ chmod 755 website # Everyone can enter $ chmod 755 website/public # Everyone can read web files $ chmod 700 website/private # Only owner can access $ chmod 750 website/logs # Owner and group can access

Web Server Setup

Test your web directory permissions:

  1. Can you read the public index.html file?
  2. Can you access the private config.txt?
  3. What happens if you try to create a file in logs/?
Click for expected results
  1. Yes - public directory has read permissions for everyone
  2. No - private directory is restricted to owner only
  3. Depends on your group membership - logs directory allows owner and group

Exercise 6: Changing Ownership

The chown command changes file ownership. You'll need sudo for this:

$ sudo useradd -m testuser # Create a test user (if needed) $ sudo chown testuser hello.txt $ ls -l hello.txt -rw-r--r-- 1 testuser valente 12 Dec 10 15:45 hello.txt
Note: You need sudo privileges to change ownership to another user. In a real web server scenario, you might change ownership to the web server user (like www-data).

Common Permission Problems and Solutions

Problem: "Permission Denied" when running a script

$ ./myscript.sh bash: ./myscript.sh: Permission denied # Solution: Add execute permission $ chmod +x myscript.sh $ ./myscript.sh Hello from script!

Problem: Can't access files in a directory

$ ls mydir/ ls: cannot open directory 'mydir/': Permission denied # Solution: Add execute permission to directory $ chmod u+x mydir $ ls mydir/ file1.txt file2.txt

Problem: Web server can't read files

# Solution: Make files readable by others $ chmod 644 *.html *.css *.js # And make directories accessible $ chmod 755 public_html/

Cleanup

When you're done practicing, clean up your test files:

$ cd ~ $ rm -rf permission-practice

What's Next?

Congratulations! You've mastered the basics of Linux permissions. These skills will be essential when we set up our nginx web server in Module 7.

Continue to Module 4: Shell & Terminal

Key Takeaways

  • Use chmod to change permissions (symbolic or octal)
  • Directories need execute permission to be entered
  • Files need read permission to be viewed and execute to be run
  • Web files typically use 644, directories use 755
  • Always test permission changes before applying to important files