I hope you have understand file ownership and permissions from my previous post Understanding File Permissions Linux. In this post you will learn how to set file permissions in linux.
Setting File Permissions in Linux – Symbolic mode:
You can set file permissions with theÃ‚Â
chmodÃ‚Â command. Both the root user and the file’s owner can set file permissions.Ã‚Â
chmodÃ‚Â has two modes, symbolic and numeric.
The symbolic mode is pretty easy to remember. First, you decide if you set permissions for the user (u), the group (g), others (o), or all of the three (a). Then, you either add a permission (+), remove it (-), or wipe out the previous permissions and add a new one (=). Next, you decide if you set the read permission (r), write permission (w), or execute permission (x). Last, you’ll tellÃ‚Â
chmodÃ‚Â which file’s permissions you want to change.
Let’s have a couple of examples. Suppose we have a regular file calledÃ‚Â
testfile, and the file has full access permissions for all the groups (long directory listing would show
-rwxrwxrwxÃ‚Â as the file’s permissions).
Wipe out all the permissions but add read permission for everybody:
$Ã‚Â chmod a=r testfile
After the command, the file’s permissions would beÃ‚Â
Add execute permissions for group:
$Ã‚Â chmod g+x testfile
Now, the file’s permissions would beÃ‚Â
Add both write and execute permissions for the file’s owner. Note how you can set more than one permission at the same time:
$Ã‚Â chmod u+wx testfile
After this, the file permissions will beÃ‚Â
Remove the execute permission from both the file’s owner and group. Note, again, how you can set them both at once:
$Ã‚Â chmod ug-x testfile
Now, the permissions areÃ‚Â
As a summary, have a look at this quick reference for setting file permissions in symbolic mode:
|What to do?|
|+||add this permission|
|–||remove this permission|
|=||set exactly this permission|
Setting File Permissions in Linux – Numeric mode:
The other mode in whichÃ‚Â
chmodÃ‚Â can be used is the numeric mode. In the numeric mode, the file permissions aren’t represented by characters. Instead, they are represented by a three-digit octal number.
4Ã‚Â = read (r)
2Ã‚Â = write (w)
1Ã‚Â = execute (x)
0Ã‚Â = no permission (-)
To get the permission bits you want, you add up the numbers accordingly. For example, the rwx permissions would be 4+2+1=7, rx would be 4+1=5, and rw would be 4+2=6. Because you set separate permissions for the owner, group, and others, you’ll need a three-digit number representing the permissions of all these groups.
Let’s have an example.
$Ã‚Â chmod 755 testfile
This would change theÃ‚Â
testfile‘s permissions toÃ‚Â
-rwxr-xr-x. The owner would have full read, write, and execute permissions (7=4+2+1), the group would have read and execute permissions (5=4+1), and the world would have the read and execute permissions as well.
Let’s have another example:
$Ã‚Â chmod 640 testfile
In this case,Ã‚Â
testfile‘s permissions would beÃ‚Â
-rw-r-----. The owner would have read and write permissions (6=4+2), the group would have read permissions only (4), and the others wouldn’t have any access permissions (0).
The numeric mode may not be as straightforward as the symbolic mode, but with the numeric mode, you can more quickly and efficiently set the file permissions. This quick reference for setting file permissions in numeric mode might help:
This content is copied from http://www.tuxfiles.org/linuxhelp/filepermissions.html