Question

I am writing a quick shell script that does something with display settings, but that's irrelevant. The shell script is to be put in any directory where it can be accessed; I chose /usr/local/bin/<dir>/<script>.

I used a postinst script to create relevant shortcuts and register this script as a startup app. Once configuration is complete, I want /usr/local/bin/<dir>/<script> to be executed. This seems to execute (as it generates the verbose data) but there are no visual changes as expected (display settings do not take effect). I am expecting the changes to be done for the root user. After installation exits I can run /usr/local/bin/<dir>/<script> to get the desired effect, but sudo /usr/local/bin/<dir>/<script> does not produce the same effect, as it is making changes to the root's display settings.

It could be simply solved, if the commands inside postinst can be run WITHOUT root permissions. How can I achieve this?

Was it helpful?

Solution

If you want to run something as a user other than root, there are a number of ways to do that. It is called "dropping privileges". If you have no special requirements around how the environment is preserved, how capabilities are inherited, or how PAM configurations are applied, then the simplest way from a shell script context is probably to use /bin/su. For example,

su $someuser -c /usr/local/bin/$dir/$script

See man su for more information on that tool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top