This is basically based on the concept of ownerships and permissions for files & directories. The purpose of implementing permissions on files and directories is to restrict the read, write & execute access to a file or directory from unauthorized user access. So that we can provide a kind of security on files & directories.
Permissions on Unix-like operating systems are managed in three distinct classes. These classes are known as user, group, and others.
Permission Classes:
User (u) - The owner of the file.
Group (g) - Other users belongs to the group. The owner may be a member of the file's group.
Others (o)- Users who are not the owner, nor a member of the group, considered as a file's others class.
Permission Types:
Read (r) - The read permission grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory (but not to find out any further information about them such as contents, file type, size, ownership, permissions, etc.)
Write (w) - The write permission grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
Execute (x) - The execute permission grants the ability to execute a file. This permission must be set for executable binaries (e.g. a compiled C program) or shell scripts in order to allow the operating system to run them. When set for a directory, this permission grants the ability to access file contents and metainfo if its name is known, but not list files inside the directory (unless read is set).
How to check permission ?
Just issue ls -l command to a file or directory, you will be able to see the permission associated with that file.
# ls -l test.txt
-rwxr-xr-x. 1 subrat sysadmin 2225 May 7 14:32 test.txt
Look at the first field of the output. This field is the permission associated with the file or directory. Just separate it as below, you will be able to understand.
-rwxr-xr-x
- rwx r-x r-x
| | | |
File Type User Permission Group Permission Others Permission
File Types -
- Regular File
d Directory File
l Linked File
In similar way, for directory issue ls -ld command to a directory.
# ls -ld test
drwxr-xr-x. 2 subrat sysadmin 12288 May 12 11:44 test
In the above example, for user, there is read, write & execute permission, for group, there is read & execute permission, and for others, there is read & execute permission.
Octal representation of Permissions -
The following octal numbers are being used for representing permission.
r -- 4
w -- 2
x -- 1
For e.g. You want to set permission as follows,
For user as rwx (read, write, execute)
User --- 4+2+1 --- 7
For group as r-x (read, execute)
Group --- 4+0+1 --- 5
For others as r-- (read)
Others --- 4+0+0 --- 4
So, the effective permission in octal will be 754.
Now we will learn how to change the permissions, ownership, group of a file or directory.
chmod
The chmod command is used to change the access permission to a file or directory. It can be done by the owner of the file or by the user having admin privilege.
It can be done in two different ways, Normal way & Numeric way.
In Normal way, we have use the following different options,
u=user, g=group, o=other, a=all (default)
In Numeric way, we will use the octal representation for effective permission.
r=4, w=2, x=1
e.g. rwx = 4+2+1 = 7
rw- = 4+2+0 = 6
r-x = 4+0+1 = 5
-wx = 0+2+1 = 3
r-- = 4+0+0 = 4
-w- = 0+2+0 = 2
--x = 0+0+1 = 1
--- = 0+0+0 = 0
Note: Using -wx option is of no meaning as because if a user do not have read permission, then what is the meaning of giving him write & execute permission.
Also we need to use the following symbols for adding & revoking access permission,
+ means add permission
- means remove permission
= means assign permission and remove the permission of unspecified fields
Here we go...
- Adding permissions for user/owner to file or directory.
# chmod u+r file.txt
# chmod u+w,u+x file.txt
# chmod u+r,u+w,u+x dir1
- Adding permissions for group and others to a file or directory.
# chmod g+r,o+r file.txt
# chmod g+w,o+x dir1
- Adding multiple permissions in a single line.
# chmod u+rwx,g+rx,o+x file.txt
# chmod a+r,u+wx,g+x dir1
# chmod u+rwx,go+r dir2
# chmod ug+rwx,o+rx dir3
# chmod ugo+rwx dir4
- Revoking permissions from user, group & others.
# chmod u-w,g-x file.txt
# chmod a-x,o-rw file.txt
# chmod g-w,o-rwx dir1
# chmod go-wx file.txt
The above all examples will add or remove the read, write & execute permissions to or from the existing permissions respectively of a file or directory. That means with the existing permissions, the operations will be done on file or directory accordingly & leave all other privileges as it is.
There is also another way, it won't look for the existing permission. It will simply overwrite the new permission you want to apply to a file or directory. This can be done with '=' operator.
# chmod ug=rwx,o=rx file1
# ls -l file1
-rwxrwxr-x. 1 subrat sysadmin 73740 Apr 9 06:42 file1
# chmod a=rwx file1
# ls -l file1
-rwxrwxrwx. 1 subrat sysadmin 73740 Apr 9 06:42 file1
This also can be done using the numeric way as follows,
# chmod 777 file2
# ls -l file2
-rwxrwxrwx. 1 subrat subrat 3324 Apr 9 06:43 file2
# chmod 755 scripts/
# ls -ld scripts/
drwxr-xr-x. 2 root root 4096 May 12 12:54 scripts/
Note: For directory at least provide the execute permission if you want others to enter to your directory.
- Apply permission to multiple files or directory in a single line.
# chmod 751 file1 file2 file3
# ls -l file1 file2 file3
-rwxr-x--x. 1 subrat sysadmin 73740 Apr 9 06:42 file1
-rwxr-x--x. 1 subrat sysadmin 3324 Apr 9 06:43 file2
-rwxr-x--x. 1 root root 3744 Apr 10 09:40 file3
- To provide the recursive privileges for the directory, sub-directories & the files.
# chmod -R 755 scripts/