Answered by the Webhosting Experts

Ownership and Permissions in Linux

Ownership and Permissions in Linux Overview

Ownership and permissions in Linux are a concept that determines who can access and modify files and directories. Every file and directory have an owner and a group, which are usually the user who created them and the user’s current group. The owner and group of a file or directory can be changed using the chown command. Permissions can be read, write, or execute, and can be changed using the chmod command. Ownership and permissions are important for security and multi-user environments in Linux as Linux also uses permissions to control the access rights of the owner, group, and others. 

File and Ownership Permissions Structure

In Linux, understanding file ownership and permissions is crucial for effective system management and security.

Every file and directory in Linux have three kinds of owners:

  • User: The creator of the file becomes its owner. You can change the ownership later.
  • Group: Users are part of specific groups. Managing users in a multi-user environment involves creating separate groups (e.g., dev team, QA team, sysadmin team). Group membership simplifies permission management.
  • Other: This group includes all users on the system, even if you’re the sole user. Everyone with access to the system belongs to this group.

Each file and directory have three permissions for all three owner types:

  • Files
    • Read (r): Allows viewing or copying file contents.
    • Write (w): Permits modifying file content.
    • Execute (x): Enables running executable files (scripts or programs).
  • Directories
    • Read: Lists files and copies them from the directory.
    • Write: Adds or deletes files (requires execute permission).
    • Execute: Allows entering the directory.

File Permissions have two modes. An absolute mode and a symbolic mode. The modes detail the type of entries required for permissions to take effect.

  • Symbolic Mode 
    • Symbolic mode allows you to modify permissions based on their current state. You use symbols like +, -, and = to add, subtract, or set permissions.
      • + Adds permissions.
      • Removes permissions.
      • = Sets permissions explicitly
  • Absolute Mode
    • In absolute mode, you explicitly specify the permissions using numeric values (octal notation). These values represent the combination of read, write, and execute permissions for the owner, group, and others.
      • The numeric values are as follows:
        • 0: No permissions (—)
        • 1: Execute only (–x)
        • 2: Write only (-w-)
        • 3: Write and execute (-wx)
        • 4: Read only (r–)
        • 5: Read and execute (r-x)
        • 6: Read and write (rw-)
        • 7: Read, write, and execute (rwx)

Viewing File and Directory Permissions

Viewing file and directory ownership and permissions in Linux can be done with each via the command line interface of your Linux server.

The first one is using the ls command with the -l flag, as such ls -l. This command would allow a user to view a list of files within the directory they are in or in another (Using ls -l /path/to/directory) to view the permissions for each file. 

ls -l Command Displaying Files and Directories Within a Directory Along with Their Permissions
ls -l Command Displaying Files and Directories Within a Directory Along with Their Permissions

The second method is using the stat command to view a more in-depth scope of the file or directory involved.

stat Command Displaying an In-Depth View of File or Directory Permissions
stat Command Displaying an In-Depth View of File or Directory Permissions

Modifying File and Directory Permissions

Follow the steps below to proceed with file and directory permissions modification. For this example, we will use the absolute and symbolic modes with the file test_file.txt and the command chmod.

In this example we are modifying a file using the symbolic method by first viewing the file permissions using stat test_file.txt and modifying the permissions using chmod +x test_file.txt. Note that without specifying the user, group, other assignment, then the +x would apply all of them as seen below. 

To add permissions specifically to one of the owners, you can use chmod u+x test_file.txt which will add the +x to the user owner.

Using Symbolic Mode to Modify File Permissions
Using Symbolic Mode to Modify File Permissions

In this example we are modifying a file using the absolute method by first viewing the file permissions using stat test_file.txt and modifying the permissions using chmod 744 test_file.txt. Note that after the change has been done, the permissions changed from 644 to 744 or also known as rw-r–r– to rwxr–r–.

Using Absolute Mode to Modify File Permissions
Using Absolute Mode to Modify File Permissions

Thank you for reading and I hope that you enjoyed this Ownership and Permissions in Linux guide. For more guides, please check out our Knowledge Base which is updated weekly!

 

-Written by Pascal Suissa

Need More Personalized Help?

If you have any further issues, questions, or would like some assistance checking on this or anything else, please reach out to us from your my.hivelocity.net account and provide your server credentials within the encrypted field for the best possible security and support.

If you are unable to reach your my.hivelocity.net account or if you are on the go, please reach out from your valid my.hivelocity.net account email to us here at: support@hivelocity.net. We are also available to you through our phone and live chat system 24/7/365.