Using su with commands

4

Ubuntu Server 12.04

On a remote server that I ssh into I have set up a special user with:

sudo adduser --system --disabled-login --disabled-password --group suser

to run some maintenance functions. On occasions I su into a login session for suser with:

sudo su -l suser -s /bin/bash

to do some tasks... however I particularly want to be able to run some git commands via:

sudo su suser -c <COMMAND>

from my real user and no login to the suser.

Unfortunately I haven't been able to determine HOW I go about that. I have tried the only ways that makes sense to me:

sudo su suser -c git clone https://example.com/repos/project.git
sudo su suser -c "git clone https://example.com/repos/project.git"
   or even
sudo su suser -c mkdir /home/suser/test
sudo su suser -c "mkdir /home/suser/test"

but nothing happens and I return to prompt. Can anyone assist or explain why this isn't possible?

JDex

Posted 2013-06-19T11:36:39.643

Reputation: 43

Answers

5

The problem is that suser was created with /bin/false as its default shell. Therefore, when you try to run commands as suser through sudo, the system attempts to run them using /bin/false/ which is not a real shell and fails. You can either set a shell for suser or you can specify it on the command line when you run sudo. Alternatively, you can use sudo's -u option.

  1. Use -u:

    sudo -u suser mkdir /home/suser/foo
    

    This works because by default, sudo uses /bin/bash (or whatever you have set the default $SHELL to be). Therefore, it will execute a command as suser but using bash, so the command is correctly executed.

  2. Set suser's default shell:

    sudo chsh suser 
    

    Enter /bin/bash (or whatever you prefer) in the prompt that will appear. You should now be able to launch commands as suser:

    sudo su suser -c "mkdir /home/suser/test"
    
  3. Set the shell explicitly:

    sudo su suser -s /bin/bash -c "mkdir /home/suser/test"
    

terdon

Posted 2013-06-19T11:36:39.643

Reputation: 45 216

That's it... facepalm I was under the mistaken impression that since I wasn't doing -l, that I was keeping the current shell and environment. Thank you! – JDex – 2013-06-19T12:34:23.903