How to check file permission(s) in Linux
When the “permission denied” error occurs on a file or a directory operation, start by checking the allocated permissions for the user, group and other. To do that, you can use the ls -l command. For example, the following are permissions for various files and directories in the “/var” directory. The output above contains specific information. Once you know the file permissions of your working directory or file, you can change them easily.
How to use chmod to change file permissions
The chmod command is the easiest way to change file permissions in Linux. The general syntax for the command is:
Permission: This represents the permissions given to a user, group, owner, or all.Operator: this indicates permissions given to the permission’s assignee: + grants permission, - denies permissions, and = specifies which permissions to set.Mode: this dictates which permissions to set: r read, w write, or x execute.
For more information about chmod and how it works, check out this article. For example, to deny everyone write access to the backup directory in the “/var” directory, you can use the command: Although changing file permissions using the chmod command is effective, it only changes file permissions for the specified directory or file. Fortunately, you can recursively change the file permissions of a directory or file and its sub-directories and files. To do that, use the chmod command recursive -r option. For example, let’s recursively remove read permissions for the “/var/backup” directory and all its files and subdirectories. The current permissions are: Now let’s run chmod recursively to change the permissions: Now you can see that the file permissions have changed.
Recursive change permission with find
Typically, you will not be setting files and directories with similar permissions. This is because you need the execute permission in order to cd into a specific directory. However, most files do not require this execute permission. To overcome this, you can use the find utility in Linux. This allows you to only find specific files or directories and set permissions on them. For more information on using Find in Linux, check out this article. Let us take the “/var/log” directory. The directory contains both files and directories. To set specific permissions for files only, we can use the find command with the -type f option. See an example command below: In the above example, we used the find command to search the “/var/log” directory for files, then set read, write and execute permissions for users, groups and all. We can also do the same case for directories. In this case, we specify the -type d to only get directories. For example: In the above example, we only set the permission 755 to directories and not both files and directories. You can verify this by using the ls -la command. You will notice that all files have the permission 777, while directories have 755 permissions.
In Closing
Knowing how to change file permissions in Linux is an essential skill, as it helps prevent unauthorized access and modifications to various files and directories. Meanwhile, you can also change file ownership to restrict file access or use am access control list for even finer control of file permissions in Linux.