KPK*_*KPK 2 linux bash shell ubuntu
我是Ubuntu和bash脚本的新手,但我刚刚创建runUpdates.sh并将其添加到我.profile运行它:
if [ -f "$HOME/bin/runUpdates.sh" ]; then
. "$HOME/bin/runUpdates.sh"
fi
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,我希望脚本像root运行一样运行(因为我不想输入我的sudo密码)
我发现了几个地方,我应该能够做到sudo chown root.root <my script>并且sudo chmod 4755 <my script>,当我运行它,它应该以root身份运行.但它不是......
这个脚本对我来说很好看.我错过了什么?-rwxr-xr-x 1 root root 851 Mar 23 21:14 runUpdates.sh*
你可以帮助我以root身份运行这个脚本中的命令吗?我真的不想更改sudors文件,我真的只想在root下运行这个脚本中的命令(如果可能的话).
#!/bin/sh
echo "user is ${USER}"
#check for updates
update=`cat /var/lib/update-notifier/updates-available | head -c 2 | tail -c 1`;
if [ "$update" = "0" ]; then
echo -e "No updates found.\n";
else
read -p "Do you wish to install updates? [yN] " yn
if [ "$yn" != "y" ] && [ "$yn" != "Y" ]; then
echo -e 'No\n';
else
echo "Please wait...";
echo `sudo apt-get update`;
echo `sudo apt-get upgrade`;
echo `sudo apt-get dist-upgrade`;
echo -e "Done!\n";
fi
fi
#check for restart
restartFile=`/usr/lib/update-notifier/update-motd-reboot-required`;
if [ ! -z "$restartFile" ]; then
echo "$restartFile";
read -p "Do you wish to REBOOT? [yN] " yn
if [ "$yn" != "y" ] && [ "$yn" != "Y" ]; then
echo -e 'No\n';
else
echo `sudo shutdown -r now`;
fi
fi
Run Code Online (Sandbox Code Playgroud)
我添加的用户是调试,它总是输出我的用户而不是root,并提示输入sudo密码(因为我用sudo调用命令)或告诉我你是root用户?(如果我删除sudo)
还有,有没有办法实时输出更新命令stdout,而不是只有一个块完成?
(我也尝试过shebang #!/bin/bash)
出于安全原因,setuid不适用于shell脚本.如果要在没有密码的情况下以root身份运行脚本,可以编辑/etc/sudoers以允许它在sudo没有密码的情况下运行.
要"实时更新",您将直接运行命令而不是使用echo.
小智 5
这样做不安全,您可能应该使用sudoers,但是如果您确实需要/想要,可以使用以下方法进行操作:
echo <root password> | sudo -S echo -n 2>/dev/random 1>/dev/random
sudo <command>
Run Code Online (Sandbox Code Playgroud)
之所以有效,是因为sudo在成功使用后,不需要为简短窗口输入密码。
SUID root scripts were phased out many years ago if you really want to run scripts as root you need to wrap them in an executable, you can see an example on how to do this on my blog: http://scriptsandoneliners.blogspot.com/2015/01/sanitizing-dangerous-yet-useful-commands.html
The example is how to change executable permissions and place a filter around other executables using a shell script but the concept of wrapping a shell script works for SUID as well, the resulting executable file from the shell script can be made SUID.
https://help.ubuntu.com/community/Sudoers
| 归档时间: |
|
| 查看次数: |
22564 次 |
| 最近记录: |