tips
30 tips Permissions
One command, setting or habit at a time about Permissions: what it does, the line to copy and a caveat or two.
Directories and groups 9
-
Makes new files in a shared folder get the right ACL automatically.
sudo setfacl -R -d -m g:media:rwX /srv/media -
Starts a shell that already has a group you were just added to.
newgrp media -
Takes one user out of one group without touching other memberships.
sudo gpasswd -d alice docker -
Shows why adding a group always needs the append flag.
sudo usermod -G users alice -
Sets how private the home folder of every new user account is.
grep -E '^(HOME_MODE|UMASK)' /etc/login.defs -
Compares the groups of the current session with the groups stored for the account.
id -
Explains the directory execute and read bits, and how 711 allows access without listing.
chmod 711 /tmp/demo -
Stops users from deleting each other's files in a folder everyone can write to.
sudo chmod 1777 /srv/dropbox -
Makes new files in a directory inherit its group, so a team can share the folder.
sudo chmod g+s /srv/shared
Permissions 8
-
Copies a folder while keeping its owners, modes, timestamps and links.
sudo cp -r /srv/site /srv/site.r -
Stores and restores owners by number, so a restore elsewhere gets the right owners.
sudo tar --numeric-owner -cpf app.tar -C /srv app -
Backs up owners, modes and ACLs of a folder tree to one text file.
sudo getfacl -R -p srv/data > /root/data-perms.acl -
Sets the default permissions for new files and directories so others get no access.
umask 027 -
Prints a file's permissions as an octal number with owner, group and name.
stat -c '%a %U:%G %n' /etc/ssh/sshd_config -
Lists all files on the system that have file capabilities set.
sudo getcap -r / 2>/dev/null -
Grants a program the right to open low ports without running it as root.
sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/myapp -
Changes the owner of only the files that currently belong to a given user or group.
sudo chown -c -R --from=1000:1000 www-data:www-data /srv/site
Modes and ownership 5
-
Prints UIDs and GIDs instead of names, so mismatched IDs become visible.
ls -ln /srv/data -
Controls the default mode of new files written by one service.
sudo systemctl edit myapp.service -
Copies the owner's permission bits to the group and clears them for others.
chmod g=u,o= notes.txt -
Gives one file exactly the mode of another file.
sudo chmod --reference=/etc/ssh/sshd_config /etc/ssh/sshd_config.new -
Copies a file and sets its mode, owner and group in one command.
sudo install -m 640 -o root -g www-data app.env /etc/app/app.env
Checking access 4
-
Opens a shell as a service account to try file access step by step.
sudo su -s /bin/bash www-data -
Tests whether another user, such as the web server, can read or write a path.
sudo -u www-data test -r /srv/site/config.php && echo can read || echo blocked -
Prints the owner and mode of each folder in a path to find where access stops.
namei -l /var/www/html/uploads/photo.jpg -
Shows how to spot extra access rules that ls -l hides behind a plus sign.
ls -l report.odt # -rw-r-----+
Capabilities and flags 2
-
Lists the Linux capabilities of the current shell, including inside a container.
capsh --print -
Locks a file against any change, including by root, until the flag is removed.
sudo chattr +i /srv/app/settings.conf
Logs and diagnostics 2
-
Finds AppArmor denials in the kernel log when chmod and chown look right.
sudo journalctl -k --since '10 min ago' | grep 'apparmor="DENIED"' -
Let logrotate rotate logs in a folder that a normal user or app can write to.
rotate 4
Nothing in this filter yet.