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.
Setting Up Our Practice Area
Let's create a safe space to practice permissions without affecting important system files:
Exercise 1: Creating and Examining Files
Let's create some files and examine their default permissions:
Understanding Exercise
Answer these questions about the permissions you see:
- What permissions does the owner have on hello.txt?
- What permissions do others have on script.sh?
- Why does testdir have execute permission for everyone?
Click for answers
- The owner has read and write permissions (rw-)
- Others have read-only permissions (r--)
- 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:
Try It Yourself
Practice these symbolic changes:
- Remove read permission from others on hello.txt
- Add write permission for group on script.sh
- Remove all permissions from others on testdir
Click for commands
Octal Method
Octal method uses numbers to represent permissions:
- 4 = read (r)
- 2 = write (w)
- 1 = execute (x)
Octal Practice
Convert these permission requirements to octal numbers and apply them:
- Make hello.txt readable by everyone, writable only by owner
- Make script.sh executable by everyone, writable only by owner
- Make testdir accessible only by owner
Click for answers
Exercise 3: Testing Permissions
Let's test what happens with different permission combinations:
Permission Testing
Test these scenarios:
- Set script.sh to read-only and try to execute it
- Set testdir to no permissions and try to enter it
- Set hello.txt to execute-only and try to read it
Click for expected results
- Script won't execute - needs read permission to be read by the system
- Can't enter directory - needs execute permission
- 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:
Directory Challenge
Set up these directory permission scenarios:
- Create a directory where you can enter but not list contents
- Create a directory where you can list but not enter
- Create a directory where you can enter and list but not create files
Click for solutions
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:
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 accessWeb Server Setup
Test your web directory permissions:
- Can you read the public index.html file?
- Can you access the private config.txt?
- What happens if you try to create a file in logs/?
Click for expected results
- Yes - public directory has read permissions for everyone
- No - private directory is restricted to owner only
- 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:
Common Permission Problems and Solutions
Problem: "Permission Denied" when running a script
Problem: Can't access files in a directory
Problem: Web server can't read files
Cleanup
When you're done practicing, clean up your test files:
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.
Key Takeaways
- Use
chmodto 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
Linux 101