Run command as Linux "system" user (shell = /bin/false)

29,607

Solution 1

I use su - targetuser -s /bin/bash from a root shell.

For direct command execution use -c:

su - targetuser -s /bin/bash -c "/bin/echo hello world" 

Solution 2

Use sudo. This will work even if the user doesn't have a real shell.

Share:
29,607

Related videos on Youtube

ceyko
Author by

ceyko

Updated on September 18, 2022

Comments

  • ceyko
    ceyko over 1 year

    I created a "system" user in Ubuntu 11.04 (adduser --system) for running certain cron jobs, but sometimes I want to test things out by manually running commands as that user. What's the easiest way to do this?

    su doesn't work, because the user has /bin/false as its shell (which is fine for cron). I've been manually changing the shell to /bin/bash to do my testing and then changing it back again, but I wonder is there an easier way?

  • ceyko
    ceyko almost 13 years
    That works, thanks! How did I miss the -s option?
  • dmourati
    dmourati almost 13 years
    you can also tack on a -c "your_command_here" option to pass the command you'd like to run versus waiting for the shell and then typing it at a new prompt.