Tuesday, November 27, 2012

Basic file permissions



Basic file permissions
Every file on your Linux system, including directories, is owned by a specific user and group. Therefore, file permissions are defined separately for users, groups, and others.
User: The username of the person who owns the file. By default, the user who creates the file will become its owner.
Group: The usergroup that owns the file. All users who belong into the group that owns the file will have the same access permissions to the file.
Other: A user who isn't the owner of the file and doesn't belong in the same group the file does. In other words, if you set a permission for the "other" category, it will affect everyone else by default.
Types of access permissions
There are three types of access permissions on Linux: read, write, and execute. These permissions are defined separately for the file's owner, group and all other users.
Read permission: On a regular file, the read permission bit means the file can be opened and read. On a directory, the read permission means you can list the contents of the directory.
Write permission: On a regular file, this means you can modify the file or write new data to the file. In the case of a directory, the write permission means you can add, remove, and rename files in the directory. This means that if a file has the write permission bit, you are allowed to modify the file's contents, but you're allowed to rename or delete the file only if the permissions of the file's directory allow you to do so.
Execute permission: In the case of a regular file, this means you can execute the file as a program or a shell script.
we can view the access permissions of a file by doing the long directory listing with the ls -l command. This is what a long directory listing might look like:
[roo@raju~]ls -l
drwxr-xr-x 3 raju student 80 2012-11-27 21:37 dir
-rw-r----- 1 raju student 8187 2012-11-25 13:35 file
-rwxr-xr-x 1 raju student 10348 2012-10-30 20:31 otherfile
The very first column,shows the file type and permissions. The second column shows the number of links (directory entries that refer to the file), the third one shows the owner of the file, and the fourth one shows the group the file belongs to. The other columns show the file's size in bytes, date and time of last modification, and the filename.
The first character can be any of these:
d = directory
- = regular file
l = symbolic link
s =Linux domain socket
p = named pipe
c = character device file
b = block device file
The next nine characters show the file's permissions, divided into three groups, each consisting of three characters. The first group of three characters shows the read, write, and execute permissions for user, the owner of the file. The next group shows the read, write, and execute permissions for the group of the file. Similarly, the last group of three characters shows the permissions for other, everyone else. In each group, the first character means the read permission, the second one write permission, and the third one execute permission.
The characters are
r = read permission
w = write permission
x = execute permission
- = no permission

  Setting file permissions
We  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,
  1. Symbolic
  2. Numeric.
Symbolic mode
The symbolic mode option consists of three parts: the user category (owner, group, or other) affected, the function performed, and the permissions affected. For example, if the option is g+x, the executable permission is added for the group.


Numeric mode
The syntax for the chmod command in octal mode is:
chmod octalmode filename
The octalmode option consists of three octal numbers, 0 to 7, that represent a combination of permissions for the file or directory..
Assigned Octal Values for Permissions
Octal Value Permission
4   Read
2   Write
1   Execute
These numbers are combined into one number for each permission set.
Octal Digits for Permission Sets
           Octal Value
Permission Sets

7
rwx
421
6
rw-
420
5
r-x
401
4
r--
400
3
-wx
021
2
-w-
020
1
--x
001
0
---
000
You can modify the permissions for each category of users by combining octal numbers. The first octal number defines owner permissions, the second octal number defines group permissions, and the third octal number defines other permissions.
Combined Octal Mode Values and Permissions
           
Octal Mode

Permissions
644
rw-r--r--
751
rwxr-x--x
775
rwxrwxr-x
777
rwxrwxrwx
Changing Ownership
chown
     chown command is used to change the owner / user of the file or directory. This is an admin command, root user only can change the owner of a file or directory.

Syntax:
  The Syntax is
     chown [options] newowner filename/directoryname

Options:
-R
Change the permission on files that are in the subdirectories of the directory that you are currently in.
-c
Change the permission for each file.
-f
Prevents chown from displaying error messages when it is unable to change the ownership of a file.
chgrp:
     chgrp command is used to change the group of the file or directory. This is an admin command. Root user only can change the group of the file or directory.
Syntax:
  The Syntax is
     chgrp [options] newgroup filename/directoryname
Options:
-R
Change the permission on files that are in the subdirectories of the directory that you are currently in.
-c
Change the permission for each file.
-f
Force. Do not report errors.
Examples
[root@raju]#chmod  u+r g+r 0+x textfile
[root@raju]# chmod  ug+rwx o+r-- textfile
[root@raju]# chmod  755 textfile
[root@raju]#chown raju:raju  textfile
[root@raju]#chgrp koti textfile




Stay Connected With Free Updates!

Subscribe via Email

Follow me!

Leave Your Comment Below If You Like This Post

0 comments: