0

I know that title was a little ambiguous. Allow me to explain our issue. We designed an installer script on Ubuntu since Debian based systems are our primary core, however, some of our cloud based systems and some of our customers use non Debian based systems. As a result, our script is failing in a few critical places since we used sudo. I know the answer to our question is very basic, I just don't know the exact syntax I should be using to be sure we support a fairly large number of common distributions (CentOS, RHEL, Fedora, Ubuntu, Debian, etc)

Ok so here is the issue. Our script is an installer for our software. The script installs the required files, services, etc. The install script must run as root, so the user must either be at a root console, or use sudo. Either way, everything is good up till this point. At the end of the script, the installer MUST drop out of the root console and start the service as the software's user. To do this, when I first wrote the script, I used the following:

usermod -aG sudo <username>                   #Put the softwares user in the sudo group
sudo -u <username> service <software> start   #Drop to the user and start the service

Of course as soon as I tried to deploy this on our non Debian based systems, the script errored out since CentOS didn't understand sudo. The solution is NOT to enable sudo as this may also be true on some of our customers systems in which we don't have the ability to turn this on and off. So naturally I started looking into a way to do it with the older su command. I tried the following but it either errors out or drops out of the script immediately after changing users:

su <username>
service <software> start

Any suggestions on the correct syntax so I can make this work on most Linux distributions?

Thanks

* UPDATE *

I tried the following:

su <username> -c 'service <software> start'

The command would run, but fail because the user does not have root privileges. It specifically says it cant find the service command (again no root). So why does the above sudo command work? Does it run as the user, but with root privileges?

  • just a note: an installer should never add a regular user to group sudo. Group sudo is meant for admins only. – mviereck Mar 7 '18 at 23:20
0

You should pass the command directly to su:

su <username> -c 'service <software> start'

If you simply execute the line su <username> then you start an interactive shell as that user.

It's a bit strange that starting the service should be done as the specified user; it's more usual that the start script takes care of running whatever as the required user so that root can simply start the service without having to resort to sudo or su.

  • This sorta works. The su option works, but I cannot issue the service command as a regular user which is strange because it used to work fine using the following: sudo -u <username> service <software> start – Atomiklan Feb 18 '15 at 18:12
  • I guess using sudo, the service is started with the <username> user, but with root privileges? – Atomiklan Feb 18 '15 at 18:13
0

Your need is an excellent use case for Ansible. It's an open source configuration mgmt / orchestration tool that will solve this issue for you. It is agentless and works over SSH so you typically don't need to do any client setup.

  • Thanks for the tip, and this looks great, but adds too much complexity for what we need. This should be a very simple problem for us to resolve. No need to add huge overhead. Thanks though! – Atomiklan Feb 18 '15 at 18:19
0

You didn't read the su manpage! The desired username must always be at the end.

su -c 'service <software> start' <username>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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