DEV Community

Chethan
Chethan

Posted on

Understanding Linux Permissions: A Beginner-Friendly Guide for DevOps πŸš€

One of the most important concepts that I and many beginners encountered was File Permissions. These topics are super important if you want to become handy with handling Linux files. In this blog, I will deep dive and provide a brief overview of Linux File permissions and help others learn it fast.

πŸ“ŒListing File Permissions:
To check permissions of a file use: ls -l
Files in Linux have different types:

1) - β†’ regular file
2) d β†’ directory
3) l β†’ symbolic link (e.g., lrwxrwxrwx 1 root root 7 Aug 26 10:00 lib -> usr/lib)

In the permissions string (rwxr-xr-x):
1) r β†’ read
2) w β†’ write
3) x β†’ execute

The string is divided into 3 sets:
1) User (owner)
2) Group
3) Others

So rwxr-xr-x means: owner can read, write, execute; group can read & execute; others can read & execute.

πŸ”‘Changing Permissions with chmod:
Permissions can be set numerically or symbolically:

1) Numeric (octal method)
r = 4, w = 2, x = 1
Eg: To give all permissions to all sets of users: chmod 777 file.txt

2) Symbolic (using letters)
Eg:
1) To give read permission to the user: chmod u+r file.txt
2) To give read permissions to both the group and others: chmod go+r file.txt

To remove permission use '-' : chmod g-r file.txt

βš™οΈDefault Permissions with unmask:
When a new file is created, its default permissions depend on the umask value.

Default mask : 022
To change it : umask 021 (This removes read permission from group and execute permission from others)

πŸ”’Special Permission Bits:

Linux also supports advanced permission bits:
1) SUID (Set User ID) – executable runs with file owner’s privileges :

Eg: chmod u+s file.txt , chmod 4555 file.txt

2) SGID (Set Group ID) - Here there are two cases:

  • On files β†’ runs with file group privileges
  • On directories β†’ new files inherit directory’s group

Eg: chmod g+s file.txt , chmod 2777 file.txt

3) Sticky Bit – on directories, prevents deletion of files by anyone except the owner

Eg : chmod +t dir , chmod 1755 file.txt

πŸ‘₯Change Ownership:

1) Change file owner. Eg: chown newuser filename
2) Change group ownership. Eg:chown :newgroup filename
3) Change both. Eg:chown -R newuser:newgroup director

To change group:

1) For a single file. Eg: chgrp newgroup filename
2) For all files in directory. Eg: chgrp -R newgroup directory/

This was my learning summary on Linux permissions. If you’re also exploring DevOps/Linux, practicing these commands hands-on will help things click much faster. Do share it with your newtork and share your thoughts on this πŸ™Œ

πŸ‘‰ Which of these file permissions do you use the most ?

Top comments (0)