Score:0

Unable to run systemd service without password despite NOPASSWD in sudoers file

cn flag

I am running Ubuntu 20.04.6 LTS and added a custom service file for systemd. I want to start/stop it without entering a password.

I checked several posts here and on other StackExchange instances but nothing works so far.

I would like to start and stop this service without having to enter a password each time. For this, I added the following lines to the sudoers file using visudo:

christian  ALL=NOPASSWD: /usr/sbin/shutdown -h now
christian  ALL=NOPASSWD: /usr/bin/systemctl start mc

However, when I try to run the systemctl command, it still prompts me for a password:

christian@server:~$ sudo /usr/bin/systemctl start mc
[sudo] password for christian:

What might I be doing wrong? How can I troubleshoot this issue?

Raffa avatar
jp flag
I think that line in sudoers should be `christian ALL=(ALL) NOPASSWD: /usr/bin/systemctl start mc` and the other line should be similarly modified as well to work.
hr flag
Do you really need to run `mc` as a *system* service? Have you considered running it as a [systemd user service](https://wiki.archlinux.org/title/Systemd/User)? that shouldn't require elevated privileges.
Apollo avatar
cn flag
@Raffa I copy pasted your suggestion (this: `christian ALL=(ALL) NOPASSWD: /usr/bin/systemctl start mc`) and it still does not work. I tried running it and it still prompts for the password. I'll try it later if a restart of the machine does the trick.
Apollo avatar
cn flag
@steeldriver good point. Here it would work, but on other instances I need to run them as root, so I'd like to get this to work first. Thanks for your suggestion.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.