The сhmod (change mode) command changes the access mode of files and directories.
Syntax of the chmod command is the following:
$ chmod [options] permissions file[s]
- -R, –recursive – Change files and directories recursively;
- -f, –silent, –quiet – Suppress most error messages.
View the current file / directory mode using ls command:
$ ls -l MyFile.txt -rw-r--r-- 1 john admin 0 2012-12-02 04:30 MyFile.txt $ ls -ld MyDir drwxr-xr-x 2 john admin 4096 2012-12-02 04:29 MyDir
or using stat command:
$ stat -c '%A %a %n' MyFile.txt -rw-r--r-- 644 MyFile.txt $ stat -c '%A %a %n' MyDir drwxr-xr-x 755 MyDir
The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to “all”. The references are represented by one or more of the following letters:
|u||user||the owner of the file (folder)|
|g||group||users who are members of the file’s (folders’s) group|
|o||others||users who are not the owner of the file (folder) or members of the group|
|a||all||all three of the above, is the same as ugo|
The chmod program uses an operator to specify how the modes of a file (folder) should be adjusted. The following operators are accepted:
|+||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|
The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions:
|r||read||permitted to read the contents of file or directory (view files and sub-directories in that directory)|
|w||write||permitted to write to the file or in to the directory (create files and sub-directories in that directory)|
|x||execute||permitted to execute the file as a program/script or enter into that directory|
|X||special execute||applies execute permissions to directories regardless of their current permissions and applies execute permissions to a file which already has at least 1 execute permission bit already set (either user, group or other).|
[X] Is not a permission in itself but rather can be used instead of x. It is only really useful when used with ‘+’ and usually in combination with the -R option for giving group or other access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used ‘chmod -R a+rx’, whereas with ‘X’ you can do ‘chmod -R a+rX’ instead.
You can use either the octal representation or symbolic representation to change the permission of a file or directory.
Octal representation for permissions:
- First number is for user
- Second number is for group
- Third number is for others
|7||rwx||read, write, execute|
10 Simple Examples:
1. Read permission is added for all:
$ chmod a+r file
2. Execute permission is removed for all:
$ chmod a-x file
3. Change the permissions of the file to read and write for all:
$ chmod a+rw file
4. Read and write permissions are set for the owner, all permissions are cleared for the group and others:
$ chmod u=rw,go= file
5. Change the permissions of the directory and all its contents to add write access for the user, and deny write access for everybody else:
$ chmod -R u+w,go-w directory
6. Removes all privileges for all:
$ chmod file
7. Change the permissions of the file to read, write, and execute for all:
$ chmod 777 file
8. Sets read, write and no execution access for the owner and group, read only for all others:
$ chmod 664 file
9. Set a directory tree to ‘-rwx’ for owner directories, ‘-rw’ for owner files, ‘—‘ for group and others:
$ chmod -R u+rwX,g-rwx,o-rwx directory
10. Remove the execute permission on all files in a directory tree, while allowing for directory browsing:
$ chmod -R a-x+X directory
This is a must-read for anyone who wants to understand, explain and implement DevOps culture, process and tools for high performance development and operations.
All DevOps practices in a single place - organisational, cultural and technical. Lots of exceptional case studies.
Increase profitability, elevate work culture, and exceed productivity goals through DevOps practices.