如何向一個群組授予對一組特定命令的執行權限,並向另一個群組授予另一組命令的執行權限

如何向一個群組授予對一組特定命令的執行權限,並向另一個群組授予另一組命令的執行權限

假設有一組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).

筆記:

  • 範例中的問題:通常killshell 內建指令(用 進行檢查type kill)。如果你讓使用者擁有一個,shell恐怕你不會找到一種方法來避免他們的使用kill(除非你以正確的方式修改shell的源代碼並重新編譯它......)。
  • 如果您要為這些使用者關閉的命令具有readexecution所有使用者設定的屬性(例如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/nologinas shell),並編寫以下 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應該不那麼安全...

參考:

相關內容