55

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
./backup_db.sh /tmp/test
exit

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

Thanks!

Wadih M.
  • 962
  • 1
  • 9
  • 17

8 Answers8

88

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

/bin/su -c "/path/to/backup_db.sh /tmp/test" - postgres

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

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.

baumgart
  • 2,423
  • 18
  • 17
  • It will always require me type in password ? How can get round it ? – zjffdu Mar 29 '12 at 07:15
  • 3
    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 01 '13 at 13:46
  • 1
    Note: for portability, the `- postgress` should appear at the end of the command. From the man page: `When - is used, it must be specified before any username. For portability it is recommended to use it as last option, before any username. The other forms (-l and --login) do not have this restriction.` – jonny May 02 '17 at 14:45
  • will this work on `www-data` user ? – Chang Zhao Aug 30 '21 at 21:56
  • Yes, it will work for any user. – baumgart Sep 22 '21 at 18:29
27

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

Basil A
  • 1,910
  • 2
  • 17
  • 18
10

To automate this on a schedule you could put it in the user's crontab. 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
Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
3

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.

ADJenks
  • 113
  • 4
nik
  • 7,040
  • 2
  • 24
  • 30
  • 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
2

Try the su manpage:

su -c script_run_as_postgres.sh - 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.

pjz
  • 10,497
  • 1
  • 31
  • 40
2

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.

Geoff Fritz
  • 1,717
  • 9
  • 11
1

You can also use:

sudo -u postgres script_run_as_postgres.sh

Xandersoft
  • 21
  • 1
1

If the user already has an entry to sudo and you don't know superuser's password then you can try following: This one restarts the postgres initialized at /data/my-db/pgsql/9.6/data

sudo su - postgres -c "/usr/pgsql-9.6/bin/pg_ctl -D /data/my-db/pgsql/9.6/data -l /var/log/pgsql.log restart"
smishra
  • 111
  • 2