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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With