Saturday, 20 August 2011

permissions and chmod

To change file or directory permissions, you use the chmod (change mode) command. There are two ways to use chmod: symbolic mode and absolute mode.

Using chmod in Symbolic Mode:                             

The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

Chmod operatorDescription
+Adds the designated permission(s) to a file or directory.
-Removes the designated permission(s) from a file or directory.
=Sets the designated permission(s).


Here's an example using testfile. Running ls -1 on testfile shows that the file's permissions are as follows:
[amrood]$ls -l testfile
-rwxrwxr--  1 amrood   users 1024  Nov 2 00:10  testfile



Then each example chmod command from the preceding table is run on testfile, followed by ls -l so you can see the permission changes:
[amrood]$chmod o+wx testfile
[amrood]$ls -l testfile
-rwxrwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
[amrood]$chmod u-x testfile
[amrood]$ls -l testfile
-rw-rwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
[amrood]$chmod g=r-x testfile
[amrood]$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile


Here's how you could combine these commands on a single line:
[amrood]$chmod o+wx,u-x,g=r-x testfile
[amrood]$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile



Using chmod with Absolute Permissions: (1,2,4)          

The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.
Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.
NumberOctal Permission RepresentationRef
0No permission---
1Execute permission--x
2Write permission-w-
3Execute and write permission: 1 (execute) + 2 (write) = 3-wx
4Read permissionr--
5Read and execute permission: 4 (read) + 1 (execute) = 5r-x
6Read and write permission: 4 (read) + 2 (write) = 6rw-
7All permissions: 4 (read) + 2 (write) + 1 (execute) = 7rwx

Here's an example using testfile. Running ls -1 on testfile shows that the file's permissions are as follows:
[amrood]$ls -l testfile
-rwxrwxr--  1 amrood   users 1024  Nov 2 00:10  testfile

Then each example chmod command from the preceding table is run on testfile, followed by ls -l so you can see the permission changes:
[amrood]$ chmod 755 testfile
[amrood]$ls -l testfile
-rwxr-xr-x  1 amrood   users 1024  Nov 2 00:10  testfile
[amrood]$chmod 743 testfile
[amrood]$ls -l testfile
-rwxr---wx  1 amrood   users 1024  Nov 2 00:10  testfile
[amrood]$chmod 043 testfile
[amrood]$ls -l testfile
----r---wx  1 amrood   users 1024  Nov 2 00:10  testfile

No comments:

Post a Comment