Skip to main content

So you want to run a command as another user while you're logged in as root, huh? No worries, you can use the su or sudo command for that.

Using su

The su command allows you to switch to another user. To run a single command as another user, you can use it like this:

su - username -c "command"

For example, if you want to run ls as the user john, you'd do:

su - john -c "ls"

Using sudo

Alternatively, you can use sudo to execute a command as another user:

sudo -u username command

For example, to run ls as john:

sudo -u john ls

Just make sure that the user you're switching to has the necessary permissions to run the command you're interested in.