
我希望我的所有用戶都可以運行 bash 腳本。但是這個bash腳本需要有root權限,我希望這個腳本是只讀的並且以root權限運行
我的 bash 腳本:
#!/bin/bash
/sbin/swapoff -a
在我的搜尋中,我找到了解決方案,但它不起作用,這是解決方案:
# chown root:root monshell.sh
# chmod 4755 monshell.sh
# exit
$ ./monshell.sh
但當我使用基本用戶運行時出現此錯誤:swapoff:不是超級用戶。
答案1
創建 su root bash 腳本是危險的。 bash 腳本太容易以您不希望執行其他命令的方式進行操作。每個新的 su root 可執行檔都會帶來新的安全風險。
授予此類權限的首選方法是使用 sudo。建立檔案/etc/sudoers.d/swapoff,如下:
ALL ALL = /bin/swapoff -a
然後所有主機(第二個所有)上的所有使用者(第一個所有)都可以運行sudo /bin/swapoff -a
。每次他們這樣做時,都會記錄到系統日誌中。