How to Change Permission of Files and Directories in Unix/Linux

Unix / Linux File and Directories Permissions:

File Ownership is very important component of Unix that provide a secure method for storing files. Every File In Linux/Unix has three following attributes.
  • Owner Permissions
  • Group permissions
  • Other Permissions

Permissions are applied on three lavels
  1. Owner or User level Permissions: The owner’s level permissions determine what actions the owner of the file can perform on the file.
  2. Group level Permissions:  The group's level permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.
  3. Others level Permissions: The others level permissions indicate what action all other users can perform on the file.

How to check file Permissions?

While using ls -l Unix command it displays various information related to file permission as follows:

# ls –l filename


[root@kernal ~]# ls -l /home/rahul
-rwxr-xr--   1 rahul  users 1024 Nov 21 00:11 myfile
drwxr-xr--- 1 rahul  users 1024 Nov 21 00:12 mydir


Filetype+permissions, links, owner, group name of owner, size in bytes, date of modification, filename

The first column represents different access mode ie. permission associated with a file or directory.
The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute (x):

  • Access modes are of three types:

The basic building blocks of Unix permissions are the read, write, and execute permissions, which are described below:
  1. r          Read Only
  2. w         Write/edit/delete/append
  3. x          Eexecute/run a command

  • Access modes are different on file and directory:

Permissions
Files
Directory
R
Open the file
‘ls’ the contents of dir
W
Write,edit,append,delete file
Add/del/rename contents of dir
X
To run a command/shell script
To enter into dir usig ‘cd’

Changing Permissions: To change file or directory permissions, you use the chmod (change mode) command. There are two ways to use chmod:
  1. symbolic mode(ugo)
  2. absolute mode(numbers)

Symbolic Mode Using chmod : The easiest way for a user 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 operator
Description
+
Adds the designated permission(s) to a file or directory.
-
the designated permission(s) from a file or directory.
=
Removes Sets the designated permission(s).

Who  à To whom the permissions to be assigned

User/owner(u); Group (g); Others(o)

Example:

Assigning different permissions to the file (user=rwx,group=rw and oters=r)

# chmod u=rwx,g=rw,o=r  file1(filename)


[root@kernal ~]# chmod u=rwx,g=rw,o=r file
[root@kernal ~]# ls -l testfile
-rwxrw-r-- 1 rahul users 1042 Nov 21 00:20 file


Assigning full permission to the file i.e. rwx to all

# chmod ugo=rwx file1


[root@kernal ~]# chmod ugo=rwx file1
[root@kernal ~]# ls -l file1
-rwxrwxrwx 1 rahul users 1024 Nov 2 00:10 file


Likewise you can add or remove permissions from any file for anyone ( user,group and other)
  • # chmod u+x file1 (Adding permission to user only)
  • # chmod go-wx file1 ( Removing write and execute permissions to group and other )
  • # chmod uo+wx file1 (Adding write and execute permissions to user and other )
  • # chmod go=r file1 ( Giving only read permission to group and other )

 Absolute Permissions with Using chmod : The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.

Number
Octal Permission Representation
4
Read Permission
2
Write Permission
1
Execute permission

Example:

Assigning different permissions to the file

# chmod 764 file ( where 7 means rwx i.e.4+2+1, rw=6 i.e. 4+2 and 1 indicates x)


[root@kernal ~]#  ls -l file
-rwxrwxr--  1 rahul  users  1024 Nov 21 00:30 file
[root@kernal ~]#  chmod 764 file
[root@kernal ~]#  ls -l file
-rwxrw-r--  1 rahul  users  1024 Nov 21 00:30 file


Assigning full permission to the file i.e. rwx to all

# chmod 777 file1


[root@kernal ~]#  ls -l file1
-rwxrw-r--     1 rahul  users  1024 Nov 21 00:32 file1
[root@kernal ~]#  chmod 777 file1
[root@kernal ~]#  ls -l file1
-rwxrwxrwx  1 rahul  users  1024 Nov 21 00:32 file1


Removing all permission from others

# chmod 770 file1 (where 0 indicates no permissions)


[root@kernal ~]#  ls -l file1
-rwxrwxrwx    1 rahul  users  1024 Nov 21 00:32 file1
[root@kernal ~]#  chmod 777 file1
[root@kernal ~]#  ls -l file1
-rwxrwx---      1 rahul  users  1024 Nov 21 00:32 file1


Note: - All the above permissions and Procedure is same for file and directories.


If you Like post then share and comment please And if you have any suggestion for me do comment .



Post a Comment