sudo -S sh -c "bash" <passwordfile returns immediately

spraff asked:

I have a script which opens terminal windows and sends commands to them. Some of these had to run as root so I would send them this command

sudo sh -c 'do_stuff && bash'

This would run some initial commands and then drop into an interactive shell.

In my environment, the terminal programs themselves cannot be started by the root user (yes, really) and it actually makes sense to store the root password in a file (yes, really) and launch several such terminals without asking for a password each time.

This is a silly situation, but roll with it. Please don’t tell me I’m doing the wrong thing, I don’t have any other choice in this environment.

If I send them this command

sudo sh -S -c 'do_stuff && bash' <passwordfile

then the terminals do not drop into an interactive shell, sudo exits immediately after do_stuff.

How do I drop into an interactive shell from sudo -S?

My answer:


You’re doing the wrong thing. But you do have another choice.

First, storing the password in a file is completely silly and a serious security problem. Rather, configure sudoers to permit the necessary actions without a password.

Second, don’t try to run the non-interactive command and an interactive shell at the same time. This won’t work. You’ll have to run them separately, but you will be able to run them this way: To have sudo start an interactive shell, you simply run sudo -i.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.