chmod command in Linux with examples

19 Feb, 2021
In Unix-like operating systems, the chmod command is used to change the access mode of a file.The name is an abbreviation of change mode.
Syntax :
chmod [reference][operator][mode] file... 

The references are used to distinguish the users to whom the permissions apply i.e. they are list of letters that specifies whom to give permissions. The references are represented by one or more of the following letters:
Reference   Class     Description
u          owner      file's owner

g          group      users who are members of
                      the file's group

o          others     users who are neither the
                      file's owner nor members of 
                      the file's group

a          all       All three of the above, same as ugo

The operator is used to specify how the modes of a file should be adjusted. The following operators are accepted:
Operator  Description
+         Adds the specified modes to the
          specified classes

-         Removes the specified modes from 
          the specified classes

=         The modes specified are to be made
          the exact modes for the specified 
          classes

Note : Putting blank space(s) around operator would make the command fail.
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
The modes indicate which permissions are to be granted or removed from the specified classes. There are three basic modes which correspond to the basic permissions:
r       Permission to read the file.
w       Permission to write (or delete) the file.
x       Permission to execute the file, or, in
        the case of a directory, search it.

Types of permissions which we will be changing using chmod command :In linux terminal, to see all the permissions to different files, type ls -l command which lists the files in the working directory in long format. The figure below shows an example to use ls -l and its output :
Let us take a look at above figure. To make things easy to understand some columns and rows are eliminated and extra spaces are added to the permissions column to make it easier to read as shown below:
- rw- rw- r--  mik  mik    assgn1_client.c
- rw- rw- r--  mik  mik    assgn1_server.c
d rwx rwx r-x  mik  mik    EXAM
- rw- rw- r--  mik  mik    raw.c
- rwx r-x r-x  mik  mik    header.sh
... so on...

The very first column represents the type of the file i.e. is it a normal file or adirectory where d represents a directory and – represents a normal file.
The first set three letters after the file type tell what the Owner of the file, have permissions to do. For example: In assgn1_client.c, has owner’s permission as rw-, which means the owner mik can only read(r) and write(w) the file but cannot execute(x).
Note: The 3rd and 4th columns represents the name of the owner of the file and the group to which the owner belongs respectively.
The next three letters after the user’s permission are the group’s permissions.For example: header.sh has group permissions as r-x, which means Other people in the mik group can not write(w) the header.sh script but can only read(r) or execute(x) it.
Note that when a directory has the x set, this takes the special meaning of “permitted to search this directory”.
The last three letters in the permissions column tell us what the “others” may do. The general practice is to protect the files from external access so that others can’t write any files or directories. They may read(r) or execute(x) it. For example: The assgn1_client.c has others permission as r- – which means it can only be read by other(external) access but cannot be written or executed by them.
Now, let us see how chmod command can be used to change the access mode of a file.
Example 1 :Let’s change the assgn1_client.c permission so that the owner cannot write(w) in the file but can only read it.
BEFORE: -rw-rw-r--  mik  mik  assgn1_client.c

COMMAND: chmod u=r assgn1_client.c

AFTER: -r--rw-r--  mik   mik   assgn1_client.c

Before :
After :
Example 2 :Let’s restrict the permission such that the user cannot search the directory EXAM.
BEFORE: drwxrwxr-x  mik   mik   EXAM

COMMAND: chmod u=rw EXAM

AFTER: drw-rwxr-x   mik  mik    EXAM

After applying the chmod u=rw EXAM command, the user (owner) cannot change the directory. If the user tries to change the directory, then it shows the message “Permission denied” as shown in the figure below :Linux Tutorials | Changing Permissions | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersLinux Tutorials | Changing Permissions | GeeksforGeeksWatch laterShareCopy link33/36InfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 7:57•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=RWReI80utd4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>Reference :chmod Wikipedia
linux-command
Linux-directory-commands
Linux-file-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
tar command in Linux with examples
Conditional Statements | Shell Script
'crontab' in Linux with Examples
Tail command in Linux with examples
Docker - COPY Instruction
scp command in Linux with Examples
UDP Server-Client implementation in C
Cat command in Linux with examples
echo command in Linux with Examples
touch command in Linux with Examples