Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

All I need to do is to run a specific script as a particual user who does have the nologin/false shell indicated in /etc/passwd.

I would run the script as root and this should run as another user. Running :~# su -c "/bin/touch /tmp/test" testuser would work but I need a valid shell for the testuser. I know I can disable the password with passwd -d testuser and leave the shell to /bin/bash this way would secure a little bit but I need to have nologin/false shell.

Basically what I need is what crontab does when we set jobs to be running as a particular user, regardless this one has nologin/false shell.

p.s I found this thread Executing a command as a nologin user but I have no idea how to "concatenate" the command su -s /bin/sh $user to the script I need to run

Thanks

share|improve this question

You can use the -s switch to su to run a particular shell

su -s /bin/bash -c '/path/to/your/script' testuser
share|improve this answer
    
What if su is root:root -rwsr-x--- ? – Patryk Mar 10 '15 at 0:24
    
What if the user doesn't have a password? – CMCDragonkai May 18 '15 at 10:12
    
@CMCDragonkai That is left as an exercise for the student. – Wesley May 18 '15 at 18:41
2  
@Wesley Found out that you need to be root to run that command for passwordless user. – CMCDragonkai Aug 16 '15 at 10:53
    
@lain, If the user is supposed to not have a shell, the process related to the command should not be a child of a bash process. Thus, you should also use exec to replace the shell with the script. And if you like to make the script a child of init, you just use &, as an example su -s /bin/bash -c 'exec /path/to/your/script &' test – Facundo Victor Apr 27 at 12:59

You can do this with sudo -u if you have it installed:

# whoami
root
# sudo -u apache whoami
apache
# getent passwd apache
apache:x:48:48:Apache:/var/www:/sbin/nologin
share|improve this answer
    
How do you pass parameters to sudo -u apache whoami? – CMCDragonkai May 18 '15 at 10:11
    
@CMCDragonkai Any parameters after the command will be passed along. You can think of it like "sudo [-u apache] [whoami <param1> <param2>]". – Handyman5 May 19 '15 at 6:27
    
I think using sudo -u is probably not a good idea for running commands as a nologin user, because it exposes the SUDO_USER in the environment who is the real one invokes the command. Maybe I am just overthinking it. – Meow Apr 30 at 15:36

just realized :

su -s "/bin/bash" -c "/bin/touch /tmp/testuser" testuser

maybe there is a better way ?!

share|improve this answer

By providing the script as the argument to execute to /bin/sh:

su -s "/bin/sh /your/script/location" username
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.