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 File Permissions (with example exercise)
Photo by Danielle Rice / Unsplash

Understanding Linux Permissions

Permissions in Linux define who can read, write, or execute files and directories. These permissions are represented in two ways:

  1. Symbolic (letters): rwx format.
  2. Numeric (numbers): chmod format like 755.

Symbolic Representation

Permissions are displayed as a string of 10 characters when you run ls -l, e.g., -rw-r--r--.

Character PositionMeaningExample
1File type- (file), d (directory)
2-4Owner’s permissionsrwx (read, write, execute)
5-7Group’s permissionsr-- (read only)
8-10Others’ permissionsr-- (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

  1. chmod 755 myfile.txt
    • Owner: rwx (full access).
    • Group: r-x (read and execute only).
    • Others: r-x (read and execute only).
  2. chmod 644 myfile.txt
    • Owner: rw- (read and write).
    • Group: r-- (read only).
    • Others: r-- (read only).

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:
  1. sudo adduser newuser
    - Replace newuser 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 press Enter to skip the optional details.
  2. cat /etc/passwd
    - This will display a list of all system users. The new user should appear at the bottom.
  3. su - newuser
    - Replace newuser 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:

  1. Create a folder:
    mkdir permissions_practice
    cd permissions_practice


    Explanation: mkdir creates a new directory. Here, it's named permissions_practice.
  2. 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:

  1. Use the ls command to view permissions:
    ls -l

    Explanation: ls -l lists files with detailed information, including permissions.

4. Modify File Permissions

Steps:

  1. 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.
  2. Try to edit the file using nano:
    nano example.txt

    Explanation: This will fail because the owner no longer has write permissions.
  3. Restore write permissions:
    chmod u+w example.txt

    Explanation: Restores the write capability for the owner.

5. Practice with Folders

Steps:

  1. Create a subfolder and file:
    mkdir test_folder
    touch test_folder/sample.txt
  2. Remove execute permissions from the folder:
    chmod u-x test_folder
  3. Try to list the contents of the folder:
    ls test_folder

    Explanation: Without execute (x) permission, the user cannot access the folder.
  4. 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.