For sites that may have special PAM setups that make this difficult, you can bypass su/sudo entirely with systemd
and a little more typing:
- Define a
oneshot
service that executes your script. If your script is only a few commands, you can use one or more ExecStart
with commands instead of executing an external file that contains those same commands.
/home/myuser2/myscript.service
[Unit] Description=My Script [Service] Type=oneshot User=myuser2 ExecStart=/full/path/to/my/script.sh [Install] WantedBy=multi-user.target
- Define a Polkit rule that lets another unprivileged user start the service
/etc/polkit-1/rules.d/my-script.rules
polkit.addRule(function(action, subject) { if (action.id == "org.freedesktop.systemd1.manage-units" && subject.user == "myuser1") { if (action.lookup("unit") == "myscript.service") { var verb = action.lookup("verb"); if (verb == "start" || verb == "stop" || verb == "restart") { return polkit.Result.YES; } } } });
- Enable the service:
systemctl enable /home/myuser2/myscript.service
Now myuser1
can do systemctl start myscript.service
which will ask systemd to execute the script (as myuser2
). The service can be debugged as usual with systemctl status
and journalctl
.