DEV Community

Cover image for File and Directory Permissions in Linux: A Beginner-Friendly Guide🛡️
SAHIL
SAHIL

Posted on

File and Directory Permissions in Linux: A Beginner-Friendly Guide🛡️

Ever wondered why sometimes you can open a file and other times you get a "Permission denied" error in Linux? 🤔 The answer lies in file and directory permissions! Think of them as the gatekeepers of your system, controlling who can do what with your files and folders. Let's dive into the basics!

What are File Permissions? 🚦
File permissions are a set of rules that determine who can read, write, or execute a specific file or directory. They are a fundamental security feature in Linux and other Unix-like operating systems.

Understanding the Syntax: The ls -l Command 👀
The most common way to view file permissions is by using the ls -l (list long) command in your terminal. The output might look something like this:

-rw-r--r-- 1 user group 1024 Oct 27 10:00 my_document.txt
drwxr-xr-x 2 user group 4096 Oct 26 15:30 my_directory
Enter fullscreen mode Exit fullscreen mode

Let's break down the first part (-rw-r--r-- and drwxr-xr-x):

File Type (First Character):

  • -: Regular file
  • d: Directory
  • l: Symbolic link

Other less common types exist too!

Permissions (Next 9 Characters): These are divided into three sets of three characters, representing permissions for:

  • Owner (User): The user who owns the file.
  • Group: A group of users who have been granted specific permissions.
  • Others (World): All other users on the system.

Within each set of three characters, you'll see:

  • r: Read permission (allows viewing the contents of a file or listing the contents of a directory).
  • w: Write permission (allows modifying the contents of a file or creating, deleting, and renaming files within a directory).
  • x: Execute permission (allows running a file as a program or accessing files within a directory - for directories, it's often called "search" or "traversal" permission).
  • - : No permission granted.

In our example:

my_document.txt (-rw-r--r--): The owner has read and write permissions, the group has read permission, and others have read permission. It's a regular file.

my_directory (drwxr-xr-x): The owner has read, write, and execute permissions; the group has read and execute permissions; and others have read and execute permissions. It's a directory.

Number of Hard Links: (The 1 and 2 in the example) For regular files, this is usually 1. For directories, it's at least 2. This is a more advanced topic, so don't worry too much about it for now.

Owner: (The first user) The username of the file's owner.

Group: (The second group) The name of the group associated with the file.

File Size: (The 1024 and 4096) The size of the file or directory in bytes.

Last Modified Date and Time: (The Oct 27 10:00 and Oct 26 15:30) When the file or directory was last modified.

File or Directory Name: (my_document.txt and my_directory) The name of the file or directory.

Changing Permissions: The chmod Command ✍️
The chmod (change mode) command is your go-to tool for modifying file and directory permissions. There are two main ways to use it: symbolic mode and numeric mode.

Symbolic Mode
Symbolic mode is more intuitive for making specific changes. The basic syntax is:

chmod [who][operator][permission] filename
Enter fullscreen mode Exit fullscreen mode

who: Specifies who the change applies to:

  • u: User (owner)
  • g: Group
  • o: Others
  • a: All (user, group, and others)

operator: Specifies the action to take:

  • +: Add the permission
  • -: Remove the permission
  • =: Set the permission (overrides existing permissions)

permission: Specifies the permission to change:

  • r: Read
  • w: Write
  • x: Execute

Examples:

To give the owner execute permission to my_script.sh:
chmod u+x my_script.sh

To remove write permission for the group from report.pdf:
chmod g-w report.pdf

To set read and write permissions for the owner and only read permission for the group and others on data.csv:
chmod u=rw,g=r,o=r data.csv

Numeric Mode
Numeric mode uses a three-digit octal (base-8) number to represent the permissions for the owner, group, and others respectively. Each digit is a sum of the following values:

4: Read (r)

2: Write (w)

1: Execute (x)

0: No permission (-)

To calculate the numeric mode, add the values for the desired permissions for each category.

Examples:

Read and write for owner (4+2=6), read-only for group (4), read-only for others (4): 644

Read, write, and execute for owner (4+2+1=7), read and execute for group (4+1=5), read and execute for others (4+1=5): 755

The syntax for using numeric mode with chmod is:

chmod [mode] filename
Enter fullscreen mode Exit fullscreen mode

Examples:

To set my_document.txt to read/write for owner, read-only for group and others:

chmod 644 my_document.txt
Enter fullscreen mode Exit fullscreen mode

To set my_directory to full permissions for owner, read/execute for group and others:

chmod 755 my_directory
Enter fullscreen mode Exit fullscreen mode

Changing Ownership: The chown Command👤
Sometimes you might need to change the owner or the group associated with a file or directory. This is done using the chown (change owner) command. You'll typically need sudo privileges to do this.

Syntax:

sudo chown [user][:group] filename
Enter fullscreen mode Exit fullscreen mode

user: The new owner's username.

:group: (Optional) The new group name. If omitted, only the owner is changed.

Examples:

To change the owner of important_file.txt to newuser:
sudo chown newuser important_file.txt

To change the owner to anotheruser and the group to developers:
sudo chown anotheruser:developers another_file.txt

To change only the group of shared_directory to editors:
sudo chown :editors shared_directory

Important Considerations 🔑
Directory Permissions: Execute permission (x) on a directory is crucial for being able to access the files within it. Without it, even if you have read permission on a file inside, you won't be able to "enter" the directory.

Default Permissions (umask): When a new file or directory is created, it gets a set of default permissions determined by the umask (user file-creation mode mask) setting. You can view and modify your umask value.

Security: Understanding and correctly setting file permissions is vital for system security. Incorrect permissions can lead to unauthorized access or modification of sensitive data. Be mindful of who needs what level of access!

Conclusion 🎉
File and directory permissions are a core concept in Linux that might seem a bit technical at first, but with a little practice, you'll get the hang of it. Mastering these concepts will give you greater control over your system's security and organization. So, go ahead, play around with ls -l, chmod, and chown in a safe testing environment, and unlock the power of Linux permissions! Happy tinkering! 🐧

Top comments (0)