I can do an su with su <username> and it asks for my password. Is there a password parameter for su such that i wont be prompted for a password?
e.g. su <username> -p <password>
If you want to write a script that runs as a different user something like this works, though it dows output the word "password" without a newline to standard out
su - username <<!
enterpasswordhere
enter commands to run as the new user
!
if you have a user named fred with a password of 1234 and want to get an ls of fred's home directory as fred, without the password string displayed, it would look like
su - fred <<! >/dev/null 2>&1
1234
whoami > /dev/tty
ls > /dev/tty
!
I belive, there is not and it would not be a good idea. Here's why:
If you write a password in a command like su <username> -p <password>, it would be stored in plain text in your bash history. This is certainly a huge security issue.
If you need to run commands with su (or sudo) in an automated way, write a shellscript containig the commands without su or sudo and run su <username> script.sh
echo <passwd> | sudo -S <command-line>.Althought it is not safe, but it works.
– Dai Kaixian
Feb 15 '17 at 07:53
This only works for me
echo "<password>" | sudo -S sleep 1 && sudo su - <username>
cat password.txt | sudo -S is a better method. At least you can chmod password.txt to read only for the user itself.
– Rinzwind
Mar 29 '24 at 09:47
If you want to execute some specific commands as a new user, use the following command:
sudo -u {username} {command to be executed as the new user}
or here: http://stackoverflow.com/questions/715908/how-do-you-make-sudo-save-the-password
– HongboZhu Oct 07 '13 at 13:02superuserhere, http://superuser.com/questions/67765/sudo-with-password-in-one-command-line – alvas Oct 07 '13 at 13:08