We can determine the permissions of a file by looking at the mode bits of that file.
Mode Bits Structure
The mode bits structure of files and directories:
Permissions of Directory and File
Let’s take a look at the default permissions for the newly created files and directories:
As we can see from the following table, the directory is:
- readable, writable and executable to the user: the user called root;
- readable and executable to the group: everyone in the root group;
- readable and executable to others: other users.
Without execute permission, we can’t access the directory.
Without write permission, we can’t add new files or delete existing files in a directory.
As we can see from the following table, the file is:
- readable and writable to the user: the user called root;
- readable to the group: everyone in the root group;
- readable to others: other users.
We can set permissions for users, groups, others, and everyone in symbolic mode.
We can assign read, write, and execute permissions to the file owner, file group, others or everyone through the plus, equal sign operator, or remove the appropriate permissions from the user by the minus sign operator:
There are four users, file’s owner, file’s group, others, and all:
|u||Owner of the file|
|g||Group of the file|
|o||Any other users|
|a||All users, this is equivalent to ugo|
There are three actions, append, remove, and assign:
|+||Append the permission(s) to user(s)|
|-||Remove the permission(s) from user(s)|
|=||Assign the permission(s) to user(s)|
There are three permissions, read, write, and execute, respectively:
|r||Readable or Viewable permission|
|w||Writable or Editable permission|
|x||Executable or Runnable permission|
We can append the permissions of the user after the addition symbol to the permissions of the user before the addition symbol:
|Copy To||Operation||Copy From|
Changing Special Mode Bits
Set User or Group ID Mode Bit
Impact on Directories
If a directory has the group id set up, the files created in this directory belong to the group of the directory, but setting the user id does not seem to have much effect. A child directory created in this directory inherits the group to which the parent directory belongs and also inherits the special mode bit set group id.
Impact on Programs or Files
If a program is set up with user id, then whoever executes it, the operator is always the owner of the program. If a program is set up with group id, then no matter who executes it, the runtime group is always the group to which the application belongs.
Set Restricted Deletion Flag or Sticky Bit
If we don’t want the files we created to be deleted by others; we can add a special mode bit the so-called sticky bit to the permission of the directory containing those files:
|o (This is optional)||+-=||t|
A file in the flagged directory can only be deleted by the root user, the owner of the directory and the file’s owner.
Making Multiple Changes
We can use commas to separate multiple actions that change permissions:
Linux also gives us another way to change user permissions more quickly. We can provide a range of numbers to represent their corresponding permissions. A complete numeric model has four digits
Special Mode Bits
The first digit represents a special mode bit:
|4000||Set User ID|
|2000||Set Group ID|
|1000||Restricted delete flag or sticky bit|
The Owner of the File
The second digit represents the owner of the file:
|0400||Readable or Viewable to owner|
|0200||Writable or Controllable to owner|
|0100||Executable or Accessible to owner|
Usually only displayed as 400, 200 and 100.
The Group of the File
The third digit represents the group of the file:
|0040||Readable or Viewable to users belonging to the file’s group|
|0020||Writable or Controllable to users belonging to the file’s group|
|0010||Executable or Accessible to users belonging to the file’s group|
Usually only displayed as 40, 20 and 10.
The fourth digit represents for others:
|0004||Readable or Viewable to other users|
|0002||Writable or Controllable to other users|
|0001||Executable or Accessible to other users|
Usually only displayed as 4, 2 and 1.
When used, the preceding zeros are usually removed. Next, we’ll show you how to use most of the mode bits listed above.
Author Dong Chen
LastMod Fri Mar 15 2019