Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run some commands in Debian package script from current user

How to run some commands in my installations scripts of deb-package (preinst, postinst, prerm, postrm) not from root but from current user (user that invoked the installation)?

like image 943
macropas Avatar asked Apr 24 '26 18:04

macropas


1 Answers

root is the current user. You should never expect that the installation of the package is performed by sudo

You can check the SUDO_USER environment variable for the user that may have invoked the script, if the installation was performed from the context of a simple sudo dpkg -i - sudo may not have been installed, so the installation may have been performed by su, which means that the environment variable will not be set.

If you want to invoke scripts as that user you just invert the sudo - viz:

sudo -u $SUDO_USER -c <command to invoke>

but you need to ensure that you properly know the user that invoked the script - i.e. SUDO_USER could be root!

Typically, though, because you can have an arbitrary number of users on a Linux system, you should not do something like this, as it will leave the system in a state where only one user can use the package. You should create state/configuration on first invocation as the ordinary user.

Finally, don't expect a GUI, don't expect a terminal on the installation if it's just being shipped as a dpkg.

like image 54
Petesh Avatar answered Apr 28 '26 12:04

Petesh