0

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?

the paul
  • 8,972
  • 1
  • 36
  • 53
whizzzkid
  • 1,174
  • 12
  • 30

1 Answers1

2

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.

the paul
  • 8,972
  • 1
  • 36
  • 53
  • this does not work for multiple things... like let's say gsettings... ideally this should have worked for user whizzzkid: sudo su "whizzzkid" -c gsettings set org.gnome.desktop.background picture-uri "file://". But it fails with permission denied to dconf. – whizzzkid Feb 24 '14 at 17:56
  • 1
    I answered your question as asked; you said what the script did was irrelevant and that all would be solved if the commands could be run without root permissions. Note the caveat, though: "if you have no special requirements around how the environment is preserved". The gsettings tool does depend on certain environment variables being intact. See some of the answers at http://stackoverflow.com/questions/10374520/gsettings-with-cron for clues on how to proceed. – the paul Feb 25 '14 at 18:11