Linux File Ownership

  • Every file and directory on your Unix/Linux system is assigned 3 types of owner, given below.
  • User
    • Owner of the File
    • User also called an owner
  • Group
    • User-group can contains multiple users
    • All users in the group have the same file permissions
  • Other
    • Any other user who has access to the file
    • Does not own the file nor belong to a User-group

Changing Ownership and Group in Linux

  • chown
  • chgrp
# Only changing the ownership of a file/directory
sudo chown <newOwner> <fileName>  

sudo chown user filename

# Change both user and group of a file or directory
sudo chown <newOwner:newGroup> <fileName>  
sudo chown user:group filename

# Only change group
chgrp group_name filename

Additional info

  • Check all groups in system
vim /etc/group
  • Groups you are member of
groups
# hitman adm cdrom sudo dip plugdev lpadmin sambashare docker
  • Change your default group
newgrp cdrom

Linux File Permissions

  • Read
    • Give you the authority to open and read a file
  • Write
    • Gives you the authority to modify the contents of a file
  • Execute
    • In Unix/Linux, you cannot run a program unless the execute permission is set.

The characters are pretty easy to remember.

r = read permission
w = write permission
x = execute permission
– = no permission

Linux File Permissions Linux File Permissions

Changing file/directory permissions in Linux Using ‘chmod’ command

# Syntax
chmod permissions filename
  • Absolute(Numeric) Mode in Linux
    • In the Absolute mode, you change permissions for all 3 owners
# Give rwx permissions to all
chmod 777 /MyStuff

# user--> 7(rwx), group-->5(r-x), other-->4(r--)
chmod 754 /MyStuff
  • Symbolic Mode in Linux
    • In the symbolic mode, you can modify permissions of a specific owner
# ugo -->  user, group, other
# +-= --> adding, removing, changing
chmod ugo+-=rwx /MyStuff

# Give read permissions to other
chmod o+r /MyStuff

# Remove, write and execute permissions for group and other
chmod go-wx /MyStuff