
假設有一組shell指令{ls,cat,kill}
我想讓 A 組中的使用者能夠執行命令 {ls, cat} 並使 B 組中的使用者能夠執行命令 {cat, kill}
如何做到這一點,最好的方法是什麼?
我考慮過一些解決方案,但它們似乎不是 100% 安全的。
答案1
限制執行命令的可能性的方法之一是受限外殼。
摘錄自手冊,其中指出不允許或不執行以下操作:
Changing directories with the cd builtin.
Setting or unsetting the values of the SHELL, PATH, ENV, or BASH_ENV variables.
Specifying command names containing slashes.
Specifying a filename containing a slash as an argument to the . builtin command.
Specifying a filename containing a slash as an argument to the -p option to the hash builtin command.
Importing function definitions from the shell environment at startup.
Parsing the value of SHELLOPTS from the shell environment at startup.
Redirecting output using the ‘>’, ‘>|’, ‘<>’, ‘>&’, ‘&>’, and ‘>>’ redirection operators.
Using the exec builtin to replace the shell with another command.
Adding or deleting builtin commands with the -f and -d options to the enable builtin.
Using the enable builtin command to enable disabled shell builtins.
Specifying the -p option to the command builtin.
Turning off restricted mode with ‘set +r’ or ‘set +o restricted’.
之後,您可以添加指向您希望他們可以執行的命令的連結。
也許實現你的目標的另一條路應該是透過指某東西的用途sudo
。
在您的情況下,您可以編輯sudoers
文件(使用visudo
)並獲得類似於以下內容的內容:
User_Alias USERS_GROUP_A = joe, mike, cedric
User_Alias USERS_GROUP_B = jude, zoe, cedric
Cmnd_Alias COMMANDS_GROUP_A = /bin/ls, /bin/cat, /usr/bin/zip
Cmnd_Alias COMMANDS_GROUP_B = /bin/kill, /bin/cat, /usr/bin/zip
USERS_GROUP_A ALL= COMMANDS_GROUP_A
USERS_GROUP_B ALL= COMMANDS_GROUP_B
# users of the group USERS_GROUP_A may run /bin/ls, /bin/cat, and /usr/bin/zip
# from any machine (ALL).
# users of the group USERS_GROUP_B may run /bin/kill,/bin/cat and /usr/bin/zip
# from any machine (ALL).
筆記:
- 範例中的問題:通常
kill
是shell 內建指令(用 進行檢查type kill
)。如果你讓使用者擁有一個,shell
恐怕你不會找到一種方法來避免他們的使用kill
(除非你以正確的方式修改shell的源代碼並重新編譯它......)。 如果您要為這些使用者關閉的命令具有
read
為execution
所有使用者設定的屬性(例如ls -l /usr/bin/zip
)-rwxr-xr-x 1 root root 188296 Oct 21 2013 /usr/bin/zip
也許您可以使用解決方法,將
execution
屬性限制為僅所有者及其群組sudo chattr o-x /usr/bin/zip
,-rwxr-xr-- 1 root root 188296 Oct 21 2013 /usr/bin/zip
新增用戶(例如酷用戶) 到該組(可能使用
/usr/sbin/nologin
asshell
),並編寫以下 2 行來代替上面的相應內容:USERS_GROUP_A ALL=(cooluser) NOPASSWD: COMMANDS_GROUP_A USERS_GROUP_B ALL=(cooluser) NOPASSWD: COMMANDS_GROUP_B # users of the USERS_GROUP_A may run /bin/ls, /bin/cat and /usr/bin/zip # as the user cooluser from any machine (ALL). # users of the USERS_GROUP_B may run /bin/kill,/bin/cat and /usr/bin/zip # as the user cooluser from any machine (ALL).
該關鍵字
NOPASSWD:
是為了避免密碼的請求。
您的使用者可以使用以下命令執行命令sudo -u cooluser /usr/bin/zip
副作用:其他使用者將無法運行該命令,直到您不將他們包含在文件所有者的群組中...如果是的話
root
應該不那麼安全...
參考: