Start, restart and stop small service as unprivileged user

I’v wrote a very small script that stream my favorite radio station the whole time the computer runs to avoid one minutes of advertisement on every reconnection to the server of the radio station.
This script runs as systemd service which is fairly stable, but if there is an interruption of the internet of the streaming server, I have either to restart the computer or to restart the service with root rights. So I was searching for a solution that gives me the possibility to control this service as unprivileged user (explicit no su, sudo or whatever).
I found something in Google, but nothing is working:

You can restart a system-wide systemd service as a non-root user at any time by configuring sudoers via visudo to allow that specific user to run systemctl restart.

Setting Up Permitted Access

  1. Open the sudo configuration file safely by running:

    bash

    sudo visudo
    
    

    Verwende Code mit Vorsicht.

  2. Add a rule granting the specific user rights to restart your exact service without typing a password (or requiring their own password):

    text

    username ALL=(ALL) NOPASSWD: /bin/systemctl restart myservicename.service
    
    

    Verwende Code mit Vorsicht.

  3. Replace username with your actual Linux user and myservicename.service with your target service name.

Restarting the Service Anytime

Once configured, the user can run this command from their own session at any moment:

bash

sudo systemctl restart myservicename.service

This is my /etc/sudoers:

## User privilege specification

root ALL=(ALL:ALL) ALL

hk ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart vlc-Server.service

Is there a possibility to get this to work, or would be the suid-bit an option, I tried this on my script without success. I had never success with this suid-bit, it never worked even I’ve studied the usage of it.

Is there any specific reason why the streaming script runs as root? If you can run it as user, you could set up a systemd script to start it at login, and restart it with the following command:

systemctl --user restart myservicename

systemd user services/timers etc are stored in ~/.config/systemd/user/, and are enabled/disabled etc in the same way as system services. You just need to add --user to the command and omit the sudo.

Edit 2026-08-30T11:45:00Z:

You might find it easier to add a dropin file to the /etc/sudoers.d/ directory rather than modify the /etc/sudoers file. This command should do it:

echo "$USER ALL=(ALL) NOPASSWD: /bin/systemctl restart myservicename.service" | sudo tee "/etc/sudoers.d/restart-myservicename-without-sudo"

That would still use sudo, it just wouldn’t ask for a password.

As @scotty65 stated, if you want to run it as a normal user then make a user service :down_arrow: (unless you need root for your script).

Thank you for your answer. User service is not possible for what reason ever, when I start the script (not as service I get a suspicious error message:

Temporary file /usr/bin/sediU623N cannot be opened: No permission

I’ve tried you suggestion to create a file in /etc/sudoers.d/ (with your command), but I will be still prompted to enter my password when I invoke systemctl restart vlc-Server.service.

Here the content of /etc/sudoers.d/restart-vlc-Server-without-sudo:

[linux hk]# cat /etc/sudoers.d/restart-vlc-Server-without-sudo 

hk ALL=(ALL) NOPASSWD: /bin/systemctl restart vlc-Server.service

Or is it impossible because its as script and not a binary executable. I’ve just found this here in regard to SUID-Bit:
SUID doesn’t work with scripts
So I assume that I have more or less the same problem.

Mod edit: Consecutive posts merged.
If there are no intervening replies, please edit your previous post instead to add updates; add @mentions where needed. Cheers!
:wink:

What command are you using within the systemd service to play the audio stream?

mpg123 has --loop option to reconnect an audio stream if it drops out

cvlc has -R --repeat options to reconnect a single stream

I don’t like the look of that, but maybe that’s because I don’t know what your script is actually doing.

That’s nonsensical, sudoers and sudoers.d are for configuring sudo.

As I said, it would only allow you to run that command via sudo without entering a password. You still need to use sudo.

sudo systemctl restart vlc-Server.service

Perhaps it would be best to share your script so we can see what’s going on, and advise appropriately.

Thank you so much. This solution is just great. I did some tries with disconnecting the network cable and it came always back.

Nothing should ever write to /usr except for the package manager, and it is certainly not the proper place to store temporary files. Temporary files should go under /tmp.


This is wrong. Spaces on that line are interpreted as separators for the multiple commands you are allowed to execute. Therefore, that whole command should be enclosed in quotes.

Did someone perused the script?

OP didn’t find it necessary to show us the contents of the script. :man_shrugging: