How to Set and Manage Permissions in Linux: A Comprehensive Guide

Introduction to Linux permissions

Hi everyone, in today’s lesson, we’re diving into a crucial aspect of Linux administration: how to set permissions in Linux. This guide is aimed at helping you to understand and implement file permissions effectively to secure your system from external and internal threats, as well as prevent accidental occurrences, such as deletion of critical files, or running of critical scripts that could compromise system stability.

Why Do We Set File Permissions?

File permissions are fundamental in any operating system for several reasons:

  1. Security: Protecting the system from unauthorized access and potential attacks.
  2. Integrity: Preventing accidental modifications or deletions of critical files.
  3. Control: Ensuring that only authorized users can execute certain actions.

As an administrator, knowing how to set permissions in linux, is crucial for maintaining the health and security of your Linux system.

Understanding File Attributes

Before diving into permissions, it’s essential to understand file attributes. Every file in Linux has specific attributes that determine who can read, write, or execute the file.

how to set permissions in linux

In the image above, a file was created, and it’s attributes and permissions were shown, using the ls -l command.

These attributes and permissions will be properly explained in the next couples of sections.

Creating and Viewing File Attributes

Let’s start by creating a file to examine its attributes. If you’re unfamiliar with basic Linux commands, consider checking out my “Learn Linux from Scratch” series for foundational knowledge.

To create an empty file, use the touch command:

touch file1

Now, let’s view the file attributes using the ls -l command:

ls -l file1

You’ll see an output similar to the one in the image above:

-rw-r--r-- 1 victor victor 0 May 29 12:34 file1

Here’s a breakdown of what each part means:

  • -rw-r--r--: The file permissions.
  • 1: Number of hard links.
  • victor: The owner of the file.
  • victor: The group owner of the file.
  • 0: File size in bytes.
  • May 29 12:34: Date and time of the last modification.
  • file1: The name of the file.

File Permissions Explained

The first part, -rw-r--r--, represents the file permissions divided into four sections:

  1. File Type: The first character (-) indicates the type of file (- for a regular file, d for a directory, l for a symbolic link, etc.).
  2. Owner Permissions: The next three characters (rw-) indicate the owner’s permissions (read and write in this case).
  3. Group Permissions: The following three characters (r--) show the group’s permissions (read-only here).
  4. Others Permissions: The last three characters (r--) indicate the permissions for others (also read-only).

This simply means that, for a file with the following file permissions
-rw-r--r--
The owner, who is the user stated, can read and write only.
The group, and other users in the group, can read only.
A user who is neither the file owner, nor a member of the group, can also read only.

Example with a Directory

Let’s create a directory to see the difference between file permissions and directory permissions:

mkdir directory1
ls -l directory1

You’ll see something like this:

how to set and manage file and directory permissions in linux
drwxr-xr-x 2 victor victor 6 Jan 22 03:17 directory1

Here, the first character is d, indicating that this is a directory.

How to Set Permissions in Linux

Permissions can be set in two modes: symbolic and absolute.

Symbolic Mode

In symbolic mode, we use symbols to change permissions.

  • u: User (owner)
  • g: Group
  • o: Others
  • r: Read
  • w: Write
  • x: Execute

For u (user), we can use the following symbols:
u+r means, give a read permission to user
u-r means, remove a read permission from user
u+w means, give a write permission to user
u-w means, remove a write permission from user
u+x means, give an execute permission to user
u-x means, remove an execute permission from user

For g (group), we can use the following symbols:
g+r means, give a read permission to the group
g-r means, remove a read permission from the group
g+w means, give a write permission to the group
g-w means, remove a write permission from the group
g+x means, give an execute permission to the group
g-x means, remove an execute permission from the group

For o (others), we can use the following symbols:
o+r means, give a read permission to others
o-r means, remove a read permission from others
o+w means, give a write permission to others
o-w means, remove a write permission from others
o+x means, give an execute permission to others
o-x means, remove an execute permission from others

Examples

  1. Add Execute Permission to User:
   chmod u+x file1
   ls -l file1

Output:

   -rwxr--r-- 1 victor victor 0 May 29 12:34 file1
  1. Remove Write Permission from Group:
   chmod g-w file1
   ls -l file1

Output:

   -rwxr--r-- 1 victor victor 0 May 29 12:34 file1
  1. Add Write Permission to Group:
   chmod g+w file1
   ls -l file1

Output:

   -rwxrw-r-- 1 victor victor 0 May 29 12:34 file1

Absolute Mode

In absolute mode, permissions are set using numbers:

  • 4: Read
  • 2: Write
  • 1: Execute

The numbers are added together to set multiple permissions. For example:

  • 7 (4+2+1): Read, write, and execute
  • 6 (4+2): Read and write
  • 5 (4+1): Read and execute
  • 4: Read only

Examples

  1. Set All Permissions for User, Group, and Others:
   chmod 777 file1
   ls -l file1

Output:

   -rwxrwxrwx 1 victor victor 0 May 29 12:34 file1
  1. Set Read and Write for User, Read for Group, and No Permission for Others:
   chmod 640 file1
   ls -l file1

Output:

   -rw-r----- 1 victor victor 0 May 29 12:34 file1
  1. Set Read, Write, and Execute for User, Read and Execute for Group, and Read Only for Others:
   chmod 754 file1
   ls -l file1

Output:

   -rwxr-xr-- 1 victor victor 0 May 29 12:34 file1

Changing Directory Permissions

The same principles apply to directories. For example:

chmod 755 directory1
ls -ld directory1

Output:

drwxr-xr-x 2 victor victor 4096 May 29 12:34 directory1

Conclusion

Understanding how to set permissions in Linux is a vital skill for any Linux system administrator. By using both symbolic and absolute modes, you can precisely control who has access to your files and directories, ensuring your system’s security and integrity.

Thank you for reading! If you have any questions or need further clarification, please refer to the links below on how to set permissions in linux, or reach out via the provided contact details. Happy learning!


Youtube Tutorial On How To Set Permissions In Linux

Additional Resources

Don’t forget to subscribe to our channel, like, share, and comment. Your support encourages us to create more educational content. Thank you and bye for now!

RHCSA 9 /EX200 EXAM PRACTICE QUESTION AND ANSWER

Be the first to comment

Leave a Reply

Your email address will not be published.


*