Chmod linux command example - Linux permissions Introduction
Chmod linux command example - Linux permission tutorial includes how to use linux Chmod command to change file permissions and change directory permissions in Linux. Chmod command was first appeared in AT&T Unix.
To Change file and directories permissions according to the scenario is very vital. It even becomes more important if you are working as a system administrator because you cannot afford to take the risk of assigning wrong permission to files and directories. Any wrong permission makes your system vulnerable to intruder attack.
Below are given in details the roles, permissions and the numerical representation of chmod command.
There are three main chmod options you can use for files and directories in any Linux or Unix system. These options are u, g, o. you can also give different linux permissions for these three options. For example chmod read write or chmod execute permission. We will learn about it in details.
u = user g = group o = others
Three chmod permissions are read, write and execute . you can use alternatively chmod read for read, chmod write for write and chmod execute for execute permission. See the example given below.
# chmod +r <file name> # chmod +w <file name> # chmod +x <file name>
r = read w = write x = execute
Chmod permissions can also be represented in numeric form. Each read, write and execute permission are assigned with a unique number. Please see below the numbers associated the r, w and x permission.
r = 4 w = 2 x = 1
You can given permissions to users, groups and others individually. For example you may want to add write permission only to users. The following example will illustrate in details.
# chmod u+w file1 # chmod g+w file1 # chmod o+w file1
Similarly if you are willing to change file permissions and assign different permissions to user, group and others at a time, you can use comma(,) between them. In the below example read permission will be given to user, write permission to group, execute permission to others.
# chmod u+r,g+w,o+x file1
Chmod all permission
In some case you may want to keep the same permission for all (user, group,others). use the option 'a' for that. In the below given example read, write permission will be given to all for file1.
# chmod a+rw file1
Chmod reference option
You can also copy the permissions of a file to a new file, its called taking a reference from a file. The option "--reference" is used to copy a file permission to another. In the given example file2 is used as a reference.
# chmod --reference=file2 file1
Chmod recursive permission
Use option "-R" to recursively change permission of directory and files and its sub-directory.
# chmod -R u+rw directory/
Chmod remove permission
Chmod remove permission can be used to remove an already pre-defined to a file. Use the option "-" . The example is given below.
# chmod g-rw file
You can also remove multiple permissions at a time from different roles by using comma( ,) in the between.
# chmod u-r,g-x,o-w file1
Chmod numbers to change file and directory permissions
As the numbers are associated with permissions, its become very easy when you want to change combinations of permissions in a file. For example read,write,execute for user, group and others becomes chmod 777. Similarly, 'rwx' for users and 'rx' for group and others becomes chmod 755. Refer the example given below.
# chmod 777 file1 # chmod 755 file1 # chmod 431 file1 # chmod 512 file1
Change directory permission only
Consider you are having multiple files and sub-directories inside a directory and you want to apply execute permissions only to its directories and sub-directories without having any affect on files, use capital 'X' for that.
# chmod u+X directory/ # chmod g+X directory/ # chmod u+X * directory/
See the permissions of files and directories
After you make the necessary changes to your files and directories. you also need to confirm if the permissions are set or not. use command "ls" with option "-l".
# ls -l file1 -rw-r--r--. 1 root root 0 Nov 23 23:00 file1
For more info use man chmod
For more information on how to use chmod command use 'man'.
# man chmod
This is it with the article Chmod linux command example - Linux permissions.
Also see article on UNIX sed command
Image credit:Andrés Álvarez Iglesias of flickr