Linux下通过受限bash创建指定权限的账号

Posted 散尽浮华

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下通过受限bash创建指定权限的账号相关的知识,希望对你有一定的参考价值。

 

在日常业务运维中,有时为了配合解决问题,需要给非运维人员开通系统账号,用于查询日志或代码。通常为了系统安全或避免不必要的误操作等目的,会将账号权限降至最低。下面介绍下在Linux下通过受限bash创建指定权限账号的操作记录:

[[email protected] ~]# ln -s /bin/bash  /bin/rbash
[[email protected] ~]# useradd -s /bin/rbash wangshibo
[[email protected] ~]# passwd wangshibo
[[email protected] ~]# mkdir /home/wangshibo/bin
[[email protected] ~]# chown root. /home/wangshibo/.bash_profile 
[[email protected] ~]# chmod 755 /home/wangshibo/.bash_profile
[[email protected] ~]# vim /home/wangshibo/.bash_profile       //复制下面的内容覆盖原内容
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
 
# User specific environment and startup programs
 
PATH=$HOME/bin
 
export PATH
[[email protected] ~]# ln -s /bin/cat /home/wangshibo/bin/cat [[email protected] ~]# ll /home/wangshibo/ total 4 drwxr-xr-x 2 root root 4096 Nov 25 23:38 bin [[email protected] ~]# ll /home/wangshibo/bin/ total 0 lrwxrwxrwx 1 root root 8 Nov 25 23:12 cat -> /bin/cat

如上设置后,可以发现创建的wangshibo用户家目录下的文件权限是root.root上面只设置了wangshibo用户的cat权限,并且只能cat查看wangshibo用户家目录/home/wangshibo下的文件。除了cat命令外。不能执行其他命令!

[[email protected] ~]$ cat /var/log/messages
cat: /var/log/messages: Permission denied
[[email protected] ~]$ ls
-rbash: /home/wangshibo/bin/ls: No such file or directory
[[email protected] ~]$ touch test
-rbash: /home/wangshibo/bin/touch: No such file or directory

如果要想在其家目录下有其他命令的执行权,那么需要添加这些命令的软链接到/home/wangshibo/bin目录下(可以通过which命令查看二进制命令的全路径)

[[email protected] ~]# ln -s /bin/ls /home/wangshibo/bin
[[email protected] ~]# ln -s /bin/touch /home/wangshibo/bin
[[email protected] ~]# ln -s /bin/mkdir /home/wangshibo/bin
[[email protected] ~]# ln -s /usr/bin/vim /home/wangshibo/bin/
[[email protected] ~]# ll /home/wangshibo/bin/
total 0
lrwxrwxrwx 1 root root  8 Nov 25 23:12 cat -> /bin/cat
lrwxrwxrwx 1 root root  7 Nov 25 23:44 ls -> /bin/ls
lrwxrwxrwx 1 root root 10 Nov 25 23:45 mkdir -> /bin/mkdir
lrwxrwxrwx 1 root root 10 Nov 25 23:44 touch -> /bin/touch
lrwxrwxrwx 1 root root 12 Nov 25 23:45 vim -> /usr/bin/vim

这样,wangshibo用户就拥有了上面加入的命令的执行权

[[email protected] ~]# su - wangshibo
[[email protected] ~]$ ls
bin
[[email protected] ~]$ touch test
[[email protected] ~]$ mkdir ops
[[email protected] ~]$ vim test
[[email protected] ~]$ cat test
dsfdsafsadf
[[email protected] ~]$ rm -f test
-rbash: rm: command not found
[[email protected] ~]$ ls /usr/
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[[email protected] ~]$ cat /var/log/messages
cat: /var/log/messages: Permission denied

以上是关于Linux下通过受限bash创建指定权限的账号的主要内容,如果未能解决你的问题,请参考以下文章

Linux创建用户如何指定shell,有啥权限

Linux系统用户账号的管理

linux 如何指定nologin用户执行命令

RBash - 受限的Bash绕过

Linux下创建root权限的账号osadmin

Linux下设置文件权限