[英]how to switch to root user without entering password in bash script on RedHat
I want to switch to root user in bash script on RedHat by specifying the password in code rather than entering it. 我想通过在代码中指定密码而不是输入密码来在RedHat的bash脚本中切换到root用户。
Any help will be highly appreciated. 任何帮助将不胜感激。
Thanks 谢谢
It's not a good idea to store passwords in plain text in a script. 在脚本中以纯文本形式存储密码不是一个好主意。 You can use
visudo
to edit the sudoers
file and allow users to run a command using sudo
without using a password. 您可以使用
visudo
编辑sudoers
文件,并允许用户使用sudo
运行命令而无需使用密码。
The flexibility of sudo is widely under-estimated. sudo的灵活性被广泛低估。 This leads to very poor practices (like the
sudo su -
canon-ball surgery method). 这导致非常差的做法(例如
sudo su -
规范球手术方法)。
A much better method is to specificly allow the commands you intend to allow without use of a password : 更好的方法是明确允许您打算允许的命令, 而无需使用密码 :
phill = NOPASSWD: /bin/ls, /usr/bin/lprm
See for more examples: Shell script - Sudo-permissions lost over time 查看更多示例: Shell脚本-随着时间的流逝丢失了Sudo权限
Let sudo read from stdin using the -S flag 使用-S标志让sudo从stdin中读取
sudo -S mkdir /mnt/somedir <<END
password here
END
This will let a user create a directory in /mnt owned by root 这将使用户在/ mnt中创建一个由root拥有的目录
EDIT: this doesn't seem to work for the "su" command, yet I think this solution might be helpful 编辑:这似乎不适用于“ su”命令,但我认为此解决方案可能会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.