Linux 怎麼用Shell腳本來執行su - 並且輸入密碼得到root權限
PHP中文网
PHP中文网 2017-04-17 14:33:25
0
3
694

如題,我怎麼一行命令執行 su - 並且輸入密碼得到root權限

下面這樣不行

$ echo "mypassword" | su -
su: Sorry
PHP中文网
PHP中文网

认证0级讲师

reply all(3)
小葫芦

Can use expect script

#!/usr/bin/env expect

spawn su

expect "Password:"

send "Your Password\r" # 不要丢了 '\r'

interact
PHPzhong

echo "password" | sudo -S try

大家讲道理

This is not possible and should not be done. Consider that if privileges can be escalated in this way, the root privileges of the system will be accessible to anyone.

Generally, if a script requires root permissions, we will execute the script directly as root at the beginning, instead of running it as our own ID first, and then switch identities through su or sudo during an operation that requires permissions. .

In addition, if you need to execute it as root and it can be executed and called by a user with ordinary permissions, you can consider setting setuid for the execution file

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!