How can I pass a password to the "su" command?
Solution 1
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.
Solution 2
You could ssh to localhost as another user to execute whatever command you want. Or, use sudo and edit /etc/sudoers such that sudo does not ask for a password. However, there could be security implications.
EDIT: Please let me know why when you vote it down. My answer may not be perfect but at least it works. I do that myself for some licensed software that can only be run under a weird user name.
Solution 3
You do not want to specify the password as a command-line argument. Not so much because of @unwind's answer (scripts could be made private) but because if someone runs a list of processes, you could see the command argument and hence the password in question.
Solution 4
The version of su I have on my Linux server does not support such an option. I checked sudo, but it doesn't either. They want to do the prompting themselves, to ensure it's done in a safe manner and (I guess) to discourage people from putting passwords verbatim in scripts and so on.
Rob Sobers
I'm a software developer with a love for problem solving, design, and technology in general. I'm currently working at Fog Creek Software in New York.
Updated on September 05, 2022Comments
-
Rob Sobers over 1 year
I have a program that is going to take a password as input and then do a shell execute to perform a "su" (switch user) command in UNIX. However, I don't know how to pass the password variable to the UNIX su command. The language I have to use for this is pretty limited (UniBasic).
Any ideas?