P
P
pstbin2020-01-22 13:00:57
linux
pstbin, 2020-01-22 13:00:57

How to run a command as a different Linux user?

Good afternoon.
There is Linux (CentOS 7). There are two regular users on this Linux - "user1" and "user2".
There is a script that is located here: "/home/user2/scripts/script1.sh".
User1 can run this script by executing the command: "su user2 /home/user2/scripts/script1.sh", which will prompt the console for user2's password.
I know the password, but how can I add this password to the command so that it runs immediately, without asking for a password?
Found the option "su user2:password /home/user2/scripts/script1.sh", but it doesn't work, says like this "su: user user2:password does not exist".
Tell me how to overcome?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
Y
Yuri Samoilov, 2020-01-22
@pstbin

$echo <password> | sudo -S <command>

P
pfg21, 2020-01-22
@pfg21

use sudo.
in the /etc/sudoers config, write the necessary commands to run from another user without asking for a password.

C
CityCat4, 2020-01-22
@CityCat4

man sudo
There are some incredible possibilities there. I don’t know how for a regular user, but for root (running the program from root without asking for a password) is done like this:

# Nagios Remote Plugin Executor priviegion
nrpe            ALL=(ALL) NOPASSWD: /usr/lib64/nagios/plugins/

V
Victor Taran, 2020-01-22
@shambler81

SUID, SGID

P
pstbin, 2020-01-22
@pstbin

Thanks to everyone, the solution was found: "$ echo password | su -l user2 -c /home/user2/scripts/script1.sh"
Suddenly it will come in handy for someone else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question