Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
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/*Step by step
- **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. - One file per team in
/etc/sudoers.d/— mode0440, owned by root; the file name must not contain a dot or end with~, or sudo ignores it. - Groups, not users —
%webops; add and remove people withusermod -aG webops alice/gpasswd -d alice webops. - Full paths and exact arguments —
/usr/bin/systemctl restart nginxallows only that. A command without arguments in the rule allows any arguments. - 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. - **
NOPASSWDonly 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. - Test —
visudo -cchecks all files;sudo -l -U alicelists exactly what alice may run. - Logs — every sudo use is logged:
journalctl _COMM=sudo(or/var/log/secure,/var/log/auth.log). Send them to your central log system.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Rule file is ignored | Name contains a dot (webops.conf) or ends with ~. | Rename to webops. |
syntax error and sudo broken | File edited without visudo. | pkexec visudo or boot to rescue; always use visudo. |
| User can get a root shell | Editor, pager or interpreter allowed. | Use sudoedit; allow exact commands only. |
| Command refused though listed | Different path or extra arguments. | Use the full path from command -v; match the arguments. |
| New group member is refused | Old session without the new group. | Log out and in again. |
| Script with NOPASSWD abused | Script writable by the user. | Owner root, mode 755, in a root-owned folder. |
Go further
- Command pages: visudo, sudo, usermod, journalctl.
- Next how-tos: Create a user with sudo rights, Lock or disable a user account, See who logged in and when, Watch file changes with auditd.
- Learn it: RHCSA — sudo, RHCSA — Work as root.
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.