Allow starting, stopping and restarting a system service as a specific user (without su password)

In this example we are going to allow the system user “minecraft” to start, stop and restart the system services mincecraft, minecraft2 and minecraftpe

I’ve tested this in CentOS7 and Debian 9, it may be slightly different to control services in other disto’s

Normally only root can control services unless the user is part of the su group or specific service control has been setup in a file created in the /etc/sudoers.d directory

You will either need to be logged in as root or use su to issue the following commands

Let’s start by adding the minecraft user to the sudo group

usermod -aG sudo minecraft

Now we will create the file in the sudoers directory using visudo, this will define which services the minecraft user can use without su password

visudo -f /etc/sudoers.d/minecraft

Next, add the following (change the service names to meet your specific requirements)

minecraft ALL = NOPASSWD: /bin/systemctl stop minecraft.service, /bin/systemctl$ start minecraft.service, /bin/systemctl start minecraft2.service, /bin/systemctl stop minecraft2.service, /bin/systemctl start minecraftpe.service, /bin/systemctl stop minecraftpe.service

Now we need to restart the sudo service

service sudo restart

Assuming you have already created the relevant services to be controlled, log in as your user and you should have full control of your service(s) with

sudo systemctl start minecraft
sudo systemctl stop minecraft
sudo systemctl reload minecraft
Print Friendly, PDF & Email

More Like This


CentOS Ubuntu / Mint


  • Post a comment