chmod

Chmod Command in Linux File Permissions

In Linux, access to the files is managed through the file permissions, attributes, and ownership. This ensures that only authorized users and processes can access files and directories.

This tutorial covers how to use the chmod command to change the access permissions of files and directories.

Before going further, let’s explain the basic Linux permissions model.

In Linux, each file is associated with an owner and a group and assigned with permission access rights for three different classes of users:

  • The file owner.
  • The group members.
  • Others (everybody else).

File ownership can be changed using the chown and chgrp commands.

There are three file permissions types that apply to each class:

  • The read permission.
  • The write permission.
  • The execute permission.

This concept allows you to specify which users are allowed to read the file, write to the file, or execute the file.

File permissions can be viewed using the ls command:

ls -l filename.txt

Copy

-rw-r--r-- 12 linuxize users 12.0K Apr  8 20:51 filename.txt
|[-][-][-]-   [------] [---]
| |  |  | |      |       |
| |  |  | |      |       +-----------> 7. Group
| |  |  | |      +-------------------> 6. Owner
| |  |  | +--------------------------> 5. Alternate Access Method
| |  |  +----------------------------> 4. Others Permissions
| |  +-------------------------------> 3. Group Permissions
| +----------------------------------> 2. Owner Permissions
+------------------------------------> 1. File Type

Copy

The first character shows the file type. It can be a regular file (-), directory (d), a symbolic link (l), or any other special type of file.

The next nine characters represent the file permissions, three triplets of three characters each. The first triplet shows the owner permissions, the second one group permissions, and the last triplet shows everybody else permissions. The permissions can have a different meaning depending on the file type.

In the example above (rw-r--r--) means that the file owner has read and write permissions (rw-), the group and others have only read permissions (r--).

Each of the three permission triplets can be constructed of the following characters and have a different effects, depending on whether they are set to a file or to a directory:

Effect of Permissions on Files

Permission Character Meaning on File
Read - The file is not readable. You cannot view the file contents.
r The file is readable.
Write - The file cannot be changed or modified.
w The file can be changed or modified.
Execute - The file cannot be executed.
x The file can be executed.
s If found in the user triplet it sets the setuid bit. If found in the group triplet, it sets the setgid bit. It also means that x flag is set.
When the setuid or setgid flags are set on an executable file, the file is executed with the file’s owner and/or group privileges.
S Same as s but the x flag is not set. This flag is rarely used on files.
t If found in the others triplet it sets the sticky bit.
It also means that x flag is set. This flag is useless on files.
T Same as t but the x flag is not set. This flag is useless on files.

Effect of Permissions on Directories (Folders)

In Linux, Directories are special types of files that contain other files and directories.

Permission Character Meaning on Directory
Read - The directory’s contents cannot be shown.
r The directory’s contents can be shown.
(e.g. You can list files inside the directory with ls.)
Write - The directory’s contents cannot be altered.
w The directory’s contents can be altered.
(e.g. You cannot create new files, delete files ..etc.)
Execute - The directory cannot be changed to.
x The directory can be navigated using cd.
s If found in the user triplet, it sets the setuid bit. If found in the group triplet it sets the setgid bit. It also means that x flag is set. When the setgid flag is set on a directory the new files created within it inherits the directory group ID (GID), instead of the the primary group ID of the user who created the file.
setuid has no effect on directories.
S Same as s but the x flag is not set. This flag is useless on directories.
t If found in the others triplet it sets the sticky bit.
It also means that x flag is set. When the sticky bit is set on a directory, only the file’s owner, the directory’s owner, or administrative user can delete or rename the files within the directory.
T Same as t but the x flag is not set. This flag is useless on directories.

The general form of the chmod command take the following form:

Read More here

 

Linux File Permissions, Linux File Permissions All Users, Linux File Permissions Command, Linux File Permissions Examples

Chmod Codes, Chmod In Linux