Linux Level 1, Task 9: Understanding and Setting File Permissions¶
Today's task was a dive into a cornerstone of the Linux operating system: file permissions. The objective was to take a bash script that was not runnable and make it executable for all users on the system. This is a fundamental skill that every sysadmin and developer needs to know.
This exercise was a great opportunity to explore the chmod command and the two different ways of setting permissions: the intuitive "symbolic" method and the powerful "octal" method. I chose to use the octal method (755) for my solution, as it's a very common and efficient way to set standard permissions. This document is my detailed, first-person guide to that entire process.
Table of Contents¶
- The Task
- My Step-by-Step Solution
- Why Did I Do This? (The "What & Why")
- Deep Dive: Symbolic vs. Octal Permission Notation
- Common Pitfalls
- Exploring the Commands I Used
The Task¶
My objective was to modify a script's permissions on App Server 1. The specific requirements were:
1. Locate the script at /tmp/xfusioncorp.sh.
2. Grant it executable permissions.
3. Ensure that all users on the system could execute it.
My Step-by-Step Solution¶
The process involved inspecting the file, changing its permissions, and verifying the result.
Step 1: Connect and Investigate¶
First, I connected to App Server 1 (ssh tony@stapp01). I then used ls -l to inspect the initial permissions of the script.
ls -l /tmp/xfusioncorp.sh
# Example Output: -rw-r--r-- 1 root root 40 Oct 01 13:00 /tmp/xfusioncorp.sh
-rw-r--r-- confirmed that the file had read and write permissions for the owner, and read-only for everyone else. Crucially, there were no x (execute) permissions.
Step 2: Set the Executable Permissions (The Octal Method)¶
This was the core of the task. I used the chmod command with the numeric code 755 to set the standard permissions for a script that should be executable by everyone.
sudo chmod 755 /tmp/xfusioncorp.sh
Step 3: Verification¶
The final and most important step was to confirm that the permissions were set correctly.
ls -la /tmp/xfusioncorp.sh
# Output: -rwxr-xr-x 1 root root 40 Oct 01 13:01 /tmp/xfusioncorp.sh
-rwxr-xr-x. This was the definitive proof of success:
- rwx for the owner (root): Read, Write, Execute.
- r-x for the group (root): Read, Execute.
- r-x for others (all users): Read, Execute.
This confirmed that the script was now executable by everyone on the system.
Why Did I Do This? (The "What & Why")¶
- File Permissions: This is a fundamental security feature in Linux. Every file and directory has a set of permissions that determines who can read, write, or execute it.
- Permission Categories: These permissions are applied to three distinct categories of users:
1. Owner: A single user who owns the file.
2. Group: A user group that shares a set of permissions.
3. Others: All other users on the system.
- The x Bit: The "execute" bit is a special permission. For a regular file like a script, this bit must be "set" for the operating system to allow it to be run as a program. Without the x bit, it's just a text file that you can read, but not run. For a directory, the x bit means you are allowed to cd into it.
Deep Dive: Symbolic vs. Octal Permission Notation¶
The chmod command supports two ways of defining permissions. This task was a perfect illustration of both.
Symbolic Notation (The "Relative" Way)¶
This method is intuitive and uses letters to modify permissions. The structure is who operator permission.
- who: u (user/owner), g (group), o (others), a (all).
- operator: + (add), - (remove), = (set exactly).
- permission: r (read), w (write), x (execute).
The alternative solution to my task would have been:
sudo chmod a+x /tmp/xfusioncorp.sh
Octal (Numeric) Notation (The "Absolute" Way)¶
This method is powerful and uses a 3-digit number to set all permissions for the owner, group, and others at once. Each permission has a numeric value:
- read (r) = 4
- write (w) = 2
- execute (x) = 1
You add the numbers together for each category (owner, group, others) to get the final permission set.
My chosen solution was chmod 755:
- First digit (Owner): 7
- 7 = 4 + 2 + 1 -> r + w + x -> rwx
- Second digit (Group): 5
- 5 = 4 + 1 -> r + x -> r-x
- Third digit (Others): 5
- 5 = 4 + 1 -> r + x -> r-x
So, 755 is the standard, absolute way to say "the owner can do anything, but everyone else can only read and execute." This is the most common permission set for web files and executable scripts.
Common Pitfalls¶
- Forgetting sudo: Changing permissions on a file owned by another user (especially root) requires sudo. Forgetting it would result in a "Permission denied" error.
- Setting the Wrong Permissions: Using a number like 777 (rwxrwxrwx) is a common but dangerous mistake. It gives everyone write permission, which is a major security risk for most files.
- Misinterpreting ls -l: It's crucial to know how to read the -rwxr-xr-x output to correctly verify that the permissions have been set as intended.
Exploring the Commands I Used¶
- ls -l or ls -la: The primary command to list files in long format, which is how I view the permissions string. The -a flag also shows all hidden files.
- sudo chmod 755 /tmp/xfusioncorp.sh: The main command for my solution.
- chmod: The change mode command, used to set file permissions.
- 755: The octal code that sets rwxr-xr-x permissions.
- sudo chmod a+x /tmp/xfusioncorp.sh: The alternative solution using symbolic notation.