- Linux permissions are a fundamental aspect of system security and user management.
Basic Linux Permissions
- Linux employs a three-tiered permission system to control access to files and directories. Each file or directory has three main permission types:
Permission Types
-
- Read (r): This permission allows viewing the contents of a file or listing a directory’s contents.
- Write (w): This permission allows modifying a file or creating, renaming, and deleting files within a directory.
- Execute (x): This permission allows running a file as a program or accessing a directory’s contents.
Permission Classes
-
- Owner (u): The user who owns the file.
- Group (g): A group of users who can share the same permissions.
- Others (o): All other users on the system.
Permission Representation
-
-
Permissions are displayed using the
ls -l
command:
-
Here:
-
-
-
: Indicates a regular file (ord
for directories,l
for symbolic links, etc.).rwx
: Permissions for the owner.r-x
: Permissions for the group.r--
: Permissions for others.
- Each permission can also be represented numerically as :
- Read = 4, Write = 2, Execute = 1.
- Example:
rwx
= 7 (4+2+1),rw-
= 6 (4+2),r--
= 4.
-
Managing Linux Permissions
- Basic permissions can be managed using the
chmod
,chown
, andchgrp
commands.
Changing Permissions
-
-
Using Symbolic Permission Method
- To Add permission:
chmod u+x file
(adds execute for the owner). - To Remove permission:
chmod g-w file
(removes write for the group). - To Set specific permission:
chmod o=rw file
(sets read and write for others).
- To Add permission:
-
Using the Numeric Permission Method
- Example:
chmod 755 file
(Owner: rwx, Group: r-x, Others: r-x).
- Example:
-
Changing Ownership
-
- Change file owner:
chown username file
. - Change file group:
chgrp groupname file
. - Change both:
chown username:groupname file
.
- Change file owner:
Advanced Linux Permissions
- Advanced permissions provide greater flexibility for securing files and directories. These include special bits and ACLs (Access Control Lists).
Using Special Permission Bits
-
-
SetUID (
s
):- This allows a program to execute with the permissions of its owner.
- This special bit is commonly used for programs needing elevated privileges (e.g.,
passwd
). - For example :
chmod u+s file
(adds SetUID).
-
SetGID (
s
):- This ensures files created in a directory inherit the group ownership of the directory.
- This is useful for collaborative environments.
- For Example :
chmod g+s directory
.
-
Sticky Bit (
t
):- This is used on directories to restrict file deletion.
- Only the file owner or directory owner can delete files, regardless of other permissions.
- For example :
chmod +t directory
.
-
Access Control Lists (ACLs)
-
- ACLs provide fine-grained control by allowing permissions to be set for specific users or groups.
- To view ACL :
getfacl file
. - To modify ACL :
setfacl -m u:username:rw file
(grants read/write to a user). - To remove ACL :
setfacl -x u:username file
.
- To view ACL :
- ACLs provide fine-grained control by allowing permissions to be set for specific users or groups.
Managing Advanced Linux Permissions
Enabling and Managing Special Bits
-
- To SetUID :
chmod 4755 file
. - To SetGID :
chmod 2755 file
. - To Sticky Bit :
chmod 1755 directory
.
- To SetUID :
Managing ACLs
-
- To Set Default ACL:
setfacl -d -m u:username:rwx directory
(applies to new files within a directory). - To Set Recursive ACL:
setfacl -R -m g:groupname:rw directory
.
- To Set Default ACL:
Managing umask
- The
umask
determines the default permissions for newly created files and directories.
Understanding umask
-
- Default permissions before applying
umask
:- For Files:
666
(read and write for all). - For Directories:
777
(read, write, and execute for all).
- For Files:
umask
subtracts permissions from the default.- Example: A
umask
of022
results in:- Files:
644
(666 – 022). - Directories:
755
(777 – 022).
- Files:
- Example: A
- Default permissions before applying
Viewing and Setting umask
-
- To view current
umask
:umask
(press enter). - To set
umask
:umask 027
(sets default permissions to 640 for files and 750 for directories).
- To view current
Permanent umask Setting
-
- To make the
umask
permanent, add it to the shell configuration file, for example :- For Bash shell :
~/.bashrc
or~/.bash_profile
. - For Zsh shell :
~/.zshrc
.
- For Bash shell :
- To make the
Thus, simply we can say that this comprehensive framework ensures robust and flexible permissions management in Linux.
Summary of Commands Used in Permission Management in Linux
Permission Type Descriptions | Commands | Examples |
Change permissions | chmod |
chmod 755 file |
Change owner | chown |
chown user file |
Change group | chgrp |
chgrp group file |
SetUID | chmod u+s |
chmod 4755 file |
SetGID | chmod g+s |
chmod 2755 file |
Sticky Bit | chmod +t |
chmod 1755 directory |
View ACL | getfacl |
getfacl file |
Modify ACL | setfacl |
setfacl -m u:username:rw file |
View umask | umask |
umask |
Set umask | umask |
umask 027 |
0 Comments