Linux Stall
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials
No Result
View All Result
Linux Stall
No Result
View All Result
Home Tutorials

Understanding file permissions and access rights in Linux

Chankey Pathak by Chankey Pathak
January 5, 2012
in Tutorials
45 0
4
Understanding file permissions and access rights in Linux
15
SHARES
754
VIEWS
Share on FacebookShare on Twitter

In Linux everything is a file. To set access rights on the specific files we use chmod command. To see what are the access rights/ permissions of a specific file we use ls -l command. Below we will learn how to see permission of a file and change it.

Note: To change permissions of a file you must be login as root user.

Suppose you have a file named as linuxstall.txt in a directory named as LinuxStall which is in /tmp. Use ls -l command to see its permissions as:

ls -l /tmp/LinuxStall/linuxstall.txt

The output will be as

-rwxr--r--. 1 root root 0 Jan 5 15:39 /tmp/LinuxStall/linuxstall.txt

Let us first understand what does the above line means by breaking it down in different parts.

The left most bit can be of two types either d or –.
If it is d then it means that it is a directory.
If it is – then it means that it is a file.

Here in our example it is – means it is a file.

Part 1: r w x (Shows owner’s access rights)
Explanation:
First bit is – which means it is a file, if it is d then it means it is directory.
First bit is r which means read i.e the owner can open this file.
Second bit is w which means write i.e the owner can edit this file.
Third bit is x which means execute i.e the owner can execute this file.

Part 2: r – – (shows group’s access rights)
Explanation:
First bit is r which means read i.e the group members can open this file.
Second bit is – which means group members can not edit this file.
Third bit is – which means group members can not execute this file.

Part 3: r – – (shows other users which are neither owner nor group members)
Explanation:
First bit is r which means read i.e the other users can open this file.
Second bit is – which means other users can not edit this file.
Third bit is – which means other users can not execute this file.

 

Changing file permission using chmod command

chmod stands for change file mode bits

There are two methods of changing file permission using chmod command.

Syntax: chmod permission path_of_file

Method 1: Numerical Method

In this method we use numeric bits which corresponds to r w x. The numeric values for these are

r = 4, w = 2, x = 1

chmod 444 /tmp/LinuxStall/linuxstall.txt

will change the file permission of file linuxstall.txt to

-r – – r – – r – – which means owner, group, others can only read this file.

chmod 555 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r – x r – x r – x which means owner, group, other can read and execute the file.

chmod 666 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r w – r w – r w – which means owner, group, other can read and edit the file.

chmod 644 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r w – r – – r – – which means owner can read and edit the file, group and other can only read the file.

Hence we saw that first numeric bit is for owner, second numeric bit is for group and third one is for other users. You can set the numeric bits to 1,2,4 for execute, write and read purpose respectively.

Method 2: Alphabetical Method

There are 4 alphabets associated with it, they are u,g,o and a.

u stands for owner

g stands for group

o stands for others

a stands for all

To add permission we use + and to remove permission we use –

Example 1: To grant read permission to all group users

chmod g+r /tmp/LinuxStall/linuxstall.txt

Example 2: To remove read permission from group users

chmod g-r /tmp/LinuxStall/linuxstall.txt

Example 3: To grant read,write,execute permission to owner

chmod u+rwx /tmp/LinuxStall/linuxstall.txt

Example 4: To remove execute permission from other users

chmod o-x /tmp/LinuxStall/linuxstall.txt

Example 5: To grant read and write permission to all users

chmod a+rw /tmp/LinuxStall/linuxstall.txt

Noticed use of a? It sets permission for all users.

 

Conclusion: We saw that each file in Linux has access rights associated with it. It is divided in 3 parts, one is owner, second is group users and third are other users. We can use chmod command to change the file permission. There are two methods, one is numerical and other is alphabetical method. Numerical method of setting permission is preferred though.

Tags: permissionssecurityTutorials
Previous Post

How to run a Linux Shell Script?

Next Post

How to install software in linux from source file (.zip, .tar.gz, .tar.bz2) ?

Chankey Pathak

Chankey Pathak

Data Scientist at Morgan Stanley. I've been using Linux since past 12 years. I plan to share what I know about Linux in this blog.

Related Posts

case of computer server workstation technology wireless vector illustration
Tutorials

Port Forwarding in OpenSSH

July 25, 2020
softlinking linux
Tutorials

Softlinks vs. Hardlinks: A Quick Explanation

July 23, 2020
write chroot read stat
Tutorials

Strace in Linux: History, Structure and Usage

July 21, 2020
case of computer server workstation technology wireless vector illustration
Tutorials

Brocade: Health Check Commands

July 12, 2020
A programmer coding
Tutorials

Using Basic Loops In Bash Scripts

July 12, 2020
uname
How to

How to find Linux distribution name and version?

June 22, 2014
Next Post
How to install software in linux from source file (.zip, .tar.gz, .tar.bz2) ?

How to install software in linux from source file (.zip, .tar.gz, .tar.bz2) ?

Comments 4

  1. Vishal Vyas says:
    9 years ago

    Nice site to get the knowledge about linux….

    Thanks

    Reply
    • Chankey Pathak says:
      9 years ago

      I am glad you like it Vishal :)

      Reply
  2. daemon says:
    9 years ago

    You can change permission for all files and folders inside with -R option

    example: chmod -R 777 uploads

    Reply
    • Chankey Pathak says:
      9 years ago

      Yes, -R option recursively changes permissions of all files of a directory and sub-directories. Thanks for sharing :)

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Terms and Conditions
  • Contact Us
  • About Us

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

No Result
View All Result
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In