chmod — CHange file access permission MODes
chmod is used to change the permission of the file(s) or directories. It is an important command that every Unix user must know. It is responsible for the security and protection for the various files.But first we must know what is access permissions.
If you type
ls -i, you would get a listing of the current directory along with the access permissions. That is who is the owner of the file, what kind of permissions the user, group, others have, etc.
Here is an example listing
-rwxr-xr-- 1 foobar bar 10 Feb 14 15:00 myfile
The first field denotes whether the entry is a directory or a file or a link, etc. If it is a – then it is a file. If it is a l then it is a link. If it is a d then it is a directory. There are more under these, but these are the important ones.
The access permissions can be divided for owner(u), group(g) and eveyone else(o).
Each has three fields and are
- r – Permission to read the file. In case of a directory, it is the permission to list the directory
- w – Permission to write to the file. For a directory, it is permissions to create/remove files in that directory
- e – Executable permissions for the file. For a directory, it means permissions to access files in the directory.
Internally the permissions are stored a numbers. That is
- 1 – x – Execute
- 2 – w – Write
- 4 – r – Read
You can combine these numbers to give multiple permissions using a single command.
- 3 (2+1) – wx – Write and execute
- 5 (1+4) – rx – Read and Execute
- 6 (4+2) – rw – Read and Write
- 7 (1+2+4) – rwx – Read, Write and execute
You specify these numbers for all the three categories of users – Owner, group, others.
$ chmod 777 myfile — All permission to all. Here permissions are given in numerical format.
$ chmod 755 f1 f2 — setting all permissions to owner and read & Execute permissions to group and others.
$ chmod u=rwx,g=rx,o=rx myprg — Same as above. But here permissions are given in symbolic format.
$ chmod u+x,g+r,o-rwx myprg — Add Exec permission to owner, Add Read permission to group and Remove all permission for others.
$ chmod u-x,go-rx myfile — Remove Exec permission for owner and Remove Read & Exec permissions for group & others.
$ chmod -R 755 mydir — Set the permission for files & Dirs recursively.
$ chmod –reference=f1 f2 — Set f1’s mode to f2.
$ chmod -c 755 myfile — Verbose but report only when a change is made.
$ chmod -v 754 myfile — Verbose output for every file processed.
Note: `chmod’ never changes the permissions of symbolic links.
Read : man chmod / info chmod