Commands for managing file and directory ownership and permissions.
Changes access permissions for files and directories.
| Syntax/Option | Description |
|---|---|
| chmod [mode] [file] | Change file permissions |
| -R | Change permissions recursively for directories and their contents |
| -v | Display verbose information about the process |
| --reference=[file] | Use the same permissions as the specified file |
Numeric Mode (Octal):
chmod 755 file.txt - Give owner read/write/execute, group and others read/execute permissionschmod 644 file.txt - Give owner read/write, group and others read-only permissionschmod -R 755 directory - Recursively set permissions for a directory and its contentschmod 600 id_rsa - Set private key file to be readable/writable only by ownerchmod 440 /etc/sudoers - Set sudoers file to be readable only by root owner and sudo groupfind . -type f -name "*.sh" -exec chmod 755 {} \; - Give execute permission to all shell scripts
Symbolic Mode:
chmod u+x file.txt - Add execute permission for ownerchmod g-w file.txt - Remove write permission for groupchmod o=r file.txt - Set read-only permission for otherschmod a+r file.txt - Add read permission for all userschmod u+x,g+x,o-rwx script.sh - Add execute permission for owner and group, remove all permissions for otherschmod -R g+rX directory/ - Recursively add read permission for group to all files, and execute permission only to directories (capital X applies only to directories or files that already have execute permission)find . -type d -exec chmod g+s {} \; - Set SGID bit on all directories (new files created will inherit the directory's group)
Numeric Permission Representation:
These values are combined. For example, 7 (4+2+1) represents read, write, and execute permissions.
The three digits represent permissions for "owner", "group", and "other users" from left to right.
Changes the owner and group of files and directories.
| Syntax/Option | Description |
|---|---|
| chown [owner]:[group] [file] | Change both owner and group of a file |
| chown [owner] [file] | Change only the owner of a file |
| chown :[group] [file] | Change only the group of a file |
| -R | Change ownership recursively for directories and their contents |
| -v | Display verbose information about the process |
| --reference=[file] | Use the same owner and group as the specified file |
Examples:
sudo chown user1 file.txt - Change file owner to user1sudo chown user1:group1 file.txt - Change file owner to user1 and group to group1sudo chown :group1 file.txt - Change only the group to group1sudo chown -R user1:group1 directory - Recursively change owner and group of a directory and its contentssudo chown --reference=ref_file.txt target_file.txt - Set the same owner and group as the reference filefind /var/www -type f -exec sudo chown www-data:www-data {} \; - Set appropriate ownership for web server filessudo chown -R $(whoami) ~/.config - Change ownership of config directory to current usersudo find /home/user -not -user user -exec chown user:user {} \; - Fix ownership of files in user's home directory
Changes the group ownership of files and directories.
| Syntax/Option | Description |
|---|---|
| chgrp [group] [file] | Change the group of a file |
| -R | Change group recursively for directories and their contents |
| -v | Display verbose information about the process |
| --reference=[file] | Use the same group as the specified file |
Examples:
sudo chgrp group1 file.txt - Change file group to group1sudo chgrp -R group1 directory - Recursively change group of a directory and its contentssudo chgrp --reference=ref_file.txt file.txt - Set the same group as ref_file.txtfind /opt/app -type f -name "*.log" -exec sudo chgrp syslog {} \; - Change group of all log files to syslogsudo chgrp -R $(id -gn) ~/projects - Change group of projects directory to current user's primary groupsudo find /var/www -type d -exec chgrp www-data {} \; - Set appropriate group for web server directoriessudo chgrp -v developers *.py - Change group of all Python files to developers and show details
Controls the default permissions for newly created files and directories.
| Syntax/Option | Description |
|---|---|
| umask | Display current umask value |
| umask [mode] | Set umask value |
| -S | Display in symbolic mode |
Examples:
umask - Display current umask value (e.g., 0022)umask -S - Display in symbolic mode (e.g., u=rwx,g=rx,o=rx)umask 022 - Set umask to 022 (new files will be 644, directories 755)umask 027 - Set umask to 027 (new files will be 640, directories 750)umask 077 - Set most restrictive umask (new files will be 600, directories 700)echo "umask 002" >> ~/.bashrc - Automatically set umask to 002 at login (good for group collaboration)( umask 077 && touch secret.txt ) - Temporarily change umask to create a secure fileumask -p - Display current umask in a format usable in shell scripts
How umask works:
Maximum permissions for files are 666 (rw-rw-rw-), and for directories are 777 (rwxrwxrwx).
The umask value specifies which permissions to "subtract" from these maximums.
For example, with umask 022:
Display and set more detailed Access Control Lists (ACLs).
| Command/Option | Description |
|---|---|
| getfacl [file] | Display ACLs for a file |
| setfacl -m u:[user]:[permissions] [file] | Set ACL for a specific user |
| setfacl -m g:[group]:[permissions] [file] | Set ACL for a specific group |
| setfacl -x u:[user] [file] | Remove ACL for a specific user |
| setfacl -b [file] | Remove all ACL entries |
| setfacl -R | Apply recursively to directory and its contents |
Examples:
getfacl file.txt - Display ACLs for a filesetfacl -m u:user1:rw file.txt - Grant read/write permissions to user1setfacl -m g:group1:r file.txt - Grant read permission to group1setfacl -x u:user1 file.txt - Remove ACL entry for user1setfacl -b file.txt - Remove all ACL entriessetfacl -R -m u:user1:rx directory/ - Recursively grant read/execute permissions to user1 for directory and contentsgetfacl file1.txt | setfacl --set-file=- file2.txt - Copy ACLs from file1.txt to file2.txtsetfacl -d -m g:project:rw directory/ - Set default ACLs for newly created files in directorysetfacl -m m:rx file.txt - Limit effective mask to read/execute onlyfind /shared -type d -exec setfacl -d -m g:team:rwx {} \; - Set default team ACLs for shared directories
Execute commands as another user (typically root).
| Option | Description |
|---|---|
| -u [user] | Execute command as specified user (default is root) |
| -i | Simulate initial login of the target user |
| -s | Run a shell |
| -l | List allowed commands for current user |
| -v | Update timestamp (extend authentication cache) |
| -k | Invalidate timestamp (require re-authentication next time) |
Examples:
sudo apt update - Run apt command as rootsudo -u www-data php script.php - Run PHP script as www-data usersudo -i - Start a login shell as rootsudo -l - List allowed commandssudo -s - Start a shell as root while keeping current environment variablessudo -E apt upgrade - Run command with current user's environment variablessudo -k - Invalidate sudo authentication cache (require password next time)sudo !! - Run previous command with sudosudo -u postgres psql - Connect to database as postgres usersudo -g wheel command - Run command with specified group privileges