I would like to execute a script as a normal user and execute a command that shuts off apache (which needs a root password).

I was wondering if is possible to run the script with sudo but it executes some of the commands with an specific user and executes a specific command as a root.

How can I achieve this?

2 Answers 2


sudo -u <username> <command>

  • 1
    Inside of the script? and also, how passwords are going to be managed? I mean for admin and for that specific user?
    – VaTo
    Feb 19, 2016 at 1:14
  • 1
    Yes, use sudo -u inside the script. If script is running under root - you will need no pass for any user. Feb 19, 2016 at 1:22
su -c 'command' username

Been done this way since long before we started using sudo. Both methods work. Pick your poison.

  • Very thanks, I use this to make a script to disable some services #! /bin/bash sudo -u wachin systemctl disable snapd.autoimport.service sudo -u wachin systemctl disable snapd.core-fixup.service sudo -u wachin systemctl disable snapd.seeded.service sudo -u wachin systemctl disable snapd.service sudo -u wachin systemctl disable snapd.system-shutdown.service Sep 26, 2018 at 23:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .