An Actionable Guide to Linux File Permissions

change file permissions linux

Linux is all about security. That’s why it’s the preferred OS for servers hosting mission-critical business applications.

A typical Linux server can have a huge number of files and directories that users can access and modify. This is not a great situation because server administrators and users need to control access to files because of security and privacy reasons.

Fortunately, Linux offers a simple solution to managing and controlling file and directory access in the form of file permissions. This system allows all users on a Linux server to set up and change how others can access and use the files.

In this article, we’ll discuss file permissions in Linux. Starting from the discussion around file permissions, we’ll explore several use cases for inspecting and modifying file permissions.

Let’s start with an overview of Linux file permissions.

Table Of Content

  1. What are File and Directory Permissions
  2. Types of File Permissions in Linux
  3. How to Check File Permissions
  4. FAQs

What are File and Directory Permissions

Let’s take a minute to clarify what file permissions are and how they function before moving on to how to adjust file access rights.

Every file and directory in Linux is protected by a set of rights that control who may view, edit, and execute them.

These file permissions can potentially affect everything, including the stability and security of the operating system and the server. Server admins may restrict who has access to important information by changing file permissions. As a result, they can stop unauthorized changes and protect the system from dangerous attacks.

In Linux, every file and directory has one of three permission types: read, write, or execute. Now factor in the three user levels (user, owner, others), and you can imagine the flexibility Linux brings to managing access to files and directories.

Let’s go into a bit more detail about the permission types and users.

Types of File Permissions in Linux

Linux (and all *nix systems in general) offers three types of permissions for files and directories.

Read (r)

This permission allows users to open and read files directly or through associated applications. However, users with read permission can’t edit the contents of the files.

Write (w)

This permission allows users to read and modify the contents of files. They can also rename or delete files. An interesting situation occurs when a user has write permission for files but not for the directory that contains these files. In this case, users can access and edit the files but cannot delete them (because they don’t have write permission for the directory.

Execute (x)

This permission applies especially to executable files. A user needs the execute permission to run anything on the system. This security check is important because administrators can only allow trusted users to execute anything on their servers.

As you can imagine, a file can have a maximum of rwx permission, allowing the user to access, read, modify, and delete the file. On an interesting note, a file can have different permission for different users. For instance, a server administrator can have full rwx permissions for a file, while a user could only have rw permissions.

Permissions For Each User

When it comes to file permissions, Linux recognizes three types of users.

Owner is the user who created the file or directory.

Group is a collection of people with access to duplicate files or directories and the same permissions.

Others refer to all the other users who have access to the system.

Each user has a set of permissions to a file. For instance, the Owner might have full rwx permission. On the other hand, the same file might have different permission when it’s part of a service group.

How to Check File Permissions

You can easily check any file’s permission with the ls command with the -l option.
The typical syntax is

ls -l <filename>

For instance, here’s how you can check the permissions for the textfile.txt

# ls -l testfile.txt

The output would look something like:

# ls -l testfile.txt

The output shows the file permissions for each user type (rw for the Owner, r for the Group, and Others).

Now that you know how to check file permissions, the next step is to see how you can modify file permissions.

How to Modify File Permissions

You can use the chmod command to modify a file or directory’s permissions. The syntax for the chmod command is:

# chmod [options] mode file

Here

The “options” parameter is used to specify extra command options.
The mode specifies the new file or directory permissions.
The file is the file’s name or directory for which you need to change the permission.

The chmod command is very flexible and you can set the command’s mode through symbols or numbers.

Let’s see these options in detail.

Use Symbols to Set the Permissions

When setting the permission through the chmod command, you can use symbols as shorthand to define the permission parameters. Here’s the list of symbols:

+, which adds a permission
-, which removes a permission
=, which sets the exact permissions

These symbols are used in combination with the letters r, w, and x, which indicate the read, write, and execute rights.

For example, use the command to provide execute permission to the owner of a file named myfile.txt. This command adds execute permission (+x) to the file’s owner (u):

# chmod u+x testfile.txt

# chmod u+x testfile.txt

Now, if you wish to revoke the owner’s execute permission, use the following command that removes the execute permission (-x) from the file’s owner (u):

# chmod u-x testfile.txt

# chmod u-x testfile.txt

Here’s another way to set the permissions of a file or directory:

# chmod 644 testfile.txt

# chmod 644 testfile.txt

This command changes the file’s permissions to rw-r–r–. The first digit (6) represents the owner’s permissions, the second digit (4) represents the group’s permissions, and the third number (4) represents everyone else’s permissions.

Use Numbers to Set the Permissions

Numbers can also be used to specify the mode of the chmod command. Each permission type is given a specific value:

  • r = 4
  • w = 2
  • x = 1

By combining these numbers, each group’s permission value is computed. For instance, rw- has the value 6 (4 + 2), r-x has the value 5 (4 + 1), and rwx has the value 7 (4 + 2 + 1).

Use the following command to change the permissions of a file or directory with numbers.

# chmod 644 testfile.txt
This command changes the file’s permissions to rw-r–r–. The first digit (6) represents the owner’s permissions, the second digit (4) represents the group’s permissions, and the third number (4) represents everyone else’s permissions.

Typical Use Cases For Setting File Permissions

Now that you know how to adjust file rights in Linux, let’s look at some typical file permissions use cases.

Giving Read-Only Access

If you wish to grant read-only access to a file or directory to all users, use the following command to set the permissions to r–r–r– (444):

# chmod 444 testfile.txt

# chmod 444 testfile.txt

Give Read and Write Access

If you wish to provide read and write access to a file or directory to all users, use the following command to set the permissions to rw-rw-rw- (666):

# chmod 666 testfile.txt

# chmod 666 testfile.txt

Give Execute Access

If you wish to grant execute access to a file or directory, use the following command to set the permissions to rwxr-xr-x (755):

# chmod 755 testfile.txt

# chmod 755 testfile.txt

This command changes the file’s permissions to read, write, and execute for the owner and read and execute for everyone else.

Conclusion

File permissions are critical components of Linux security and stability. You can regulate who has access to your sensitive data, prevent unauthorized alterations, and safeguard your system from dangerous assaults by understanding how file permissions operate and how to adjust them.
This post covered how to view and alter file permissions in Linux. We’ve gone over the fundamentals of file permissions, how to see and adjust file permissions, and several frequent use cases for modifying file rights. With this understanding, you can confidently control your Linux system’s file permissions and keep your data safe and secure.

FAQs:

Q: What are file permissions?

A: File permissions are the parameters that control who may access a file and what actions they can take on it. Permissions are classified into three types: read, write, and execute.

Q: How do I view file permissions?

A: Use the “ls -l” command to see file permissions in a terminal. The first column of the report shows the file permissions.

Q: How do I change file permissions?

A: Using a terminal’s “chmod” command to change file permissions. The command syntax is “chmod [permissions] [filename]”. To give the owner of a file read and write access, use the command “chmod u+rw [filename]”.

Q: What are the different types of users that can have file permissions?

A: File permissions can be granted to three sorts of users: the file’s owner (u), users in the same group as the owner (g), and all other users (o).

Q: What are the different types of file permissions?

A: There are three sorts of file permissions: read (r), write (w), and execute (x). The read command enables the user to examine the contents of a file, the write command allows the user to edit the contents of a file, and the execute command allows the user to run a file as a program.

Q: How do I give multiple users the same permissions?

A: You may grant the same rights to many users by using the “chmod” command with the necessary arguments. For example, to give read and write access to the owner and all users in the same group as the owner, use the command “chmod ug+rw [filename]”.

Q: How do I remove file permissions?

A: File permissions can be removed using the “chmod” command with the necessary arguments. For example, use the command “chmod a-w [filename]” to remove write access for all users