linux 特定用户ssh,linux - 如何在登录后将SSH用户限制为一组预定义的命令?
你為什么不寫自己的login-shell? 為此使用Bash會非常簡單,但您可以使用任何語言。
Bash中的示例
使用您喜歡的編輯器創建文件;(這可以是任何名稱或路徑,但應該是&和&&):
#!/bin/bash
commands=("man" "pwd" "ls" "whoami")
timestamp(){ date +'%Y-%m-%s %H:%M:%S'; }
log(){ echo -e "$(timestamp)\t$1\t$(whoami)\t$2" > /var/log/rbash.log; }
trycmd()
{
# Provide an option to exit the shell
if [[ "$ln" == "exit" ]] || [[ "$ln" == "q" ]]
then
exit
# You can do exact string matching for some alias:
elif [[ "$ln" == "help" ]]
then
echo "Type exit or q to quit."
echo "Commands you can use:"
echo " help"
echo " echo"
echo "${commands[@]}" | tr ' ' '\n' | awk '{print " " $0}'
# You can use custom regular expression matching:
elif [[ "$ln" =~ ^echo\ .*$ ]]
then
ln="${ln:5}"
echo "$ln" # Beware, these double quotes are important to prevent malicious injection
# For example, optionally you can log this command
log COMMAND "echo $ln"
# Or you could even check an array of commands:
else
ok=false
for cmd in "${commands[@]}"
do
if [[ "$cmd" == "$ln" ]]
then
ok=true
fi
done
if $ok
then
$ln
else
log DENIED "$cmd"
fi
fi
}
# Optionally show a friendly welcome-message with instructions since it is a custom shell
echo "$(timestamp) Welcome, $(whoami). Type 'help' for information."
# Optionally log the login
log LOGIN "$@"
# Optionally log the logout
trap "trap=\"\";log LOGOUT;exit" EXIT
# Optionally check for '-c custom_command' arguments passed directly to shell
# Then you can also use ssh user@host custom_command, which will execute /root/rbash.sh
if [[ "$1" == "-c" ]]
then
shift
trycmd "$@"
else
while echo -n "> " && read ln
do
trycmd "$ln"
done
fi
您所要做的就是將此可執行文件設置為登錄shell。 例如,編輯;文件,并將該用戶&的當前登錄shell替換為&&。
這只是一個簡單的例子,但您可以根據需要將其設置為高級,這個想法就在那里。 小心不要通過更改自己和唯一用戶的登錄shell來鎖定自己。 并始終測試奇怪的符號和命令,看它是否真的安全。
你可以用以下方法測試它:;。
注意,確保匹配整個命令,并注意通配符! 最好排除Bash符號,如;,&,&&,||,$,并確認反引號。
根據您給用戶的自由度,它不會比這更安全。 我發現通常我只需要讓一個只能訪問少量相關命令的用戶,在這種情況下,這確實是更好的解決方案。但是,您是否希望提供更多自由,監獄和權限可能更合適。 很容易犯錯誤,只有在已經太晚的時候才會注意到。
總結
以上是生活随笔為你收集整理的linux 特定用户ssh,linux - 如何在登录后将SSH用户限制为一组预定义的命令?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器连交换机配置lacp协议,LACP
- 下一篇: Marvelous Designer 服