Bash Fu #1

Jeff Mancuso June 23rd, 2008

Ever need execute a command in the context of another user while running as root? Like running sudo from an unpriviledged account – except the other way around.

I had always thought ‘su‘ stood for “super user”, but as it turns out it actually is an acronym for “substitute user” – and it can do exactly what we want.

To execute a command in the context of another user, while running as superuser, you can run su -c, followed by the command, followed by ' - user'.

For example, if I wanted to make a script to pull in the contents of a git repository that is stored in the context of user ‘www’ I could run

su -c \"git --git-dir='/projects/www/.git' pull\" - www

For some more info on su check here

2 Responses to “Bash Fu #1”

  1. Steven McGrath Says:

    You do realize that sudo can run something from ANY user. In OSX as long as your an admin user, you have full rights to do anything with sudo.

    sudo -u USERNAME

  2. Jeff Mancuso Says:

    Steven – thanks for that bit, I didn’t know about that parameter

Leave a Reply

Subscribe:

Add to Google
RSS
Try ExpanDrive

If you’ve heard of SSH then you need ExpanDrive.