Skip to main content

File permissions in linux

 In Linux, there are three different types of permissions that can be assigned to files and directories: read, write, and execute. These permissions can be assigned to three different classes of users: the owner of the file or directory, the group that the file or directory belongs to, and all other users.

The following are the different types of file permissions in Linux:

  1. Read (r): The read permission allows a user to view the contents of a file or directory. For directories, the read permission allows a user to list the files and directories inside it.

  2. Write (w): The write permission allows a user to modify the contents of a file or directory. For directories, the write permission allows a user to create, delete, and rename files and directories inside it.

  3. Execute (x): The execute permission allows a user to execute a file or access the contents of a directory. For directories, the execute permission allows a user to access files and directories inside it.

These permissions are assigned to three different classes of users:

  1. Owner: The owner of a file or directory is the user who created it. The owner can have any combination of read, write, and execute permissions.

  2. Group: A group is a collection of users who share the same permissions to a file or directory. The group can have any combination of read, write, and execute permissions.

  3. Others: The others class includes all other users who are not the owner or members of the group. The others class can have any combination of read, write, and execute permissions.

In Linux, file and directory permissions can be set using the chmod command. The chmod command allows you to set permissions for the owner, group, and others, and to set permissions in symbolic or numeric mode.

How to change file permission in linux

The chmod command in Linux is used to change the file permissions of a file or directory. Here are some examples of how to use the chmod command to change file permissions:

  1. Changing permissions using symbolic mode:

To change the permissions of a file using symbolic mode, you can use the following command:

chmod [permissions] [file]

For example, to give the owner of a file read and write permissions, and to give the group and others only read permissions, you can use the following command:

chmod u=rw,g=r,o=r [file]

Here, 'u' stands for user, 'g' stands for group, and 'o' stands for others. 'r' stands for read permission, and 'w' stands for write permission. The equals sign '=' sets the permissions, while the comma ',' separates the permissions for different classes of users.

  1. Changing permissions using numeric mode:

To change the permissions of a file using numeric mode, you can use the following command:

chmod [numeric permissions] [file]

The numeric permissions are represented by a three-digit number, where the first digit represents the owner's permissions, the second digit represents the group's permissions, and the third digit represents the permissions for others.

Each digit is calculated by adding the values of the permissions you want to assign. 'r' has a value of 4, 'w' has a value of 2, and 'x' has a value of 1. For example, to give the owner read and write permissions, and to give the group and others read permissions only, you can use the following command:

chmod 644 [file]

Here, the first digit represents the owner's permissions (6 = 4 + 2), the second digit represents the group's permissions (4 = 4), and the third digit represents the permissions for others (4 = 4).

Note that you need to have the appropriate permissions to change the file permissions using chmod command.


Comments

Popular posts from this blog

Virtual Memory(VIRT), Shared memory(SHR) and Resident memory(RES) explained

Do you know what is VIRT(virtual memory), RES(resident memory) and SHR(shared memory) really mean in top command? - Let's find out. Resident Memory - It is part of the RAM currently used by the process. RAM is logically divided into memory pages of certain size(for ex: 4096 bytes- 4 kb), memory is assigned to a process in terms of memory pages. A memory page can be associated with one process(if page is not shared) or multiple process(if a page is shared). The number of memory pages used by a process defines the resident memory(RES) it use. If you see first process(pid 25390) in top output, which is taking 933620 KB of resident memory(RES) the number of memory pages it is currently using can be calculated as:   Number of pages used = memory used(in KB)/Page size(in KB)     - Formula 1   As per this formula, first process(pid 25390) is using 233405(933620 KB/4              KB) memory pages currently. Note that page size o...

Calculating CPU utilisation of a process inside C program?

The top program provides a dynamic real-time view of a running system.  It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel. Mostly "top" command is used for checking CPU utilisation of processes, which helps user to check which processes are CPU intensive. By default " top " command refreshes output in interval of 3 seconds. So the percentage of CPU utilisation shown is the average CPU utilisation of a process in last 3 seconds. Check the below pictures, these are the pictures captured in 3 second interval in which top refreshes its output. Note that the time is marked in red and percentage CPU utilisation field is marked in green. top output at 15:52:22 shows percentage of CPU utilisation of processes between 15:52:19 to 15:52:22. Output refreshes after 3 seconds Suppose that user wants to see percentage CPU utilization of processes in 5 min then it can run top command...

Sticky bits in linux

Consider a scenario where you create a Linux directory that can be used by all the users of the Linux system for creating files. Users can create, delete or rename files according to their need in this directory. If you think why would such a directory be created? There exists, for example, /tmp directory in the Linux system that can be used by different Linux users to create temporary files. Now, what if a user accidentally or deliberately deletes (or rename) a file created by some other user in this directory? So to avoid these kind of issues, the sticky bit concept is used. A Sticky bit is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file. No other user is given privileges to delete the file created by some other user. Given below is the command to set sticky bit on on a file or folder: bash-4.2$ chmod +t accessibleByAll/ bash-4.2$ ls -ld accessibleByAll/ drwxrwxrw t 2 indresh indresh ...