Being an expert user or administrator of a Linux operating system environment requires us to have a mastery of Linux file management. Everything under the Linux operating system environment is regarded as a file, from created or existing files and folders to internal or external hardware devices like USB devices.
Linux file management accounts for a larger proportion of the Linux OS operations and starts at a folder/directory level. Linux adheres to a tree-like structure with a base directory that leads to other sub-directories or sub-folders used to maintain created or already-existing files.
Types of Files in Linux
Before we look at handling file permissions in Linux, it is important that we briefly introduce the three types of files you will most likely handle under Linux.
- Regular Files – These types of files are common and include image, binary, and text files.
- Directories – These are files responsible for the storage of listed file names and other relevant file information. Common directories in Linux include the root (/) directory, home (/home/) directory, bin (/bin) directory, and boot (/boot) directory.
- Special Files – These types of files include real physical devices like external keyboards, hard drives, USB drives, and printers. The Linux operating system lists them as ordinary directory files.
Linux File Permissions
File permissions are a nagging issue, especially when dealing with script files. For instance, you can easily create a text file using the Linux touch command, edit it, and save it without any file permission issues as demonstrated below.
$ touch sample.txt $ nano sample.txt
However, if we attempt to create a script file and immediately run it, we are bound to run into file permission issues.
$ touch test.sh $ ./test.sh
Importance of Linux File Permissions
From the above screenshot demonstration, it might seem unfair that you have the right user access to the Linux system you are using yet you can’t create and execute script files without running into the Permission denied error prompt.
Such file execution barriers are in place to prevent the execution of scripts that might jeopardize the functional integrity of the Linux operating system unless that user is fully aware of their OS actions.
How to Change Linux File Permissions
We will now address how to change various file permissions in Linux.
Make a File Executable
The first step is to use the ls command to identify the default file permissions associated with the targeted file.
$ ls -l test.sh
The output portion
-rw-rw-r from the above implies that this file has read and write permissions but no executable
(x) permission. To make this script executable to both user and group, we will use the chmod command.
$ chmod +x test.sh
Let us confirm that the file is now executable.
$ ls -l test.sh
Change File’s Owner Permission to Read Only
A user will only read the file and neither write nor execute it.
$ chmod u-w test.sh
Change Group Permission to Read Only
Members of a user group will only read the file and not be able to write in it.
$ chmod g-w test.sh
Give File’s Owner Write Permission
A user will be able to write to the file.
$ chmod u+w test.sh
Change Group Permission to Write
Members of a group will be able to write to file.
$ chmod g+w test.sh
Applying for Multiple File Permissions
Let us for instance assume we want the file owner to have to execute permission and the associated group has to write and execute permissions, we will implement the following command:
$ chmod u+x,g+wr test.sh
When dealing with directories and want to recursively make file permission changes to them, use the chmod command with the
$ chmod -R u-w,g-w one
In the above case, the user and group can read the directory but cannot add (write) files/folders.
With this brief article guide, you should now be a master of Linux user and group file permissions in relation to granting or revoking Read, Write and Execute permissions.