Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 213Security · sudo

How to write sudo rules safely

"Just give them sudo" usually means full root for everybody. Good sudo rules give each team exactly the commands it needs — restart nginx, read its logs — and nothing more, with every use logged. But some "small" rules are full root in disguise: allowing an editor, less, find or a script the user can change. This how-to writes a rule file per team with visudo, tests it, and avoids the common escapes.

  • 5 Lessen
  • 15 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

213.1

The quick answer

/etc/sudoers.d/webops
Cmnd_Alias NGINX_CTL = /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx, /usr/bin/systemctl status nginx
Cmnd_Alias NGINX_LOG = /usr/bin/journalctl -u nginx, /usr/bin/journalctl -u nginx -n 200 --no-pager
%webops ALL=(root) NGINX_CTL, NGINX_LOG
%webops ALL=(root) sudoedit /etc/nginx/sites-available/*
visudo -f /etc/sudoers.d/webops
visudo -c
sudo -l -U alice

213.2

Step by step

  1. **Always edit with visudo** (quick answer) — it checks the syntax before saving. A broken sudoers file can lock everybody out of sudo. Keep a root shell open while you change rules.
  2. One file per team in /etc/sudoers.d/ — mode 0440, owned by root; the file name must not contain a dot or end with ~, or sudo ignores it.
  3. Groups, not users — %webops; add and remove people with usermod -aG webops alice / gpasswd -d alice webops.
  4. Full paths and exact arguments — /usr/bin/systemctl restart nginx allows only that. A command without arguments in the rule allows any arguments.
  5. Avoid shell escapes — these give a root shell: editors (vi, nano), pagers (less, more), find -exec, awk, tar, git, interpreters, systemctl edit, and any script the user can write. For editing files use **sudoedit** (quick answer, line 4): the user edits a copy as themselves, sudo writes it back.
  6. **NOPASSWD only for narrow rules** — for automation (deploy ALL=(root) NOPASSWD: /usr/local/bin/deploy-app), and the script must be owned by root and not writable by others.
  7. Test — visudo -c checks all files; sudo -l -U alice lists exactly what alice may run.
  8. Logs — every sudo use is logged: journalctl _COMM=sudo (or /var/log/secure, /var/log/auth.log). Send them to your central log system.

213.3

Check that it worked

visudo -c reports parsed OK for every file; sudo -l -U alice shows only the nginx commands; as alice sudo systemctl restart nginx works, while sudo systemctl restart sshd and sudo -i are refused (Sorry, user alice is not allowed …) and logged.

213.4

Common problems

ProblemCauseFix
Rule file is ignoredName contains a dot (webops.conf) or ends with ~.Rename to webops.
syntax error and sudo brokenFile edited without visudo.pkexec visudo or boot to rescue; always use visudo.
User can get a root shellEditor, pager or interpreter allowed.Use sudoedit; allow exact commands only.
Command refused though listedDifferent path or extra arguments.Use the full path from command -v; match the arguments.
New group member is refusedOld session without the new group.Log out and in again.
Script with NOPASSWD abusedScript writable by the user.Owner root, mode 755, in a root-owned folder.

213.5

Go further

1Why is allowing vi or less with sudo dangerous?

Both can start a shell, and that shell runs as root — it is the same as giving full root access.

2What does sudoedit do?

The user edits a temporary copy as themselves, and sudo copies it back as root, so the editor never runs as root.

Alle commando's in dit hoofdstuk