Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RPM %post as a different user

Tags:

bash

rpm

I need to run a command as a different user in the %post section of an RPM.

At the moment I am using a bit of a hack via python but it can't be the best way (it does feel a little dirty) ...

%post -p /usr/bin/python
import os, pwd, subprocess
os.setuid(pwd.getpwnam('apache')[2])
subprocess.call(['/usr/bin/something', 'an arg'])

Is there a proper way to do this?

like image 606
t0mmyt Avatar asked Oct 21 '22 08:10

t0mmyt


2 Answers

If /usr/bin/something is something you are installing as part of the package, install it with something like

attr(4755, apache, apache)   /usr/bin/something

When installed like this, /usr/bin/something will always run as user apache, regardless of what user actually runs it.

like image 105
chepner Avatar answered Nov 03 '22 21:11

chepner


The accepted answer here is wrong IMO. It is not often at all you want to set attributes to allow anyone execute something as the owner.

If you want to run something as a specific user, and that user doesn't have a shell set, you can use su -s to set the shell to use.

For example: su -s /bin/bash apache -c "/usr/bin/something an arg"

like image 34
Brian Sidebotham Avatar answered Nov 03 '22 20:11

Brian Sidebotham