Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was wondering how to run a command as another user from a script.

I have the script's owner set as root. I also have the following command being run within the script to run the command as the hudson user:

su -c command hudson

Is this the correct syntax?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Yes. Here's the --help:

oli@bert:~$ su --help
Usage: su [options] [LOGIN]

  -c, --command COMMAND         pass COMMAND to the invoked shell
  -h, --help                    display this help message and exit
  -, -l, --login                make the shell a login shell
  -m, -p,
  --preserve-environment        do not reset environment variables, and
                                keep the same shell
  -s, --shell SHELL             use SHELL instead of the default in passwd

And some testing (I used sudo as I don't know the password for the nobody account)

oli@bert:~$ sudo su -c whoami nobody
[sudo] password for oli: 
share|improve this answer

Note: "I have the script's owner set as root" doesn't do anything; even if you have the setuid bit set it still doesn't work

Assuming you're actually running the script as root, however, you can use sudo. su is primarily for switching users, while sudo is for executing commands as other users. The -u flag lets you specify which user to execute the command as:

sudo -u hudson command
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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