I have a requirement to allow non-root users to start and stop a service. It was recommended to me to use PolicyKit rather than sudoers.d, which I am familiar with.
As I have no experience with PolicyKit, I thought I would experiment and create a rule to allow non-root users to start and stop the Docker service. I have created a file, /etc/polkit-1/rules.d/10-docker.rules containing:
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.systemd1.manage-units" &&
action.lookup("unit") == "docker.service")
{
return polkit.Result.YES;
}
})
However, whenever I execute systemctl start|stop|restart docker.service, I keep getting prompted for a password. What am I missing?
Also, I would like to limit non-root users to control this service who are in a specific group e.g. blah. How do I incorporate this into my rule?
My target OS is RHEL 7.7.
On CentOS7,
actiondoes not have access to theunitinformation. This was introduced on a later systemd version, v226.https://github.com/systemd/systemd/commit/88ced61bf9673407f4b15bf51b1b408fd78c149d
I was also hit by this. You will need to allow the user to manage all units or go back to the stone age of having shell scripts on sudoers.
Use
subject.isInGroup("group").See: