shell脚本:导航到root
我正在编写一个简单的 shell 脚本,需要 root 权限。使用终端,如果您输入 sudo su 并写入密码,它肯定可以工作。 我想将密码传递给脚本文件,然后将其传递给系统成为root ex:
pass= $1
navigateToRoot($pass)
I am writing a simple shell script which requires a root privilege. Using the terminal if you type sudo su
and you write the password it works definitely.
I want to pass the password to the script file and then pass it to the system to become root ex:
pass= $1
navigateToRoot($pass)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更好的方法是:
通过设置 setuid 位,该文件将以 root(所有者)身份执行,而无需存储或传递 root 密码。
确保没有其他人可以修改脚本
请参阅评论:
SETUID 不适用于 Shell 脚本,因此此答案无效。
A better approach would be to :
By setting the
setuid
bit, the file will execute as root (owner) without having to store or pass the root password.Make sure nobody else can modify the script
SEE COMMENTS:
SETUID does not work on Shell scripts so this answer is INVALID.
根据您对问题的评论,我假设您实际上想以某种替代方式向用户询问密码,而不是将密码以明文形式保存在文件中。
您可以使用
-A
标志来sudo
。来自man sudo
:Based on your comment on the question, I'm assuming you actually want to ask the user for a password in some alternate way, rather than having your password in clear text in a file.
You can use the
-A
flag tosudo
. Fromman sudo
:如果您想从脚本发送密码,则需要一种能够在 tty-s,例如 expect (基于 tcl,一种我非常不喜欢的脚本语言)。
但是配置良好的
sudo
,或者较少使用的super
命令,或者可能是一个二进制可执行文件(在 C 中非常谨慎地编码),它是 setuid 并调用一些其他脚本,应该足够了......
If you want to send a password from a script, you need a scripting language able to do weird things on tty-s, like e.g. expect (which is based upon tcl, a scripting language that I strongly dislike).
But a well configured
sudo
, or the less usedsuper
command, or perhaps a binary executable (coded with great caution in C) which is setuid and invokes some other script, should be enough...