On Unix-like operating systems like Linux, the operating system consists of files and folders. With everything coming back to files and folders, access control of them is extremely important. Think about it with the right level of access you can do anything. File permissions allow us to restrict access to only those who should have that access.
In this blog post, we will go over basic file permissions, what they are, how they are represented, how to check, and how to change them.
What Are File Permissions?
File permissions control access to files and directories. It specifies what users or groups can access, read, and write to specific files and directories.
Permission on Linux is split into three groups which help separate who has access to what files and directories. Below are those three in the order they will be shown.
- File Owner
- Group Owner
- Other users
How Permissions Are Represented
Permissions are represented in one of two ways text or numeric. They include no permission, read access, write permission, and execute permissions (for example needed by applications, shell scripts, and folders). Below shows how they are represented both ways.
Text
- r The file can be read
- w The file can be written to
- x The file has execute permission (allows a file to be run as an application)
- – no permissions
Numeric
Now let’s see which number matches to the text version shown above:
- r=4
- w=2
- x=1
- –=0
Putting It Together – Checking File Permissions
The easiest way to check file permissions is to use the ls command using the -l flag as shown below.
ls -l
Running this command will list the files and directories in your current working directory
For example:
-rw-r--r-- 1 example example 2541 May 8 18:25 file.txt
In this example the example user has read and write.
Where the example group and other users would only have read access
In numeric form, this would be displayed as 644.
Changing File Permissions
To change file permissions on Linux use the chmod command. The basic syntax for the command is shown below.
chmod [options] file
Options would typically be the file permissions you want to apply or change as shown in the below section.
Chmod Examples
Both text and numeric options can be used with chmod as shown in the examples below.
Adding a specific permission for all three groups
chmod +x file.txt
This option will remove the executable bit to all three permissions so if the file was 755 it would now be 644 or -rwxr-xr-x.
Note this can be used with the other options (r and w) just remember it applies to all three permission groups.
Removing specific permission for all three groups
chmod -x file.txt
This option will make remove the executable bit to all three permissions so if the file was 755 it would now be 644 or -rwxr-xr-x.
Setting permissions in numeric form
chmod 640 file.txt
Setting the permission means that only the file owner can edit the file, whereas the only other users who can access the file below belong to the group owner.
Conclusion
File permissions on Linux, are an absolute necessity for access control. It allows for varied levels of keeping users away from files they don’t need access to. In this blog post, we focused on file permissions from the command line. We explored basic Linux file permissions from what they are, how they are respresented, how to check, and how to change them.
Additional Links
Done reading and looking for additional links why not check these out?
Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply