Permissions in Linux

Permissions in Linux

Understanding and Managing permissions in Linux

The purpose of this article is to give you a basic understanding of Linux permissions and to get you started with managing the same.

You can control access to files and directories on a Linux system using Linux permissions. Each file and directory is associated with a set of permissions that specify which users and groups can access it and how they can access it.

In Linux, there are three types of permissions: read, write, and execute.

  • A user with 'read' permission can view the contents of a file or directory.

  • A user with 'write' permission can modify the contents of a file or directory as well as create new files in a directory.

  • The 'execute' permission allows a user to run a program or navigate through a directory.

Permissions are represented by a group of three or four characters. The first character represents the file or directory owner's permissions. The second character represents the group to which the file or directory belongs. The third character represents all other users' permissions. If the file or directory is of a specific type, the fourth character is also set. The "ls -l" command displays the permissions of a file or directory.

ls -l

Output for the above command for the file 'file1.txt' :

error fetching the image

In the preceding example, the file "file1.txt" has read and write permissions for the owner and group, as well as read permission for all other users. The chmod command can be used to change the permissions of a file or directory. The chmod command accepts as arguments an octal number representing the permissions and a file or directory.

The octal number is created by adding the values for the permissions you want to set together. The read permission value is 4, the write permission value is 2, and the execute permission value is 1.

For example, the octal number 664 would be used to grant the owner read and write permissions, the group read and write permissions, and all other users read permission.

Below is a table depicting the octal numbers and permissions associated with it (personally I remember it the best as 421 =rwx)

number

associated permission

4

read

2

write

1

execute

To specify permissions, you can also use the chmod command in symbolic notation. In symbolic notation, "u" stands for the owner, "g" stands for the group, "o" stands for all other users, and "a" stands for all users. You can then use "+" to add permission, "-" to remove one, and "=" to set one.

For example, you can use the following command to grant the owner read and write permissions, the group read and write permissions, and all other users read permissions:

$ chmod ug=rw,o=r file1.txt

Output for the above command:

To change the owner and group of a file or directory, use the chown command. The chown command takes as arguments a user and a group, followed by the file or directory.

For example, you can use the following command to change the owner of the file "file1.txt" to the user "test1" and the group to "testing" (the username and group would differ in your case ):

chown test1:testing file1.txt

Output:

Noticed how I used sudo in addition to the command that I mentioned above?

I did this because file1's owner is the user 'kali,' and the file belongs to the group 'kali,' the above command would make user 'test1' the owner of file1.txt and the group 'testing'. This would result in the actual owner's rights being washed away, and he would no longer be able to control the file as before. As a result, it is recommended that you do not change the ownership of a file unless necessary by using the chown command. Linux by default doesn't allow changing ownership with normal privilege due to security reasons so we use sudo (superuser do) command instead which means that our command would be treated as it was given by the user of the highest privilege of that system.

Note:

  • A directory always has "x" i.e executable permissions by default when created and you should keep it that way since without "x" permission a directory can't be opened.

  • A file by default has "rw-rw-r–"

To conclude, Linux permissions allow you to control access to files and directories on a Linux system. The ls and chmod commands are used to view and change permissions, and the chown command is used to change the owner and group of a file or directory. You can securely manage the access and ownership of your files and directories on a Linux system by understanding and properly utilizing Linux permissions.

That's what makes Linux so good: you put in something, and that effort multiplies. It's a positive feedback cycle.

-Linus Torvalds

Reference:

Official Ubuntu documentation