linux 特定用户ssh,linux - 如何在登录后将SSH用户限制为一组预定义的命令?
你為什么不寫自己的login-shell? 為此使用Bash會(huì)非常簡(jiǎn)單,但您可以使用任何語(yǔ)言。
Bash中的示例
使用您喜歡的編輯器創(chuàng)建文件;(這可以是任何名稱或路徑,但應(yīng)該是&和&&):
#!/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
您所要做的就是將此可執(zhí)行文件設(shè)置為登錄shell。 例如,編輯;文件,并將該用戶&的當(dāng)前登錄shell替換為&&。
這只是一個(gè)簡(jiǎn)單的例子,但您可以根據(jù)需要將其設(shè)置為高級(jí),這個(gè)想法就在那里。 小心不要通過(guò)更改自己和唯一用戶的登錄shell來(lái)鎖定自己。 并始終測(cè)試奇怪的符號(hào)和命令,看它是否真的安全。
你可以用以下方法測(cè)試它:;。
注意,確保匹配整個(gè)命令,并注意通配符! 最好排除Bash符號(hào),如;,&,&&,||,$,并確認(rèn)反引號(hào)。
根據(jù)您給用戶的自由度,它不會(huì)比這更安全。 我發(fā)現(xiàn)通常我只需要讓一個(gè)只能訪問(wèn)少量相關(guān)命令的用戶,在這種情況下,這確實(shí)是更好的解決方案。但是,您是否希望提供更多自由,監(jiān)獄和權(quán)限可能更合適。 很容易犯錯(cuò)誤,只有在已經(jīng)太晚的時(shí)候才會(huì)注意到。
總結(jié)
以上是生活随笔為你收集整理的linux 特定用户ssh,linux - 如何在登录后将SSH用户限制为一组预定义的命令?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 服务器连交换机配置lacp协议,LACP
- 下一篇: Linux C高级编程——时间编程