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?


sudo -u <username> <command>

  • 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 '16 at 1:14
  • Yes, use sudo -u inside the script. If script is running under root - you will need no pass for any user. Feb 19 '16 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 '18 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.