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)