brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: stat -c prints the permissions as numbers

Permissions
~/tips/tip-linux-stat-format

stat -c '%a %U:%G %n' /etc/ssh/sshd_config

Prints a file's permissions as an octal number with owner, group and name.

ls -l gives permissions as letters. Sometimes you want the octal number to put in a chmod, and converting rwxr-x— in your head is silly.

Ask stat for exactly the fields you want:

stat -c '%a %U:%G %n' /etc/ssh/sshd_config

That prints something like 644 root:root /etc/ssh/sshd_config: octal mode, owner, group and name. Add a glob and you get one line per file, which helps when comparing a directory against what it should be. Other useful codes are %s for size in bytes and %y for the last modification time.

The caveat: -c is the GNU version. On macOS and the BSDs, stat uses -f with different codes, so a script that works on your Linux box will fail there.

I like this for audits. Print the numbers for a handful of sensitive files, such as SSH keys and config files with passwords, and look for anything that is world-readable. Read the full list of format sequences in the man page, there are more than most people expect.

Further reading: stat man page, On this site: umask decides the permissions of every new file, On this site: setfacl gives one extra user access without changing the group

More Permissions tips sudo su -s /bin/bash www-data sudo setfacl -R -d -m g:media:rwX /srv/media sudo cp -r /srv/site /srv/site.r ls -ln /srv/data newgrp media All Permissions tips (30) All topics

Link