Linux Permissions isn't that hard to understand

What are these numbers and characters? Wanna find out? They're pretty easy to understand actually.

Linux Permissions isn't that hard to understand
Photo by Gabriel Heinzer / Unsplash

One of the key features of Linux is its robust and granular system of permissions. In this blog post, we'll explore Linux permissions, how they work, and how you can use them to secure your files and directories.

Understanding Linux Permissions

In Linux, each file and directory has three types of permissions: read (r), write (w), and execute (x). These permissions are set for three types of users: the owner of the file, members of the file's group, and everyone else.

The following table summarizes the basic Linux permissions:

Each user can have one of three levels of access to a file or directory: full access (rwx), read-only access (r-x), or no access (---).

In addition to these basic permissions, Linux also has special permissions, such as the setuid bit (s), setgid bit (s), and the sticky bit (t). These permissions are used to set special attributes for files and directories.

Changing Permissions

To change the permissions of a file or directory, you can use the chmod command, followed by a three-digit number or a combination of letters and symbols.

The three-digit number represents the permissions for the owner, group, and everyone else, in that order. Each digit is the sum of the permissions for read (4), write (2), and execute (1). For example, the number 755 gives the owner full access, members of the group read and execute access, and everyone else read and execute access.

The following code example shows how to change the permissions of a file using chmod:

$ chmod 755 myfile.txt

Alternatively, you can use letters and symbols to set the permissions. The letters u, g, and o represent the owner, group, and everyone else, respectively. The symbols +, -, and = represent adding, removing, and setting permissions, respectively.

For example, the following command sets the read and write permissions for the owner and the group:

$ chmod ug+rw myfile.txt

Code Examples

Here are some examples of how to use Linux permissions to secure your files and directories:

Restricting access to a directory

To restrict access to a directory, you can set the permissions to only allow the owner to read, write, and execute the directory. Members of the group and everyone else will have no access to the directory.

$ chmod 700 mydir/

Allowing group members to read and write to a file

To allow members of a group to read and write to a file, you can set the group permissions to read and write, while keeping the owner permissions to full access.

$ chmod 640 myfile.txt

Setting the setgid bit for a directory

To set the setgid bit for a directory, you can use the following command. This will ensure that any files or directories created in the directory inherit the group of the parent directory.

$ chmod g+s mydir/

By understanding how Linux permissions work and how to use them effectively, you can ensure that your files and directories are protected from unauthorized access and modification. With the right permissions in place, you can use Linux with confidence, knowing that your data is safe and secure.

About the author

Joff Tiquez, hailing from Manila, Philippines, is the individual behind the establishment of OSSPH. He is a web developer who strongly supports open source and has been overseeing projects like Vue Stripe for an extended period. To get in touch with Joff, you can visit https://bento.me/jofftiquez.