Linux File Permissions Cheat Sheet

Basic File Permissions Overview
ConceptDescriptionExample
File PermissionsControls who can read, write, or execute a file
    -rwxr-xr--  1 user group  4096 Jul 28 10:30 script.sh
    ^ ^^^  ^^^  ^^^
    | |||  |||  |+- File owner and group
    | |||  |||  +-- Number of hard links
    | |||  |||  
    | |||  ||+- Others: read only
    | |||  |+-- Others: no write
    | |||  +--- Others: no execute
    | ||+------ Group: read only
    | |+------- Group: no write
    | +-------- Group: execute
    +---------- User: read, write, execute
            
Permission Display10-character string showing type and permissions
    -rwxrwxrwx = File with all permissions
    drwxr-xr-x = Directory with typical permissions
    -rw-r--r-- = Regular file with typical permissions
            
Permission Categories (User, Group, Others)
CategoryDescriptionExample
User (u)The owner of the filechmod u+x script.sh gives execution permission to the owner
Group (g)Users who are members of the file’s groupchmod g+rw data.txt gives read and write permission to the group
Others (o)All other users on the systemchmod o-rwx private.key removes all permissions for others
All (a)All three categories above (user, group, others)chmod a+r public.txt gives read permission to everyone
Symbolic Representation (rwx)
SymbolDescriptionEffect
rRead permissionFor files: Allows reading content
For directories: Allows listing contents
wWrite permissionFor files: Allows modifying content
For directories: Allows creating/deleting files within
xExecute permissionFor files: Allows running as a program
For directories: Allows entering the directory
No permissionThe permission is not granted for that position
Octal (Numeric) Representation (0-7)
Octal ValueBinaryPermissionsExample
0000No permissions (—)chmod 000 file.txt removes all permissions
1001Execute only (–x)chmod 111 file.sh sets execute-only for all
2010Write only (-w-)chmod 222 file.txt sets write-only for all
3011Write and execute (-wx)chmod 333 dir sets write and execute for all
4100Read only (r–)chmod 444 readme.txt sets read-only for all
5101Read and execute (r-x)chmod 555 program sets read and execute for all
6110Read and write (rw-)chmod 666 data.txt sets read and write for all
7111Read, write, and execute (rwx)chmod 777 script.sh sets all permissions for all
Changing Permissions with chmod (Symbolic and Numeric)
CommandDescriptionExample
chmod [options] mode fileChange file permissions
    # Numeric mode
    chmod 755 script.sh    # rwxr-xr-x
    chmod 644 text.txt     # rw-r--r--
    chmod 600 private.key  # rw-------

    # Symbolic mode
    chmod u+x script.sh    # Add execute to user
    chmod g-w file.txt     # Remove write from group
    chmod o= file.txt      # Clear all permissions for others
    chmod a+r document.pdf # Add read to all
            
Operators in symbolic mode+: Add permission
-: Remove permission
=: Set exact permission
    chmod u+rw,g+r,o-rwx private.txt
    # Add read/write for user, add read for group,
    # remove all for others
            
Changing Ownership with chown
CommandDescriptionExample
chown [options] owner[:group] fileChange the owner and/or group of a file
    # Change owner only
    chown alice file.txt
    
    # Change owner and group
    chown alice:staff file.txt
    
    # Change group only
    chown :developers project/
    
    # Recursively change ownership
    chown -R www-data:www-data /var/www/
            
Changing Group with chgrp
CommandDescriptionExample
chgrp [options] group fileChange the group of a file
    # Change group of a file
    chgrp developers project.py
    
    # Recursively change group of a directory
    chgrp -R staff /shared/documents/
            
Default Permissions and umask
Command/ConceptDescriptionExample
Default PermissionsFiles: 666 (rw-rw-rw-)
Directories: 777 (rwxrwxrwx)
These are base permissions before umask is applied
umaskMasks out permissions for newly created files
    # Display current umask (typically 022)
    umask
    
    # Set a new umask value
    umask 027
    # This will create files with 640 (rw-r-----)
    # And directories with 750 (rwxr-x---)
            
Common umask values022: rw-r–r– for files
027: rw-r—– for files
077: rw——- for files
Umask is subtracted from default permissions
Viewing Permissions (ls -l)
CommandDescriptionExample Output
ls -lList files with detailed permissions
    $ ls -l
    -rw-r--r-- 1 user  group   5678 Jun 21 13:45 data.txt
    -rwxr-xr-x 1 user  group   2345 Jun 19 10:32 script.sh
    drwxr-xr-- 2 admin staff   4096 Jun 20 15:22 secure_dir/
            
ls -laList all files (including hidden) with permissions
    $ ls -la
    drwxr-xr-x  3 user  group  4096 Jun 21 14:20 .
    drwxr-xr-x  7 user  group  4096 Jun 10 09:30 ..
    -rw-------  1 user  group  2345 Jun 15 16:42 .bash_history
    -rw-r--r--  1 user  group   220 Jun 10 09:30 .bashrc
    -rw-r--r--  1 user  group  5678 Jun 21 13:45 data.txt
            
stat fileDisplay detailed file information including permissions
    $ stat data.txt
      File: data.txt
      Size: 5678      Blocks: 16         IO Block: 4096   regular file
    Device: 801h/2049d  Inode: 1234567    Links: 1
    Access: (0644/-rw-r--r--)  Uid: ( 1000/   user)   Gid: ( 1000/  group)
    Access: 2023-06-22 10:45:12.000000000 +0200
    Modify: 2023-06-21 13:45:34.000000000 +0200
    Change: 2023-06-21 13:45:34.000000000 +0200
     Birth: -
            
Effect of Permissions on Directories
PermissionEffect on DirectoriesExample
r (read)List directory contents (view filenames)
    # With read permission
    $ ls -l dirwithread/
    -rw-r--r-- 1 user group 123 Jun 22 15:30 file1.txt
    -rw-r--r-- 1 user group 456 Jun 22 15:31 file2.txt

    # Without read permission
    $ ls -l dirwithoutread/
    ls: cannot open directory 'dirwithoutread/': Permission denied
            
w (write)Create, delete, or rename files within directoryRequires execute (x) permission as well to be useful
x (execute)Access files within directory, enter directory
    # With execute permission
    $ cd dirwithexec/
    $ cat dirwithexec/file.txt  # Works

    # Without execute permission
    $ cd dirwithoutexec/
    bash: cd: dirwithoutexec/: Permission denied
    $ cat dirwithoutexec/file.txt
    cat: dirwithoutexec/file.txt: Permission denied
            
Common directory permissions755 (rwxr-xr-x): Normal directory
700 (rwx——): Private directory
750 (rwxr-x—): Group shared directory
The execute bit is usually needed for directories to be useful
Understanding File Types in ls -l (-, d, l)
First CharacterTypeExample
Regular file-rw-r--r-- 1 user group 5678 Jun 21 13:45 data.txt
dDirectorydrwxr-xr-x 2 user group 4096 Jun 22 10:30 documents/
lSymbolic linklrwxrwxrwx 1 user group 12 Jun 22 11:15 link -> target.txt
cCharacter devicecrw--w---- 1 root tty 4, 0 Jun 22 14:30 /dev/tty0
bBlock devicebrw-rw---- 1 root disk 8, 0 Jun 22 09:20 /dev/sda
pNamed pipe (FIFO)prw-r--r-- 1 user group 0 Jun 22 15:40 mypipe
sSocketsrwxrwxrwx 1 user group 0 Jun 22 16:10 mysocket
Special Permissions: setuid, setgid, sticky bit
PermissionDescriptionExample
setuid (SUID)When set on a file, it executes with the permissions of the file owner rather than the executing user
    # Set SUID (numeric)
    chmod 4755 myprogram
    
    # Set SUID (symbolic)
    chmod u+s myprogram
    
    # Display: The 's' replaces 'x' in owner permissions
    -rwsr-xr-x 1 root root 123456 Jun 25 14:20 myprogram
    
    # Example: /usr/bin/passwd has SUID set
    -rwsr-xr-x 1 root root 68208 Apr 16 15:36 /usr/bin/passwd
            
setgid (SGID)For files: Executes with the permissions of the file’s group
For directories: New files created in the directory inherit its group
    # Set SGID (numeric)
    chmod 2755 myprog
    chmod 2770 shared_dir/
    
    # Set SGID (symbolic)
    chmod g+s myprog
    chmod g+s shared_dir/
    
    # Display: The 's' replaces 'x' in group permissions
    -rwxr-sr-x 1 user group 123456 Jun 25 14:30 myprog
    drwxrws--- 2 user group 4096 Jun 25 14:35 shared_dir/
            
sticky bitWhen set on a directory, files in that directory can only be deleted or renamed by their owner, the directory owner, or root
    # Set sticky bit (numeric)
    chmod 1777 /tmp
    
    # Set sticky bit (symbolic)
    chmod +t /tmp
    
    # Display: The 't' replaces 'x' in others permissions
    drwxrwxrwt 15 root root 4096 Jun 25 15:00 /tmp
            
Special permission bits (octal)4 = SUID
2 = SGID
1 = sticky bit
    chmod 4755 file  # SUID + rwxr-xr-x
    chmod 2755 file  # SGID + rwxr-xr-x
    chmod 1777 dir   # sticky bit + rwxrwxrwx
    chmod 6755 file  # SUID + SGID + rwxr-xr-x
            
Recursive Permission Changes (chmod -R, chown -R)
CommandDescriptionExample
chmod -R mode directoryRecursively change permissions for a directory and all its contents
    # Give read and execute permissions to everyone for all files in a directory
    chmod -R a+rx /path/to/website/
    
    # Set permissions to 755 for directories and 644 for files
    # This requires a more complex approach:
    find /path/to/dir -type d -exec chmod 755 {} \;
    find /path/to/dir -type f -exec chmod 644 {} \;
            
chown -R user:group directoryRecursively change ownership for a directory and all its contents
    # Change ownership of a web directory and all contents
    chown -R www-data:www-data /var/www/html/
    
    # Change ownership but preserve file timestamps
    chown -R --preserve-timestamps user:group directory/
            
Understanding Permissions for Executable Files
ConceptDescriptionExample
Executable bitRequired to run a file as a program
    # Make a compiled program executable
    chmod +x program
    
    # Common permission pattern for executables
    chmod 755 program  # rwxr-xr-x
            
Permissions requirementsIn addition to execute permission, binaries typically need read permission to load into memory
    # Try to run a program without execute permission
    $ chmod -x program
    $ ./program
    bash: ./program: Permission denied
    
    # Try to run a program with execute but no read
    $ chmod 111 program
    $ ./program
    bash: ./program: Permission denied
            
Understanding Permissions for Scripts
ConceptDescriptionExample
Basic script permissionsScripts need both read and execute permissions
    # Create a simple script
    $ echo '#!/bin/bash' > script.sh
    $ echo 'echo "Hello, World!"' >> script.sh
    
    # Make it executable
    $ chmod +x script.sh
    $ ./script.sh
    Hello, World!
            
Interpreter permissionsThe interpreter specified in the shebang line must also be executableIf you’re using #!/bin/bash, the bash executable must also have proper permissions
Common script permissions755 (rwxr-xr-x): Script can be run by anyone
700 (rwx——): Private script
    # Private script
    chmod 700 backup_my_files.sh
    
    # Shared script
    chmod 755 common_utilities.sh
            
ConceptDescriptionExample
Symlink permissionsPermissions on symbolic links are not used; the permissions of the target file are what matter
    $ ls -l mylink
    lrwxrwxrwx 1 user group 7 Jun 25 16:20 mylink -> target
    
    # Notice all permissions are set, but they're ignored
    # Actual access is determined by permissions on "target"
            
Changing link permissionsAttempting to change permissions on a symlink affects the target, not the link itself
    # Create a symbolic link
    $ ln -s target mylink
    
    # Trying to change permissions of the link
    $ chmod 600 mylink
    
    # This actually changes permissions of the target
            
ConceptDescriptionExample
Hard link permissionsHard links share the same inode as the original file, so they always have identical permissions
    # Create a file
    $ echo "content" > original
    $ chmod 644 original
    
    # Create a hard link
    $ ln original hardlink
    
    # Both show the same permissions because they share the same inode
    $ ls -l original hardlink
    -rw-r--r-- 2 user group 8 Jun 25 16:30 hardlink
    -rw-r--r-- 2 user group 8 Jun 25 16:30 original
            
Changing permissionsChanging permissions on either the original file or the hard link affects all hard links to that inode
    # Change permissions on the hard link
    $ chmod 600 hardlink
    
    # Both files now show the new permissions
    $ ls -l original hardlink
    -rw------- 2 user group 8 Jun 25 16:30 hardlink
    -rw------- 2 user group 8 Jun 25 16:30 original
            
Sticky Bit on Directories (/tmp)
ConceptDescriptionExample
PurposePrevents users from deleting or renaming files they don’t own in shared directoriesCommonly used on /tmp and other shared directories
Setting sticky bitCan be set with numeric mode 1xxx or symbolic mode +t
    # Set sticky bit with numeric mode
    chmod 1777 /shared
    
    # Set sticky bit with symbolic mode
    chmod +t /shared
            
Identifying sticky bitAppears as “t” in the execute position for others
    $ ls -ld /tmp
    drwxrwxrwt 20 root root 4096 Jun 26 10:15 /tmp
                ^
                |-- 't' indicates sticky bit is set
            
Finding Files Based on Permissions (find -perm)
CommandDescriptionExample
find path -perm modeFind files with exact permissions
    # Find files with exactly 644 permissions
    find /home -perm 644
            
find path -perm -modeFind files with at least these permissions (all bits in mode must be set)
    # Find files where all users have read permission
    find /home -perm -444
            
find path -perm /modeFind files with any of these permissions (any bit in mode is set)
    # Find files that are world-writable
    find /home -perm /002
            
Common permission searchesFinding files with specific security concerns
    # Find SUID files
    find / -perm -4000 -type f
    
    # Find world-writable files
    find / -perm -2 -type f -not -path "/proc/*"
    
    # Find world-writable directories
    find / -perm -2 -type d -not -path "/proc/*"
    
    # Find files not owned by any user
    find / -nouser
            
Access Control Lists (ACLs) Introduction
ConceptDescriptionExample
ACLs purposeExtend traditional permission model to allow more granular controlAllow specific users or groups access without changing ownership or basic permissions
ACLs vs traditional permissionsTraditional: one owner, one group, and others
ACLs: multiple users/groups with different permissions
Useful when standard rwx permissions aren’t flexible enough
Identifying files with ACLsThe “+” symbol after the permission string in ls -l output
    $ ls -l file_with_acl
    -rw-r--r--+ 1 user group 1234 Jun 26 11:30 file_with_acl
                ^
                |-- The '+' indicates ACLs are set
            
Setting and Modifying ACLs (setfacl, getfacl)
CommandDescriptionExample
getfacl fileDisplay ACLs for a file
    $ getfacl document.txt
    # file: document.txt
    # owner: john
    # group: staff
    user::rw-
    user:mary:r--
    group::r--
    mask::r--
    other::---
            
setfacl -m rule fileModify ACL for a file
    # Give user alice read and write permissions
    setfacl -m u:alice:rw- document.txt
    
    # Give group devs read permission
    setfacl -m g:devs:r-- document.txt
    
    # Multiple rules at once
    setfacl -m u:bob:r--,g:admins:rwx document.txt
            
setfacl -x rule fileRemove specific ACL entries
    # Remove ACL for user alice
    setfacl -x u:alice document.txt
    
    # Remove ACL for group devs
    setfacl -x g:devs document.txt
            
setfacl -R rule directoryRecursively apply ACLs to a directory
    # Recursively give read access to user bob
    setfacl -R -m u:bob:r-x /shared/projects/
            
Default ACLs on Directories
ConceptDescriptionExample
Default ACLsACLs automatically applied to new files/directories created within a directorySpecify inheritance of permissions for new items
Setting default ACLsUse the ‘d:’ prefix with setfacl
    # Set default ACL for user alice
    setfacl -m d:u:alice:rwx /projects/
    
    # Set default ACL for group devs
    setfacl -m d:g:devs:rx /projects/
    
    # Set multiple default ACLs
    setfacl -m d:u:bob:rx,d:g:admins:rwx /projects/
            
Viewing default ACLsShown in getfacl output with “default:” prefix
    $ getfacl /projects/
    # file: /projects/
    # owner: admin
    # group: staff
    user::rwx
    group::r-x
    other::r-x
    default:user::rwx
    default:user:alice:rwx
    default:group::r-x
    default:group:devs:r-x
    default:mask::rwx
    default:other::r-x
            
Removing ACLs (setfacl -b)
CommandDescriptionExample
setfacl -b fileRemove all ACLs from a file
    # Remove all ACLs from a file
    setfacl -b document.txt
            
setfacl -k directoryRemove default ACLs from a directory
    # Remove only default ACLs
    setfacl -k /projects/
            
setfacl -R -b directoryRecursively remove all ACLs
    # Recursively remove all ACLs from directory and contents
    setfacl -R -b /projects/
            
Security Risks: Misconfigured Permissions
RiskDescriptionMitigation
World-writable filesFiles that anyone can modify (permissions containing o+w)
    # Find world-writable files
    find / -type f -perm -o=w -not -path "/proc/*" -ls
    
    # Fix by removing world write permission
    chmod o-w vulnerable_file
            
SUID/SGID on scriptsScripts with setuid/setgid can lead to privilege escalation
    # Find SUID/SGID files
    find / -type f \( -perm -4000 -o -perm -2000 \) -ls
    
    # Remove SUID/SGID if not needed
    chmod u-s,g-s script.sh
            
Incorrect home directory permissionsHome directories accessible to others can expose sensitive data
    # Secure home directory
    chmod 750 /home/username
    
    # Check with:
    ls -ld /home/*
            
SSH key permissionsSSH will refuse to use keys with loose permissions
    # Correct permissions for SSH private keys
    chmod 600 ~/.ssh/id_rsa
    
    # Correct permissions for SSH directory
    chmod 700 ~/.ssh
            
Best Practices for File and Directory Permissions
PracticeDescriptionExample
Principle of least privilegeGrant only permissions that are necessary for functionalityUse 644 (rw-r–r–) for regular files, not 666 (rw-rw-rw-)
Home directory securityProtect user home directories from other users
    # Secure home directory
    chmod 750 /home/username
    
    # Ensure sensitive files are private
    chmod 600 ~/.bash_history ~/.aws/credentials
            
Configuration file securityProtect sensitive configuration files that may contain credentials
    # Make config files only readable by owner
    chmod 600 config.ini credentials.txt
    
    # System configs readable by specific groups
    chmod 640 /etc/mysql/my.cnf
            
Web directory permissionsBalance security with web server requirements
    # Web directories
    find /var/www -type d -exec chmod 755 {} \;
    
    # Web files
    find /var/www -type f -exec chmod 644 {} \;
    
    # CGI scripts
    find /var/www/cgi-bin -type f -exec chmod 755 {} \;
            
Troubleshooting Permissions Issues
ProblemCommon CausesSolution
“Permission denied” when running a scriptMissing execute permission
    # Add execute permission
    chmod +x script.sh
            
“Permission denied” when accessing a directoryMissing execute permission on the directory or a parent directory
    # Add execute permission to directory
    chmod +x /path/to/directory
    
    # Check parent directories too
    ls -ld / /path /path/to
            
Cannot modify files in a directory despite write permissionMissing write permission on the directory itself
    # Add write permission to directory
    chmod +w /path/to/directory
            
Text editor creates a temporary file but fails to saveWrite permission on file but not on directory
    # Add write permission to directory
    chmod +w /path/to/directory
            
Permissions and System Services (Apache, SSH)
ServicePermission RequirementsExample
Apache/Nginx web serverWeb server runs as www-data user and needs read access to web files, execute access to directories
    # Standard web directory permissions
    chown -R www-data:www-data /var/www/html
    find /var/www/html -type d -exec chmod 755 {} \;
    find /var/www/html -type f -exec chmod 644 {} \;
    
    # For writable directories (uploads, cache)
    chmod 775 /var/www/html/uploads
            
SSH ServerSSH is sensitive to permissions on key files and config directories
    # Server host keys
    chmod 600 /etc/ssh/ssh_host_*_key
    chmod 644 /etc/ssh/ssh_host_*_key.pub
    
    # sshd_config
    chmod 600 /etc/ssh/sshd_config
    
    # User's .ssh directory
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
            
Database (MySQL/PostgreSQL)Database files must be owned by the database user
    # MySQL data directory
    chown -R mysql:mysql /var/lib/mysql
    chmod 750 /var/lib/mysql
    
    # MySQL config
    chmod 640 /etc/mysql/my.cnf
            
Auditing and Logging Permission Changes
MethodDescriptionExample
auditdLinux audit system to monitor permission changes
    # Install auditd
    apt install auditd
    
    # Add audit rule for chmod
    auditctl -w /bin/chmod -p x -k permission_changes
    
    # Add audit rule for chown
    auditctl -w /bin/chown -p x -k permission_changes
    
    # View audit logs
    ausearch -k permission_changes
            
File integrity monitoringTools that monitor file changes including permissions
    # Install AIDE
    apt install aide
    
    # Initialize database
    aide --init
    
    # Check for changes
    aide --check
            
Permissions in Networked File Systems (NFS, SMB)
SystemPermission CharacteristicsExample
NFS (Network File System)Maps UIDs/GIDs between systems; can use root_squash to restrict root access
    # /etc/exports entry with permission options
    /shared *(rw,sync,no_root_squash)
    
    # Mounting with specific permissions
    mount -o rw,users server:/shared /mnt/shared
            
Samba (SMB/CIFS)Can map to UNIX permissions or use Windows ACLs
    # Samba share with permissions in smb.conf
    [shared]
      path = /shared
      valid users = @staff
      writable = yes
      create mask = 0644
      directory mask = 0755
            
SELinux/AppArmor and Extended Permissions
SystemDescriptionExample
SELinuxSecurity Enhancement for Linux providing Mandatory Access Control
    # View SELinux context of a file
    ls -Z /var/www/html/index.html
    
    # Change SELinux context
    chcon -t httpd_sys_content_t /path/to/file
    
    # Set default context for web content
    semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
    restorecon -R /web
            
AppArmorApplication security system that restricts program capabilities
    # View AppArmor status
    aa-status
    
    # Put profile in complain mode
    aa-complain /usr/sbin/apache2
    
    # Put profile in enforce mode
    aa-enforce /usr/sbin/apache2
            
Permissions in Docker Containers and Mounted Volumes
ConceptDescriptionExample
Volume permissionsContainer UIDs/GIDs may not match host UIDs/GIDs
    # Set explicit ownership on mounted volume
    docker run -v /host/data:/container/data:rw \
      --user 1000:1000 my-image
    
    # Use chown when creating volume
    docker run -v /host/data:/container/data:rw \
      my-image chown -R user:group /container/data
            
Docker security implicationsRunning containers as root can pose security risks
    # Run container as non-root user
    docker run --user 1000:1000 my-image
    
    # In Dockerfile
    USER appuser
            
User and Group ID Mapping (UID/GID Concepts)
ConceptDescriptionExample
UID/GIDNumeric IDs that define users and groups in Linux
    # View user and group info
    id username
    
    # View file ownership by ID
    ls -ln /path/to/file
            
Orphaned UIDs/GIDsFiles owned by deleted users or groups
    # Find files with no valid owner
    find / -nouser
    
    # Find files with no valid group
    find / -nogroup
    
    # Fix orphaned files
    chown newuser:newgroup /path/to/orphaned/file
            
Managing system usersSystem services often use specific UIDs/GIDs
    # Create system user
    useradd -r -s /usr/sbin/nologin serviceuser
    
    # Create system group
    groupadd -r servicegroup
            
Inheriting Permissions in Different Filesystems
FilesystemInheritance BehaviorExample/Control
Standard Linux filesystems (ext4, XFS)New files inherit the group of parent directory if SGID is set; umask affects new file permissions
    # Set SGID on directory
    chmod g+s /shared/project
    
    # Files created will inherit the group
    touch /shared/project/newfile
    
NFSv4Supports ACL inheritance
    # Set inherited ACLs with NFSv4
    nfs4_setfacl -a A:fd:user@domain:rwx /mnt/nfs/dir
            
ZFSCan set ACL inheritance flags
    # Set ACL inheritance on ZFS
    chmod A+everyone@:read_data/inherited:allow /tank/data
            
Permission Masks in Cloud and Virtualized Environments (AWS, Azure)
EnvironmentPermission ConsiderationsExample
AWS EC2Default umask may differ depending on AMI
    # Check current umask
    umask
    
    # Set default umask in /etc/profile or ~/.bashrc
    umask 022
            
AWS S3Object storage uses ACLs instead of UNIX permissions
    # AWS CLI S3 command with ACL
    aws s3 cp file.txt s3://bucket/ --acl public-read
            
Docker/container environmentsContainer images may have different default permissions
    # In Dockerfile
    RUN mkdir -p /app && chmod 755 /app
    COPY --chown=appuser:appgroup app/ /app/
            
Immutable and Append-Only Attributes (chattr, lsattr)
Command/AttributeDescriptionExample
chattr +i fileMake a file immutable – cannot be modified, renamed, deleted, or linked
    # Make a file immutable
    chattr +i important.conf
    
    # Try to modify an immutable file
    $ rm important.conf
    rm: cannot remove 'important.conf': Operation not permitted
            
chattr +a fileMake a file append-only – can only be opened in append mode
    # Make a log file append-only
    chattr +a logfile.log
    
    # Can add data:
    echo "new entry" >> logfile.log
    
    # Cannot truncate:
    $ echo "overwrite" > logfile.log
    bash: logfile.log: Operation not permitted
            
lsattr fileView file attributes
    $ lsattr important.conf
    ----i--------e-- important.conf
    
    $ lsattr logfile.log
    -----a-------e-- logfile.log
            
Filesystem Mount Options and Permissions (noexec, nosuid)
Mount OptionDescriptionExample
noexecPrevents execution of binaries on the mounted filesystem
    # Mount with noexec
    mount -o noexec /dev/sdb1 /mnt/data
    
    # In /etc/fstab
    /dev/sdb1 /mnt/data ext4 defaults,noexec 0 0
            
nosuidDisables SUID/SGID bits on the mounted filesystem
    # Mount with nosuid
    mount -o nosuid /dev/sdb1 /mnt/data
    
    # In /etc/fstab
    /dev/sdb1 /mnt/data ext4 defaults,nosuid 0 0
            
nodevPrevents interpretation of character or block special devices
    # Mount with multiple security options
    mount -o noexec,nosuid,nodev /dev/sdb1 /mnt/data
            
Effective Permissions vs Displayed Permissions
ConceptDescriptionExample/Check
Effective permissionsThe actual permissions a user has after considering all factorsA combination of file permissions, ACLs, and extended security frameworks
Primary group vs. supplementary groupsUsers can belong to multiple groups but have one primary group
    # Check all groups a user belongs to
    groups username
    
    # Check effective group membership
    id username
            
Checking effective permissionsTest actual access rather than just viewing permissions
    # Test file access as a different user
    sudo -u otheruser cat /path/to/file
    
    # Check access while considering ACLs
    getfacl /path/to/file
            
Temporary Permission Changes (umask in scripts)
TechniqueDescriptionExample
Temporary umaskChange umask temporarily for script execution
    #!/bin/bash
    # Save original umask
    old_umask=$(umask)
    
    # Set strict umask
    umask 077
    
    # Create sensitive file
    touch sensitive_data.txt
    echo "secret" > sensitive_data.txt
    
    # Restore original umask
    umask $old_umask
            
Context-specific permissionsTemporary permission changes for specific operations
    # Temporarily open permissions, then restore
    chmod 644 config.file
    editor config.file
    chmod 600 config.file
            
Temporary group membershipRun commands with temporary group context
    # Run a command with a different group
    sg developers -c "touch /shared/project/newfile"
    
    # Run as a different user/group
    sudo -u webuser -g webgroup command
            

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart