Understanding Linux File Permissions (with example exercise)
Find out about Linux file permissions and how to modify files and folders using a simple exercise with explanations of what all those numbers and letters mean.
Understanding Linux Permissions
Permissions in Linux define who can read, write, or execute files and directories. These permissions are represented in two ways:
- Symbolic (letters):
rwx
format. - Numeric (numbers):
chmod
format like755
.
Symbolic Representation
Permissions are displayed as a string of 10 characters when you run ls -l
, e.g., -rw-r--r--
.
Character Position | Meaning | Example |
---|---|---|
1 | File type | - (file), d (directory) |
2-4 | Owner’s permissions | rwx (read, write, execute) |
5-7 | Group’s permissions | r-- (read only) |
8-10 | Others’ permissions | r-- (read only) |
For example, -rw-r--r--
means:
-
= regular file.rw-
= owner can read and write, but not execute.r--
= group can only read.r--
= others can only read.
Numeric Representation
Each permission is represented by a number:
4
= Read (r
)2
= Write (w
)1
= Execute (x
)
Add these values to set permissions:
7
= Read (4
) + Write (2
) + Execute (1
) =rwx
6
= Read (4
) + Write (2
) =rw-
5
= Read (4
) + Execute (1
) =r-x
4
= Read only =r--
Permission Structure:
When using numbers like 755
, each digit represents permissions for owner, group, and others in that order:
7
=rwx
(Owner has full permissions).5
=r-x
(Group can read and execute).5
=r-x
(Others can read and execute).
Examples
chmod 755 myfile.txt
- Owner:
rwx
(full access). - Group:
r-x
(read and execute only). - Others:
r-x
(read and execute only).
- Owner:
chmod 644 myfile.txt
- Owner:
rw-
(read and write). - Group:
r--
(read only). - Others:
r--
(read only).
- Owner:
File vs Folder Permissions
- Files:
r
= View the file contents.w
= Modify the file.x
= Execute the file (only for scripts or programs).
- Folders:
r
= List the contents of the folder.w
= Add or remove files in the folder.x
= Enter the folder (needed to access its contents).
Please consider supporting my content by subscribing to me here, allowing me to provide easy to follow, step by step tutorials and information for beginners and students of Programming, Networking and IT.
Exercise Steps
1. Preparation
Step: Log in to the Linux VM using a terminal.
- This exercise is best done without the root user. So you should be logged in as a regular user (not root) for realistic permission handling.
- If you need to create a non-root user then follow this quick steps:
sudo adduser newuser
- Replacenewuser
with the username you want to create.
- The system will prompt you to set a password and fill in optional details (like full name, phone number, etc.). You can pressEnter
to skip the optional details.cat /etc/passwd
- This will display a list of all system users. The new user should appear at the bottom.su - newuser
- Replacenewuser
with the username you created.
- You’ll be prompted to enter the new user's password.
That will give you a non-root user since it was created and was not added to the sudo group. You can do a lot with these commands and functions but for now this will be fine for this exercise.
2. Create Files and Folders
Steps:
- Create a folder:
mkdir permissions_practice
cd permissions_practice
Explanation:mkdir
creates a new directory. Here, it's namedpermissions_practice
. - Create a file inside the folder:
nano example.txt
Explanation:nano
creates an empty file. This will be used for practising file permissions.
3. Check Current Permissions
Steps:
- Use the
ls
command to view permissions:ls -l
Explanation:ls -l
lists files with detailed information, including permissions.
4. Modify File Permissions
Steps:
- Remove write permissions for the owner:
chmod u-w example.txt
Explanation:chmod u-w
removes the write permission for the owner (u
). The-w
specifies removal of the write capability. - Try to edit the file using
nano
:nano example.txt
Explanation: This will fail because the owner no longer has write permissions. - Restore write permissions:
chmod u+w example.txt
Explanation: Restores the write capability for the owner.
5. Practice with Folders
Steps:
- Create a subfolder and file:
mkdir test_folder
touch test_folder/sample.txt - Remove execute permissions from the folder:
chmod u-x test_folder
- Try to list the contents of the folder:
ls test_folder
Explanation: Without execute (x
) permission, the user cannot access the folder. - Restore execute permissions:
chmod u+x test_folder
Now you know how to do some basic editing of files and folders in Linux and applying some different permission structures. Hope you found it helpful and please do consider joining me on this website.