ubuntu, updated
Permission best practices
How to structure users, groups and permissions on a server, with recipes for a shared team folder, service accounts and sudo.
Rules of thumb
- Give rights to groups, not to users. People change, roles stay. Add and remove group members instead of changing file permissions.
- Least privilege. Give the minimum that works.
640before644,750before755for anything with data or secrets. - Never
chmod 777. If something only works with 777, the owner or group is wrong. Fix that instead. - One account per person. No shared
adminlogin. Everyone uses their own account plussudo, so the logs show who did what. - Services get their own system user, never root and never your personal account.
- Narrow sudo. Full
sudoonly for admins. Others get exactly the commands they need via/etc/sudoers.d/. - Keys instead of passwords for SSH, and no root login over SSH (Ubuntu’s default is
PermitRootLogin prohibit-password). - Review regularly:
getent group sudo, who has a shell, which accounts haven’t logged in for months (lastlog).
Recipe: shared folder for a team
Everyone in developers can read and write, new files automatically belong to the group, nobody else gets in.
sudo addgroup developers
sudo usermod -aG developers anna
sudo usermod -aG developers ben
sudo mkdir -p /srv/projects
sudo chown root:developers /srv/projects
sudo chmod 2770 /srv/projects # setgid + rwxrwx---
The setgid bit (the 2) makes new files belong to developers. With Ubuntu’s default umask (002), they are group-writable too. If someone uses a stricter umask, add a default ACL:
sudo setfacl -d -m g:developers:rwX /srv/projects
Need read-only access for another team? sudo setfacl -R -m g:auditors:rX /srv/projects plus the same with -d (see ACLs).
Recipe: service account for an app
sudo adduser --system --group --no-create-home --home /srv/myapp myapp
sudo mkdir -p /srv/myapp /etc/myapp
sudo chown -R myapp:myapp /srv/myapp # app data: app may write
sudo chown -R root:myapp /etc/myapp # config: root writes, app reads
sudo chmod 750 /etc/myapp && sudo chmod 640 /etc/myapp/*
In the systemd unit:
[Service]
User=myapp
Group=myapp
UMask=0027
Recipe: let a team restart one service
sudo visudo -f /etc/sudoers.d/myapp
%developers ALL=(root) /usr/bin/systemctl restart myapp, /usr/bin/systemctl status myapp
More in Who is sudo?.
Quick audit
getent group sudo # who are admins?
sudo ls /etc/sudoers.d/ # extra sudo rules
sudo find /srv /etc -perm -o+w -type f 2>/dev/null # world-writable files: should be none
sudo find / -nouser -not -path "/proc/*" 2>/dev/null # files without owner