LFCS Admin Exam preparation guide series, main page can be found here.
This post is part of Essential Commands from the domain competency list for the exam. The full list can be found in the link above paragraph or the Linux Foundation page here.
Today we are going to learn how we read and how we can change the security permissions of files in Linux OS.
To see the current permission set on the file we run the ls -l command, we will see a list of objects in the directory and as well the permissions. Let us try to explain what the first ten letters mean.
The first letter can be “-” means that this is a file or “d” – for directory and “l” – for links. The next three letters represent permission for the file owner, and they can be as follow “r” – read, “w” – write, and “x” – for execution. The following three letters are for permissions for groups, and the last three are for others; they can be represented by the same r,w,x depending on the current permission setting. The read, write, and execute are represented by octal numbers 4 for read, 2 for write, and 1 for execute.
The example below, after executing the ls -l, we can see four objects in the current directory. Two are “d” directories to which one user Sebastian is the owner and have all permissions, as well as the group Sebastian, and the others can only read and execute, which in this case means they can open the folder. Next, we have the file for which user Sebastian and group Sebastian have only read and write permission because this file is not executable, there is no “x” for executable, but we can see that others only can read the file. The las object is the “l” link to which everyone has full permission.
Lets us have a look now how we can change the permissions on the files, to do just that we will be using the change mode – chmod – command. To change permission for everyone is easier to use octal numbers. We add the numbers depending on what we want to allow. For example, to provide full access, we will execute chmod 777 <file name>. The first number 7 represents a user, second group, and third other. We use number 7 because 4+2+1 equals 7. If we like to give only read and write permission, we will use number 6. Of course, we can use the rwx letters, but we can set it for one set at the time, for example, to set permissions for the user, we will tape u+rwx, then we need to apply it for “g” group and “o” other. To remove access, we will use the same command but with a different set of numbers, for example, to give everyone read-only access and remove all to other we will use chmod 440 <filename> or chmod u-wx <filename>
Let us take a quick look for changing the ownership of the files, to do that we will use the chown command. The command will look like this chown user:group <filename>. In the below example, first we are changing ownership of the file to user1 without changing the current group ownership, the second command is changing the group ownership without changing the user.
Today we learn how to change the permissions and ownership of the files in Linux. Thank you for reading.