Linux Change File Permission - Chmod Command

September 2, 2013 | By
| Reply More

The Linux security starts with file permissions. On the very basic level, file and directory permissions play a vital role in security of a system. The file permissions are applied on three levels: the owner, group members and others. The chmod command is used to change these permissions. This article discusses how these file permissions can be changed with chmod command.

File permissions

As stated earlier, the file permissions are on three levels: Owner of file (u), members of the group owner of file (g) and others (o). Moreover, there are three permissions: Read (r), Write (w) and Execute (x). For each of the levels, each permission is applied, so 9 characters (3x3) are available for these permissions. The detailed listing of ls command (-l option) lists these permissions:

$ ls -l file1
-rw-rw-r-- 1 raghu raghu 0 Jul 26 09:31 file1

The last 9 characters out of first 10 characters of the output show the permissions. The first three characters are for file owner's permissions, next three for group members' and the last three, for others' permissions. The permissions which are not present are marked as dash (-). In this output, the owner has read and write permission (no execute), group members can also read and write (but not execute), others can only read (write and execute permissions are not present).

Changing individual permissions

The chmod command takes two arguments, permissions, and the file name to which the permissions are to be applied. Let us add execute permission to the owner.

$ chmod u+x file1

$ ls -l file1
-rwxrw-r-- 1 raghu raghu 0 Jul 26 09:31 file1

The permissions for owner will be changed with 'u', group members with 'g' and others with 'o'. To add a permission, we use '+' sign, to remove, '-' sign and to overwrite, '=' sign. Now, let's revoke write permission from group.

$ chmod g-w file1

$ ls -l file1
-rwxr--r-- 1 raghu raghu 0 Jul 26 09:31 file1

Now, we use '=' sign to change others' permissions.

$ ls -l file1
-rwxr--r-x 1 raghu raghu 0 Jul 26 09:31 file1

Changing Multiple Level Permissions

The permissions for more than one level can also be changed. For example, here we change owner and group's permissions together:

$ chmod ug=w file1

$ ls -l file1
--w--w-r-x 1 raghu raghu 0 Jul 26 09:31 file1

Similary, other combinations are also possible. If on all the levels, permissions are to be changed simultaneously, we can use 'a'. For example, in following command, first we remove all the permissions and then we add read permission on all levels.

$ chmod rwx=- file1

$ ls -l file1
---------- 1 raghu raghu 0 Jul 26 09:31 file1

$ chmod a=r file1

$ ls -l file1
-r--r--r-- 1 raghu raghu 0 Jul 26 09:31 file1

The Numeric Method

The permissions can also be changed with 'numeric' method. Each permission is represented by a number:

r w x
4 2 1

And these permissions are added to form the combination of permissions. For example, 1 is equivalent to execute permission, 3 (2+1) means write and execute, 6 (4+2) means read and write, and 7 (4+2+1) means all permissions (rwx). For three levels, three digits are used. In numeric method, all the permissions are changed at once. For illustration:

$ chmod 755 file1

$ ls -l file1
-rwxr-xr-x 1 raghu raghu 0 Jul 26 09:31 file1

$ chmod 644 file1

$ ls -l file1
-rw-r--r-- 1 raghu raghu 0 Jul 26 09:31 file1

Options to chmod Command

There are not many options available for this command, but a few of them are very useful.

Changing directory permissions

To change permissions of a directory recursively, -R option is used. For example, we remove all permissions for others for all files (and the directory itself) in mydir directory.

$ ls -l mydir/
total 124
-rw-rw-r-- 1 raghu raghu 10240 Nov 7 2012 textfile1
-rw-rw-r-- 1 raghu raghu 102400 Nov 7 2012 textfile2

$ ls -ld mydir/
drwxrwxr-x 2 raghu raghu 4096 Nov 7 2012 mydir/

$ chmod -R o=- mydir/

$ ls -l mydir/
total 124
-rw-rw---- 1 raghu raghu 10240 Nov 7 2012 textfile1
-rw-rw---- 1 raghu raghu 102400 Nov 7 2012 textfile2

$ ls -ld mydir/
drwxrwx--- 2 raghu raghu 4096 Nov 7 2012 mydir/

Verbose output

The detailed output can be printed with -v or --verbose option. This option prints the files as they are processed.

$ chmod -v -R o=r mydir
mode of `mydir' changed to 0774 (rwxrwxr--)
mode of `mydir/textfile1' changed to 0664 (rw-rw-r--)
mode of `mydir/textfile2' changed to 0664 (rw-rw-r--)

A similar option is -c which works like verbose option but prints only when a change is made. For example,

$ chmod -c o+r textfile1
$ chmod -c o+rw textfile1

mode of `textfile1' changed to 0666 (rw-rw-rw-)

The errors can be suppressed if you like. If you do not want to print the error messages, you can use --silent, --quiet or -f option.

$ chmod o+w /etc/
chmod: changing permissions of `/etc/': Operation not permitted

$ chmod -f o+w /etc/
$

Help and version number

Of course, as with all commands, chmod has --help option to print the brief help about the command and the --version option to output the version of command as well as the author.

$ chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
or: chmod [OPTION]... OCTAL-MODE FILE...
or: chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.

-c, --changes like verbose but report only when a change is made
--no-preserve-root do not treat `/' specially (the default)
--preserve-root fail to operate recursively on `/'
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--reference=RFILE use RFILE's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display this help and exit
--version output version information and exit

Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.

Report bugs to .

$ chmod --version
chmod (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.

Filed Under : LINUX COMMANDS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.