WebMasterCampus
WEB DEVELOPER Resources

Linux chmod Command

Learn Linux chmod Command with examples


Linux chmod Command

In Linux, we can use “chmod” command to change file mode bits.

In Linux, the permissions control the actions that can be performed on the file or directory. They either permit, or prevent, a file from being read, modified or, if it is a script or program, executed.

chmod command Syntax

>> chmod [OPTION]... MODE[,MODE]... FILE... 

We can use the -l (long format) option to have ls list the file permissions for files and directories.

>> ls -l

total 23
-rw-r--r-- 1 tariq tariq   42 May 10 11:11 cities1.txt
-rw-r--r-- 1 tariq tariq   41 May 10 11:12 cities2.txt
-rw-r--r-- 1 tariq tariq    0 May 10 17:25 echo
-rw-r--r-- 1 tariq tariq   11 Apr 25 02:47 email_list_1
lrwxrwxrwx 1 tariq tariq   10 Apr 25 02:47 email_list_soft_link -> email_list
-rw-r--r-- 1 tariq tariq   13 Apr 27 01:02 file_copy.txt
drwxr-xr-x 2 tariq tariq 4096 Apr 21 10:44 fruits
-rw-r--r-- 1 tariq tariq   53 May 10 12:28 fruits.txt
-rw-r--r-- 1 tariq tariq   51 May 10 12:27 fruits2.txt
-rw-r--r-- 1 tariq tariq 1080 May  9 15:11 list1.txt
-rw-r--r-- 1 tariq tariq 1702 May  9 16:36 list2.txt

On each line, the first character identifies the type of entry that is being listed. If it is a dash (-) it is a file. If it is the letter d it is a directory.

The next nine characters represent the settings for the three sets of permissions.

  • The first three characters show the permissions for the user who owns the file (user permissions).
  • The middle three characters show the permissions for members of the file’s group (group permissions).
  • The last three characters show the permissions for anyone not in the first two categories (other permissions).

There are three characters in each set of permissions. The characters are indicators for the presence or absence of one of the permissions. They are either a dash (-) or a letter. If the character is a dash, it means that permission is not granted. If the character is an r, w, or an x, that permission has been granted.

The letters represent:

  • r: Read permissions. The file can be opened, and its content viewed.
  • w: Write permissions. The file can be edited, modified, and deleted.
  • x: Execute permissions. If the file is a script or a program, it can be run (executed).

Examples:

  • ––– means no permissions have been granted at all.
  • rwx means full permissions have been granted. The read, write, and execute indicators are all present.

change directory permissions in Linux

chmod Commands to change directory permissions

Command Description
chmod +rwx filename to add read; write and execute permissions.
chmod -rwx directoryname to remove read; write and execute permissions.
chmod +x filename to allow executable permissions.
chmod -wx filename to take out write and executable permissions.

This only changes the permissions for the owner of the file.

chmod +rwx filename

To add read; write and execute permissions.

>> chmod +rwx file_name

chmod -rwx directoryname

To remove read; write and execute permissions.

>> chmod -rwx directory_name

chmod +x filename

To allow executable permissions.

>> chmod +x file_name

chmod -wx filename

To take out write and executable permissions.

>> chmod -wx file_name

chmod Commands to Change Directory Permissions in Linux for the Group Owners and Others

The command for changing directory permissions for group owners is similar, but add a “g” for group or “o” for users:

Command Description
chmod g+w filename group permission to write
chmod g-wx filename group permission remove write and execute
chmod o+w filename group permission to open file
chmod o-rwx foldername group permissoin removal to read; write and execute
chmod ugo+rwx foldername to give read; write; and execute to everyone.
chmod a=r foldername to give only read permission for everyone.

chmod g+w filename

It provides the group permission to write.

>> chown -R name:filename /home/use_rname/directory_name

chmod g-wx filename

It provides the group permission remove write and execute.

>> chmod g-wx filename

chmod o+w filename

It provides the group permission to open file.

>> chmod o+w filename

chmod o-rwx foldername

It provides the Group permissoin removal to read; write and execute.

>> chmod o-rwx foldername

chmod ugo+rwx foldername

It give read; write; and execute to everyone.

>> chmod ugo+rwx foldername

chmod a=r foldername

It give only read permission for everyone.

>> chmod a=r foldername

chmod Commands to Change Groups of Files and Directories

By issuing these commands, you can change groups of files and directories in Linux.

Command Description
chgrp groupname filename Change group name of file
chgrp groupname foldername Change group name of directory

Note that the group must exist before you can assign groups to files and directories.

chown -R Command can combine the group and ownership

The -R stands for recursive, which transfers ownership of all sub directories to the new owner.

>> chown -R name:filename /home/use_rname/directory_name

chown Command to Change Permissions in Numeric Code

You may need to know how to change permissions in numeric code in Linux, so to do this you use numbers instead of “r”, “w”, or “x”.

Permission Description
0 No Permission
1 Execute
2 Write
4 Read

Basically, you add up the numbers depending on the level of permission you want to give.

chown Command Permission numbers

Permission NO Description
0 ---
1 --x
2 -w-
3 -wx
4 r-
5 r-x
6 rw-
7 rwx

chown Command More Examples

Command Description
chmod 777 foldername will give read; write; and execute permissions for everyone.
chmod 700 foldername will give read; write; and execute permissions for the user only.
chmod 327 foldername will give write and execute (3) permission for the user; w (2) for the group; and read; write; and execute for the users.

chmod 777 foldername

>> chmod 777 directory1

chmod 700 foldername

>> chmod 700 directory2

chmod 327 foldername

>> chmod 327 directory3
Created with love and passion.