|
|
Linux Commands: Changing Permissions with chmodHow to Use chmod to update file and directory permissions
The Linux chmod command is used to change the read, write and execute permissions of files and directories and can be absolute or symbolic.
Every Linux file and every Linux directory has its own set of permissions which controls who can do what with them. Each of them has three types of permission:
They can be accessed (according to the permissions) by three types of user:
But all are set by one Linux command - chmod. Viewing File PermissionsThe easiest way to examine a file or directory's permission is by using ls in its long format: $ cd ~/bin
$ ls -l
lrwxrwxrwx 1 bainm users 16 2008-12-05 18:43 cdu -> check_disk_usage
drwxr-xr-x 2 bainm users 4096 2008-12-05 19:34 cdu_data
-rwxrwxrwx 1 bainm users 84 2008-12-05 15:19 check_disk_usage
The first column shows the file permissions and is actually divided into 10 fields (with any empty fields set to -):
Each group of three fields (for owner, group and others) have the same structure:
Therefore, the file check_disk_usage has a very dangerous set of file permissions - it can be written to by anybody logged on to the system (not the best idea for an executable file in a bin directory); however, the file permissions can be changed by using chmod either of two ways:
Setting the Permission SymbolsWhen setting the permission symbols a combination of different symbols are used; to start with there are symbols for the owner, group and others:
Then a symbol to add or remove a permission:
And finally the symbols for the permissions themselves:
For example: $ chmod u+rwx check_disk_usage
$ chmod g+r-w+x check_disk_usage
$ chmod o-r-w-x check_disk_usage
$ ls -l check_disk_usage
-rwxr-x--- 1 bainm users 84 2008-12-05 15:19 check_disk_usage
Now:
Setting the Absolute PermissionThe absolute permission is an octal translation of the binary representation of the permissions a file - that sounds complicated, but is made more obvious by viewing a complete set of permissions: O Bin rwx
---------
0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx
Unlike the symbolic permissions all three permissions have to be set at the same time, for example: $ chmod 777 check_disk_usage
$ ls -l check_disk_usage
-rwxrwxrwx 1 bainm bainm 84 2008-12-05 15:19 check_disk_usage
$ chmod 750 check_disk_usage
$ ls -l check_disk_usage
-rwxr-x--- 1 bainm bainm 84 2008-12-05 15:19 check_disk_usage
SummaryA Linux user uses the chmod command to change the permissions of any of their files or directories; chmod has two modes:
In both cases the form chmod [symbolic or absolute permission] [file or directory] is used.
The copyright of the article Linux Commands: Changing Permissions with chmod in Linux Programming is owned by Mark Alexander Bain. Permission to republish Linux Commands: Changing Permissions with chmod in print or online must be granted by the author in writing.
Comments
Jun 25, 2009 2:20 PM
Guest :
1 Comment:
|
|
|
|
|
|
|
|