Chmod Linux Command & Examples
The chmod Linux command is used to change the access mode (aka file system permissions) of one or more files (or directories) only the owner or a privileged user may change the mode.
Chmod Command Usage
1
|
|
The mode can be specified by the name or the UID/ GID number, if you do not specify who then the default option of a (all) will be used. You may only use one opcode and multiple modes are separated by commas.
Situations when the chmod command would be used
- To change file permission on Linux / Unix based operating systems
- Set file as read / write / execute on Linux or Unix
- To set the sticky bit in Linux using chmod
- Set group / user file system permissions in Linux
chmod options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Who
- u User
- g Group
- o Other
- a All (the default option)
Opcode
- Add permission
- Remove permission
- = Assign permission (and remove permission of the unspecified fields).
Permission Options for Chmod
- r Read
- w Write
- x Execute
- s Set user (or group) ID
- t Sticky bit – used on directories to prevent removal of files by non-owners, also used to make sure permissions are taken from the parent directory
- u User’s present permission
- g Group’s present permission
- o Other’s present Permission
chmod permission number explained
An alternative option to specifying the above is to use the 3 digit octal number method (e.g 755), remember the following:
- The first chmod number is the owner permission
- The second chmod number is the group permission
- The third chmod number is the other’s permission
What each chmod number means: * 4 = Read * 2 = Write * 1 = Execute
There mat be a fourth number on the end this has the following meaning:
- 4 = Set user ID on execution to grant permissions based on the files owner, not the user who created the process. So for example if you ran a file it would run as the user who created it, not your user
- 2 = Set group ID on execution to grant permissions based on the files owner, not the user who created the process.
- 1 = Set the sticky bit
chmod examples
Below are some real world examples of the chmod command. Add execute permissions to a file (note this will make it executable to everyone as I am not specifying user, group or other):
1
|
|
Add execute permissions to the user for a file:
1
|
|
Set read, write, execute to everyone:
1
|
|
Another way of doing this would be:
1
|
|
chmod set read / write / execute for the user and read / execute for the group and execute only for others:
1
|
|
1
|
|
chmod read / write / execute for the new owner only:
1
|
|
1
|
|
chmod read / write / execute for the owner only:
1
|
|
chmod read / write / execute for the owner & read / execute for the group and other (global):
1
|
|
The following sets the UID to read / write and read, write , execute to the owner and read, execute to the group and other.
Common chmod file system numbers chart
You add the numbers together to get the correct file system permissions, for example 4 is read 2 is write so read / write is 6.
- 700 = chmod read, write, execute for the owner only
- 755 = chmod read, write, execute for the owner – read, execute for group and others
- 644 = chmod read, write by the owner – read only the group and other (world readable)
- 600 = chmod read, write by the owner only