Score:1

Linux: Run a specific command as another user?

hu flag

I want to be able to allow users to run a specific command as another user, is this possible?

I have a script that reads a remote file through ssh to get a status, something like:

#!/usr/bin/sh

state=$(ssh -q -i $HOME/.ssh/id_rsa" auto@remote-host "[ -f /path/to/state/file ] && cat /path/to/state/file 2>/dev/null")

printf "${state}"

The auto user has a key setup so that it can login to the remote machine without a password. I want users to be able to run this script without needing to worry about setting up their own key, for example:

bob@local-machine$ /path/to/state-check

So, the bob user can run the script as the auto user without hassle. I know you can use something like

su auto -c "/path/to/state-check"

But that would still require access to the auto user's password or sudo. Is there a way to set up a file that allows bob to run specific commands as auto passwordless? Or should I redesign the tool so that the state files are fetched and stored locally? I'm not really sure of a good way to do this. I'm running this on RHEL 9 machines.

us flag
consider setting auto up as a service on the remote server instead of relying on ssh.
djdomi avatar
za flag
sudo and su can do this, while sudo can be restricted to Only allow one command
F. Hauri avatar
cn flag
**1.** Simply remove `auto@` from your command line. `ssh` will use *logname* as user for connection. **2.** Or use `ssh ... $USER@...`.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.