Skip to main content

Permission Management

In Linux, file permissions are like security guards deciding who gets access to what. You wouldnโ€™t want your house to be public, just like you wouldnโ€™t want your system files to be accessed by anyone.

Permission Levels & Best Practicesโ€‹

rw-------	600	Only the owner can read and write (private files).
rw-r--r-- 644 Owner can read/write, others can only read.
rwxr-xr-x 755 Owner can read/write/execute, others can read/execute.
rwx------ 700 Owner can read/write/execute (private script).
rw-rw-r-- 664 Owner and group can read/write, others can read.

Set File Permissionsโ€‹

chmod 600 file.txt   # Only owner can read/write  
chmod 644 file.txt # Owner can read/write, others can only read
chmod 755 script.sh # Owner can read/write/execute, others can read/execute
chmod 700 secret.sh # Owner can read/write/execute, no access for others
chmod 664 shared.txt # Owner and group can read/write, others can read

Change Ownershipโ€‹

sudo chown [USER] [FILE] # Change ownership of a file to a specific user
sudo chown :[GROUP] [FILE] # Change group ownership of a file
sudo chown [USER]:[GROUP] [FILE] # Change user and group ownership of a file

Access Control Lists (ACL)โ€‹

ACLs provide more granular control over file permissions beyond the standard user/group/others model.

Set ACL Permissionsโ€‹

Use setfacl to grant additional user/group permissions:

setfacl -m u:[USERNAME]:rw [FILENAME]  # Give user read/write access
setfacl -m g:[GROUPNAME]:r [FILENAME] # Give group read-only access
setfacl -m o::--- [FILENAME] # Remove all access for others

View ACL Permissionsโ€‹

getfacl [FILENAME]

Remove ACL Permissionsโ€‹

setfacl -x u:[USERNAME] [FILENAME]  # Remove a userโ€™s ACL entry
setfacl -b [FILENAME] # Remove all ACL entries

Sticky Bit (t Permission)โ€‹

The sticky bit prevents users from deleting othersโ€™ files in a shared directory.

chmod +t [FILENAME]  # Enable sticky bit  
chmod -t [FILENAME] # Remove sticky bit

ls -ld [FILENAME] # Check sticky bit

# it will output like this (if enabled)
drwxrwxrwt 2 root root 4096 Mar 2 10:30 [FILEPATH or DIR]

Use Casesโ€‹

  • /tmp (default Linux temp directory)
  • Shared directories to prevent accidental/malicious deletions