Saturday, 18 April 2015

Linux Manage file permissions and ownership

Login as admin user
       su anusree

Add new linux group named sale

       sudo groupadd sale

Add new user s1 with home folder

      sudo useradd -m s1
      -m : create home folder

Add another user s2 with home folder
      sudo useradd -m s2

 Add the existing user s1 and s2 to the group sale
      sudo adduser s1 sale
      sudo adduser s2 sale
Create a folder sale in /opt
      sudo mkdir sale

Create a file named sale.txt in the folder /opt/sale
      sudo vim sale.txt

Check the permissions, owner and group of the file sale.txt
       ls -l sale.txt
       -rw-r--r-- 1 root root 0 Apr 19 00:13 sale.txt
             |    |
             |    +----- group (root)
             +---------- user (root)

        here user is the root and group is root, here user have read and write     permission and user's in group root have only read permission, and other user's have read permission

 I am going to change the group of the file sale.txt from root group to sale group

      sudo chown root :sale sale.txt
      sudo chown :sale  sale.txt

I am going to give read and write permission to the user's in the group sale to access the file sale.txt

      sudo chmod g+rw sale.txt

 Now i am giong login as one of the user inthe group sale (eg: s1) and trying to read and write to the file sale.txt , and this should work because i already set the permission to read and write to the group sale
      su s1

If you want to change the user (owner) of the file sale.txt from root to s1
      sudo chown s1:sale  sale.txt

Check the owner or user of the file sale.txt 

      ls -l
      -rw-rw-r-- 1 s1 sale 41 Apr 18 22:59 sale.txt                               
             |    |
             |    +----- group (sale)
             +---------- user (s1)