0

Is it possible to call from a bash script, run as sudo, another script with normal user privileges?

Another option would be to run a specific command as a normal user but the whole script would be run as superuser.

P.S. What I want to do is to customize the Launcher from within an installation script that has to be run as sudo.

  • @Zanna Isn't that the other way around, to increase privileges? – Henning Kockerbeck Nov 13 '16 at 11:14
  • @Zanna Ah, I see. But there's a catch to sudo -u username command, see my answer below. So just su -u username -c command might be worth a look. – Henning Kockerbeck Nov 13 '16 at 11:32
  • @HenningKockerbeck nice, upvoted :) – Zanna Nov 13 '16 at 11:33
1

There are multiple ways. If you just have to execute one command without any parameters, you can use sudo -u <username>

#!/bin/bash
whoami
sudo -u otheruser whoami
whoami

But if you need to execute multiple commands, a commands with parameters or anything with whitespace in it, that method doesn't work.

#!/bin/bash
ls /some/dir
sudo -u otheruser ls /some/dir # not working as expected
ls /some/dir

It also doesn't help to quote the command like sudo -u otheruser "ls /some/dir".

In those cases, you can just plainly switch the user with su

#!/bin/bash
whoami; ls /some/dir
su otheruser -c "whoami; ls /some/dir"
whoami; ls /some/dir
  • I can't use the "-u" parameter in su – Joe Nov 13 '16 at 11:49
  • It doesn't work, I tried it but it seems that has no effect – Joe Nov 13 '16 at 17:28
  • @Joe I just tested the example script with su I outlined above again, it does exactly what it's supposed to do. What's the output of whoami on the one hand and su otheruser -c "whoami" on the other hand? If you start the script with sudo, the first one should give "root", the second one the name of the non-privileged user (in the example, "otheruser"). – Henning Kockerbeck Nov 13 '16 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.