51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

linux 实现普通用户只允许使用部分命令

linux 实现普通用户只允许使用部分命令

由于公司奇葩需求,需要给所有开发用户开通所有服务器的只能查看权限,结合现有jumpserver实现扩展,搭建jumpserver省略,推送一个普通用户

接下来的操作都是在系统里,这里我写了一个脚本用于实现,只允许check_user用户执行 cat ls tail tailf命令

[sourcecode language="plain"]
#!/bin/bash
User="check_user"
Command="cat ls tail tailf"

ln -s /bin/bash /bin/rbash
User_alive=`cat /etc/passwd | grep check_user|wc -l`
if [ $User_alive -eq 1 ]
then
sed -i "/$User/s/\/bin\/bash/\/bin\/rbash/" /etc/passwd
mkdir /home/$User/bin
touch /home/$User/.bash_profile
if [ -f /home/$User/.bash_profile ]
then
chown root. /home/$User/.bash_profile
chmod 755 /home/$User/.bash_profile
echo > /home/$User/.bash_profile
cat >> /home/$User/.bash_profile <<EOF

.bash_profile

Get the aliases and functions

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

User specific environment and startup programs

PATH=\$HOME/bin
EOF
else
echo "profile is not exist"
exit "6"
fi
else
echo "User is not exist"
exit 5
fi
for i in $Command
do
dir=`whereis $i | awk "{print $2}"`
ln -s $dir /home/$User/bin
done
[/sourcecode]

以上转自: 转自https://blog.51cto.com/chentianwang/2062053

实现后发现后续问题目前还没有解决

1,本想把cd加上,但是centos是可以,ubuntu是不可以,因为cd在ubuntu里不算命令是内置内核里的,不知怎么解决

2,tab补全功能有的机器可以,有的不可以不知道为什么

这边测试了直接cd 不行

centos7和centos6的cd位置也不一样

linux 实现普通用户只允许使用部分命令_https://www.tiejiang.org_Linux安全运维_第1张

linux 实现普通用户只允许使用部分命令_https://www.tiejiang.org_Linux安全运维_第2张

后面发现cd 需要先执行一下bash才可以,而且必须是root切换到read_user才可以。

https://en.wikipedia.org/wiki/Restricted_shell

linux 实现普通用户只允许使用部分命令_https://www.tiejiang.org_Linux安全运维_第3张

在/usr/bin下查看cd 还是需要bash执行。

linux 实现普通用户只允许使用部分命令_https://www.tiejiang.org_Linux安全运维_第4张

只能修改Command="bash cd cat ls tail tailf",这样用户会有执行bash的权限。还是要根据实际情况设置。

赞(6)
未经允许不得转载:工具盒子 » linux 实现普通用户只允许使用部分命令