Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's a good way of running a shell script as a different user. I'm using Debian etch, and I know which user I want to impersonate.

If I was doing it manually, I would do:

su postgres
./ /tmp/test

Since I want to automate the process, I need a way to run as postgres (inheriting the environment, etc)


share|improve this question
up vote 38 down vote accepted

To run your script as another user as one command, run:

/bin/su - postgres -c "/path/to/ /tmp/test"

Breaking it down:
 /bin/su : switch user
 - : option to su, make it a login session (source profile for the user)
 postgres : user to become
 -c "/path/to..." : command to run

I recommend always using full paths in scripts like this - you can't always guarantee that you'll be in the right directory when you su (maybe someone changed the homedir on you, who knows). I also always use the full path to su (/bin/su) because I'm paranoid. It's possible someone can edit your path and cause you to use a compromised version of su.

share|improve this answer
It will always require me type in password ? How can get round it ? – zjffdu Mar 29 '12 at 7:15
It will always require you to type in a password if you are running the command as a non-root user. If you want to avoid the password, you can configure sudo to allow that. HOWEVER - configuring sudo to allow a user to run su allows them to become any user. I would suggest creating a script for your command, setting the script permissions to 700 and owned by root, then configuring sudo to allow a user to run that single script. – baumgart May 22 '12 at 15:39
I believe - while this answer might work for the OP - it is not entirely correct. To my knowledge, using both - (or --login) together with --command, -c doesn't actually start a login session, because -c always forces a non-login shell. – JeanMertz May 1 '13 at 13:46

If the target user to run has nologin shelll defined, then you can use the -s option to specify the shell:

/bin/su -s /bin/bash -c '/path/to/your/script' testuser

See the following question: run script as user who has nologin shell

share|improve this answer

If you want to automate this on a schedule you could put it in the user's contrab. Cron jobs won't get the full environment though, but it it might be better to put all the env variables you need in the script itself anyways.

To edit the user's crontab:

sudo crontab -u postgres -e
share|improve this answer
Could you please explain more? – saravanakumar May 26 at 15:23

This should be an informative read -- setuid on shell scripts

If you run su with a "- username" argument sequence, it will make a login shell for the user to give the same environment as the user. Usually, used to quickly execute your script with your home environment from a different login.

share|improve this answer
This can be useful, if you need to perform a series of actions. But bear in mind that most system service accounts shouldn't have valid home paths and shells. – Dan Carley Jul 23 '09 at 10:59

Try the su manpage:

su -c - postgres

Alernately, you could use sudo to allow you to run just that comman as postgres without a password. It takes some setup in your /etc/sudoers, though.

share|improve this answer

The "su -c ... " method posted by others is a good one. For automation, you could add the script to the crontab of the user you need it to execute as.

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.