Chmod

From Unix SME

Chmod

chmod - change file mode bits


Description

The chmod (short for change mode) command is used to manage file system access permissions on Unix and Unix-like systems. There are three basic file system permissions, or modes, to files and directories: read (r) write (w) execute (x)



chmod Who/What/Which file|directory


Who Set Description

    u	- user	- The file owner
    g	- group	- Member of the file's group
    o	- other	- Users who are not the file owner nor members of the file's group
    a	- all	- All the three previous groups


What - Operation - Description

+ - add - Adds the permissions to the file

- - remove - Removes the permissions to the file

= - set - Sets exactly the provided permissions to the file


Which - Mode - Description

r - read - Read access to the file. Listing access to the directory

w - write - Write permissions to the file or directory

x - execute - Execute permissions to the file. Allows entering the directory, and accessing files and subdirectories inside the directory

X - special execute Execute permissions to a directory, or execute permissions to a file if at least one of the execute bits is set


Examples

Remove read and write permission for group and other on the document.pdf file:

[user@host ~]$ chmod go-rw document.pdf


Add execute permission for everyone on the myscript.sh file:

[user@host ~]$ chmod a+x myscript.sh


Removes execute permission for others on the exfile.txt:

[user@host ~]$ chmod o-x exfile.txt


Octal Method

You can use the chmod command to change file permissions with the octal method instead of the symbolic method. In the following example, the # character represents a digit.

chmod ### file|directory

With the octal method, you can represent permissions as a 3-digit (or 4-digit, when setting advanced permissions) octal number. A single octal digit can represent any single value from 0-7.

In the 3-digit octal representation of permissions, each digit stands for one access level, from left to right: user, group, and other. To determine each digit:

Start with 0.

- To add read permissions for this access level, add 4.

- To add write permissions, add 2.

- To add execute permissions, add 1.


Examples

Set read and write permissions for user, and read permission for group and other, on the sample.txt file:

1.) [user@host ~]$ chmod 644 sample.txt 2.) [user@host ~]$ chmod 777 sample.txt 3.) [user@host ~]$ chmod 000 sample.txt 4.) [user@host ~]$ chmod 764 sample.txt