Linux File Permissions and Ownership: A Comprehensive Tutorial

how to check file permission in linux

Concurrent usage by a large number of users is a significant edge of Linux-based systems. Multiple users can work on a server without disruptions or conflicts in these environments.

However, when people share access to files and resources, they risk exposing confidential information or losing data as a result of the actions of other users making changes to files or folders. 

Unix introduced file permissions to tackle this issue, allowing users to specify how much control each user account has over a particular file or directory.

In this tutorial, we will discuss the idea of Linux file permissions. Starting with a look at how you can check the current file permissions, we will then go into the details of changing or updating these permissions to control access to these resources. 

Table Of Content

  1. How to Check File Permissions in Linux
    1. How to Check File & Directory Permissions From the GUI
    2. How to Check File Permissions in Command-Line with the ls Command
  2. How to Change File Permissions Using chmod
    1. Define File Permission with Symbols
    2. Define File Permission in Numeric Mode
  3. How To Change User File and Group Ownership
  4. Conclusion
  5. FAQs

How to Check File Permissions in Linux

Verifying the existing configurations is the first step in working with Linux file permission. In any Linux environment, you can do this in two ways – through the graphical interface or using the command line. 

We will now describe both approaches so you can select the approach that best fits your circumstances and skill level.

How to Check File & Directory Permissions From the GUI

Locating file or directory permissions through the graphical user interface (GUI) is easy. We will now present a general process you can follow on your Linux system. However, note that the specific steps may vary depending on the distribution and the GUI client. 

  1. Locate the file you need to examine. Choose Properties from the right-click context menu.
  2. In the file properties window, you can see the basic file information. Navigate to the Permissions tab, which is usually the second tab of the window.

context menu

  1. In the Permissions tab, you will notice that the permission for each file is a combination of three levels:
  • Owner (the person who made the file/directory)
  • Group (the group to which the owner belongs)
  • Others (all remaining normal users)

For every file, the owner can allow or limit user access based on the categories they belong to.

permission tab

In our example, all other users and group members have “Read-only” access, but the owner of the test.txt file has “Read and write” access. This implies that other users are limited to opening the file, but unable to edit it.

At this point, the owner can adjust the file access settings by viewing the appropriate option from the drop-down menu. 

Furthermore, by ticking the Execute box, you may make the file executable and allow it to function like a program.

How to Check File Permissions in Command-Line with the ls Command

If you’re comfortable with the command line, you can use the ls command to verify the permission settings of a file. 

This command gives you detailed information about files and directories. We recommend using the -l flag with the command to examine this information.

To see how a file’s permissions are set, use the following command:

# ls –l [file_name]

For example, the following command will print detailed file permission for the test.txt file: 

# ls –l test.txt

ls -l test

As you can see from the above screenshot, the command’s output gives the following details:

  • File permission(s)
  • The person who created the file (owner)
  • The group to which the owner belongs
  • The date the file was created.

You can also see the shorthand for file permissions in a string of characters (-, r, w, x). Let’s take a close look at this shorthand: 

  1. The first symbol indicates the file type. It can be a regular file (), a directory (d), or a link (i).
  2. File permission of the user (owner)
  3. File permission of the owner’s group
  4. File permission of other users

file type symbol

The letters r, w, and x represent read, write, and execute. Each ownership category (owner, group, others) can have all three privileges, a combination, or none at all (denoted by , indicating denial).

In practical terms, user accounts with reading permission (r) can view the content of a file (or files in a directory). However, they can’t make changes (nor add/remove files in a directory). On the other hand, people who have write permission (w) are able to change (add and remove) files. Last but not least, the ability to execute allows the user to run the file—a feature frequently employed to run scripts.

The outcome of the last example showed that test.txt is a normal file. While the group and others only have read-only permission, the owner has both read and write access.

rw --r --r

How to Change File Permissions Using chmod

Users need to change file permission settings frequently to provide or remove read, write, and execute permission settings of a file or directory. 

Usually, the easiest way to change permissions is to use the chmod command. The basic syntax of the command is as follows:

# chmod [permission] [file_name]

There are two methods to specify permission:

  • Use symbols ( usually alphanumeric characters)
  • Use octal values

Define File Permission with Symbols

To set permission using alphanumeric characters, you have to specify specific values for the user/owner (u), group (g), and others (o).

The syntax of the chmod command in this context involves adding the first character for each class, the equal sign (=), and finally, the character for the privileges to read (r), write (w), and/or execute (x).

You can use the following command to make a file read, write, and executable by all users.

# chmod u=rwx,g=rwx,o=rwx [file_name]

For instance, to set the following permissions for the test.txt file:

  • read and write for the user
  • read for the members of the group
  • read for other users

Use the following chmod command:

# chmod u=rw,g=r,o=r test.txt

Note: Remember to use commas instead of spaces to separate the categories.

Define File Permission in Numeric Mode

Another method to define user permission is by using the octal or numeric format. This alternative might not be as simple as the last one. However, many users consider it much quicker and require less typing effort.

Instead of letters, this method uses the octal format that expresses privileges through numbers:

  • The value for r(ead) is 4.
  • The value for w(rite) is 2.
  • The value for (e)x(ecute) is 1.
  • The value for no permission is 0.

In some cases, the privileges are added together and represented by a single number. Thus, the potential values are:

  • 7 – for read, write, and execute access.
  • 6 – for read and write access.
  • 5 – for read and execute access.
  • 4 – for read access.

As you must define permission for every category (owner, group, and user), the command will have three (3) integers that add up the rights.

For example, let’s consider the test.txt file. In the previous example, we configured file permission symbolically using the command chmod u=rw,g=r,o=r test.txt.

The identical permission settings can be established using the octal format with the command:

# chmod 644 test.txt

chmod test

How To Change User File and Group Ownership

Apart from altering permissions for files and directories, you might encounter a situation where changing the user file ownership or group ownership becomes necessary.

To perform either of these tasks, you first need to switch to the superuser role. Now, you can run the following commands: 

Use the chown command to change the file ownership. The syntax of the command is as follows:

# chown [user_name] [file_name]

Remember to replace [user_name] with the name of the user who will take over as the file’s new owner. 

Similarly, to modify the group ownership, enter the following command:

# chgrp [group_name] [file_name]

Like before, remember to enter the name of the group that will take over as the file’s new group in the place of [group_name].

Conclusion

Mastering the fundamental commands to check and modify permissions for Linux files and directories is essential for all users. If you want to learn about changing group permissions for files, we described the use of the chgrp command.

Whether you like using the graphical interface or the command line, this guide aims to enhance your understanding of file permissions. If you’re looking for a reliable dedicated hosting provider, consider checking out RedSwitches for your hosting needs.

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

 Q. What are Linux file permissions?

Linux file permissions are a collection of guidelines that specify the amount of access and who may view, edit, and execute a file or directory.

Q. How do I change permissions in Linux?

In Linux, the chmod command, the permission settings, and the filename or directory name are used to modify permissions.

Q. What is file ownership in Linux?

File ownership in Linux refers to the user and group that are assigned as the owners of a file or directory, determining who has control over it.

Q. How do permissions work in Linux?

Permissions in Linux work by assigning different levels of access (read, write, execute) to the file owner, group members, and others using a combination of symbols and numbers.

Q. What are special file permissions in Linux?

Special file permissions in Linux are additional access control settings such as the setuid, setgid, and sticky bit, which provide specific functionalities for certain files and directories.

Q. How do I change ownership using the chown command?

In Linux, you may use the chown command to change the owner of a normal file or directory. You can then provide the new owner and, optionally, the new group.

Q. Why are permissions and ownership important in Linux?

Permissions and ownership in Linux are important for maintaining security features and control over files and directories, preventing unauthorized access, and ensuring proper management of system resources.

Q. How are permissions represented in Linux?

Permissions are represented in Linux using a combination of symbols (-, r, w, x) and numbers (0-7) to indicate the level of access for the file owner, group members, and others.

Q. What are directory permissions in Linux?

Directory permissions in Linux determine who can access and perform operations on a current directory, such as listing its contents, creating or deleting files, and navigating through it.

Q. What is the chmod command and how does it work?

In Linux, the chmod command modifies a file or directory permissions by giving the required permission settings and the access mode (symbolic or numeric).