sudo su with user name, password and command
I've got two answers for you.
The first answer is "don't do it". There is, almost certainly, a better way. You can specify to sudo that certain users can perform certain commands without entering a password. In all likelihood, that is what you want to do.
Having failed to convince you, however, I will let you in on a little secret. sshpass
works on sudo
, so:
sshpass -p 'my password' sudo -S su -c some-user ./some_command
Of course, while there, we can cut the su
middle man:
sshpass -p 'my password' sudo -S -u some-user ./some_command
How to pass the password to su/sudo/ssh without overriding the TTY?
For sudo there is a -S option for accepting the password from standard input. Here is the man entry:
-S The -S (stdin) option causes sudo to read the password from
the standard input instead of the terminal device.
This will allow you to run a command like:
echo myPassword | sudo -S ls /tmp
As for ssh, I have made many attempts to automate/script it's usage with no success. There doesn't seem to be any build-in way to pass the password into the command without prompting. As others have mentioned, the "expect" utility seems like it is aimed at addressing this dilemma but ultimately, setting up the correct private-key authorization is the correct way to go when attempting to automate this.
How to enter sudo password if there are several commands on the same line in bash console
You could reverse it and execute the script explicitly as your normal user
sudo sh -c 'sudo -u $SUDO_USER test.py && shutdown now'
How to sudo su; then run command
Unless you have an unusual setup, you can't normally string su
with other preceding commands like that. I would imagine it is running sudo su
, then hanging in the root environment/session, because it's waiting for you to exit before preceding to the pm2
commands. Instead, I would consider something along the lines of this using the -c
option:
CMD="sudo su -c 'pm2 restart 0; pm2 restart 1'"
ssh -i somepemfile.pem ubuntu@1.1.1.1 "$CMD"
As suggested in another answer, it would also probably be useful to encapsulate the $CMD
variable in quotes in the ssh
call.
pass a password to sudo su
You setup password-less ssh to localhost as user xyzuser
for abcuser
to achieve what you're trying. You'll need to add abcuser
's public key as an authorized_key for xyzuser
.
Then when you're logged in as abcuser
, you can do:
ssh xyzuser@localhost do_something_as_xyzuser
If you've blocked ssh access to xyzuser
, allow it only through loopback. If you're on linux, see your /etc/security/access.conf
or equivalent on how to do that.
How can I pass a password to the su command?
Well, the best way to do that would be a setuid-root binary that ask for the password then execute whatever command is needed but it requires knowledge you say not to possess. I'd advise in looking at sudo(1) instead.
Related Topics
Prevent Process from Killing Itself Using Pkill
Object-Oriented Shell for Linux
Shell Script Function Return a String
How Many Packets or Bytes Are in the Socket Receive Queue
I Cannot Get a Result from a Single Line Put into the Erlang Shell
How to Write-Protect Every Page in the Address Space of a Linux Process
On Linux, How to Make Sure to Unlock a Mutex Which Was Locked in a Thread That Dies/Terminates
Linux X86-64 Assembly and Printf
How to Detect a Buffer Over Run on Serial Port in Linux Using C++
Parse CSV in Bash and Assign Variables
How to Speed Up Compilation Time in Linux
List Files Recursively in Linux Cli With Path Relative to the Current Directory
Automatically Kill Process That Consume Too Much Memory or Stall on Linux
What Is File Hole and How Can It Be Used
Getting Exit Status Code from 'Ftp' Command in Linux Shell