Chmod (change mode) is one of the most frequently used commands in unix
or linux operating system. The chmod command is used to change the file
or directory access permissions. To know about the access permissions of
a file or directory, use the ls -l command as shown below:
$ ls -l sample.sh -rwx-rw-r-- 1 matt deploy 94 Oct 4 03:12 sample.sh
Here in the above example: Use matt has the read, write and execute permissions on the file. Group deploy has read and write permissions. Others have only the read permission.
File and Directory permissions:
There are three different permissions. They are:
- Read (4): Permitted to read the contents of the file. In case of directory, you can view all the files and sub-directories in that directory.
- Write (2): Permitted to write to the file. In case of directory, you can create files and sub-directories.
- Execute (1): Execute the file as a program/shell script. In case of directory, You can enter into that directory.
The syntax of chmod command is
chmod [options] mode filename
The important options are:
-R : recursively change the permissions of a directory. -v : Verbose
Chmod Examples in Linux / Unix:
1. Give read, write and execute permissions to everyone.
Read, write and execute: 4+2+1=7
$ chmod 777 sample.sh
In the above example, you can see that the permissions are specified with a three digit number. The first digit is for user permissions, second is for group and third is for others permission. This type of representation is called octal representation. Alternatively, you can use the symbolic representation to give the permissions.
chmod ugo+rwx sample.sh
We will see more details about the symbolic representation later.
2. Give read permission to user, write permission to group and execute permission to others.
$ chmod 421 sample.sh
3. Recursive permissions to directory
To give read and write permissions to all the users to a directory (including files and subdirectories) use the recursive option -R.
chmod -R 666 /dir
Symbolic Representation of Permissions:
The following symbols are used to represent the users, groups and others:
- u : User
- g : Group
- o : Others a : All (user, group and others)
- r : read
- w : write
- x : execute
- 4: read
- 2: write
- 1:execute
The following symbols represent the permissions grant or revoke:
- + : Additional permissions. Selected permissions are added.
- - : Revoke the permissions. Selected permissions are revoked.
- = : Specific permissions. Only selected permissions are assigned.
Examples:
1. Remove write permission from group
$ chmod g-w sample.sh
This will only removes the write permission for the group.
2. Add new permission execute to others
$ chmod o+x sample.sh
In addition to the existing permissions, this will add execute permission to others.
3. Give only read permissions to the user
$ chmod u=w sample.sh
This will remove the existing permissions to the user and gives only write permission to the user.
No comments:
Post a Comment