Thursday 21 August 2014

Linux Permissions 101

(updated 26/04/2017)

Howdy guys Welcome to my noob friendly guide to Linux Permissions, I will be starting off with the basics and will be gradually incorporating more advanced concepts. I hope you guys find this information useful and please keep checking back as I will be updating the content as I figure out how to become a command line ninja myself. :)

So let's go forth and kick this off starting with the basics below we will look at file ownership. 

--------------------------------------------------------------------

File Ownership: Changing Owner of file




-rw- rw- r-- 1 Ally Ally 360 Mar 9 14:26 file.txt

The above example is showing the permissions on a text file I created called file.txt. You can view permissions by simply running the command ls -l 

 The Ally represents who actually owns this file in this case Ally. The Ally represents the group that    owns the file in this case Ally. 

So how do you go about changing the file ownership of the group and changing the Ally to Bob

To change ownership of a file we use the command chown

So to change the owner of the file from Ally to Bob we run the following:

sudo chown bob file.txt

if you run ls -l again to show the new permissions you will see that the new owner of the file will be:

-rw- rw- r-- 1 Bob Ally 360 Mar 9 14:26 file.txt

As you can see the owner of the file is now Bob and the file still belongs to the group Ally. 

Changing Group:

How do you change the group ownership of the file file? 

Easy :)

if you wanted the group finance to own this particular file you would simply use the command

chgrp

sudo chgrp finance file.txt

We have no successfully changed the group ownership and the new permissions on our file will look like this:

-rw- rw- r-- 1 Bob finance 360 Mar 9 14:26 file.txt

So we have successfully taught ourselves two new commands:

chown and chgrp 

but Ally what the hell does -rw rw- r--- represent? keep reading and all will be revealed below where we will discuss Linux Permissions :)

------------------------------------------------------------------------------------------------------------------------

Linux Permissions


Because I am a visual learner below is a example of permissions of a directory.
For example if you fire up your terminal type ls -l to view permissions of a particular file or directory you are interested in. 
  • As shown below the d at the start of the permission sequence represents that this is a directory ( d = directory,  - = file)
  • Permissions are broken down into three segments (User, Group, Other)
  • User has the permission READ, WRITE EXECUTE
  • Group has Read Permission remember that - represents no permission assigned
  • Other also has Read Permission assigned
  • R,W,X are shorthand for the Permissions representing Read, Write, Execute

  Ok Moving on lets look at how to use Chmod to change permissions on a file / directory.

 

 

 

 

There is also another advanced way to use Chmod to calculate permissions

In the below example we are working with a file which has the following permissions set

d rwx rwx rwx 

I will be walking you through the process of finding the octal values of the above permission :) remember! that permissions have 3 sections! representing:

Owner
Group
Other 

do not forget this :) and the d at the start represents that this file is a Directory.

Chmod Octal Notation

 

  (Chmod Bit assignment)

7 full
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
0 none

Below is a cheat sheet of common chmod values.

  

 

No comments:

Post a Comment