SystemCalls
SystemCalls

Reputation: 31

Is there a way to see characters typed for an su password

I am writing a bash script that may need to su to run a command. Something like this-

echo "Installing the XYZ System"
echo "Please enter the root password-"
su -c "java -cp ./Installer.jar com.company.FmMain ; exit"

Is there a way to have su print characters (asterisks or something) so the user has some visual feedback as they type the password?

Upvotes: 2

Views: 1238

Answers (2)

Matt
Matt

Reputation: 231

I would not recomend masking password in Linux it goes against expected normal behaviour and will likely confuse users, especially if they are experienced Linux users. The standard way of reading passwords is to turn off echoing and disable processing of special characters like escape sequences etc. before accepting the password (this is usally done through setting terminal attributes - tcsetattr and the like), this is the way su works.

There are standard library functions such as getpass which do this for you: http://www.gnu.org/s/hello/manual/libc/getpass.html

Trying to implement your own methods of doing this has potential security implimentations. I'd strongly warn aginst this.

Upvotes: 1

Grisha Levit
Grisha Levit

Reputation: 8617

You'll have to read the password into a variable first, outside of su. See the question How do I echo stars (*) when reading password with `read`? for an example of using read to accomplish this.

Then, you'll have to use expect to pass this password in to the password prompt that su presents. See the question How do I use expect to connect via ssh to a system and change the password of the host system? for an example of doing something similar.

Upvotes: 1

Related Questions