65

I've recently set up my server so that my suPHP 'virtual' users can't be logged into by using this article

My issue now is that before when I ran a rake command for my Ruby on Rails application running on the server, I used su to go into www-data and execute the command from there - obviously I can't do that anymore because of the nologin.

So as a root user, how can I execute commands as other user's, even if they are nologin?

2
  • One way is to launch a shell for that user (explicitly specifying the shell): sudo -u www-data bash execute your command(s) and logout
    – cyberx86
    Nov 21, 2011 at 18:21
  • @cyberx86 Your solution works a treat, could you submit it as an answer for me?
    – user82250
    Nov 21, 2011 at 19:01

2 Answers 2

76

One way is to launch a shell for that user (explicitly specifying the shell):

sudo -u www-data bash 

This will launch a (bash) shell as the specified user. You can then execute your command(s) and logout (to return to your previous shell)

0
56

As root you can use su -s /bin/sh $user — the -s option overrides the configured shell for the user.

3
  • 2
    This always seems to error for me.
    – user82250
    Nov 21, 2011 at 19:26
  • Works great for me, never had a prob. When running scripts with bash syntax needs /bin/bash rather than /bin/sh
    – ekerner
    Oct 8, 2017 at 18:52
  • 1
    This answer is a life saver in situations where you don't have sudo - such as when working in kubernetes pods Dec 7, 2021 at 18:40

You must log in to answer this question.