Automatically execute bash script under the privileges of another user

6,507

Solution 1

So did I get that right.

User "bla" wants to run a command as User "blub" ?

Then something like this could work:

sudo -u blub /path/to/command

If you need this to be done without being asked for a password add the follwing line to /etc/sudoers (via visudo command):

bla    ALL=(blub) NOPASSWD: /path/to/command

Solution 2

you can work with SUID and SGID bit permissions (note that this may be really dangerous)

give the permissions to execute this file to a group that the user you are executing the script as belong to, and set via SUID anyone who runs this script to run it as the user who owns the script

sudo chmod 4751 /path/to/my/file
sudo chgrp somegroup /path/to/my/file

Then, you just need to execute the script as userB who is member of somegroup and it will be executed with as userA and somegroup

ls -l /path/to/my/file
-rwsr-xr-- userA somegroup /path/to/my/file
Share:
6,507

Related videos on Youtube

nikeee
Author by

nikeee

I like API-, Language- and UX-Design. I also like: Programming language internals (esp. C#, TypeScript, C and C++ implementations). Interop of managed and unmanaged code. Language specific solutions and approaches. This includes programming languages as well as spoken ones.

Updated on September 18, 2022

Comments

  • nikeee
    nikeee almost 2 years

    Is there a way to execute a bash script under the privileges of another user (automatically)? I think "sudo" is the right way, but I don't know how to automate the process.

    E.g. I want to run something like this (user "bla" runs a process under "blub"):

    sudo <the user under which i want to run the process> <path to script>