linux 添加只读用户

这里未使用rbash新建用户,使用rbash新建只读用户不能使用cd等内置命令。

  • 添加用户
1
# useradd -m test
  • 设置密码
1
# passwd test
  • 修改用户的shell配置文件
1
2
# chown root. /home/test/.bash_profile
# chmod 755 /home/test/.bash_profile
  • 修改/home/test/.bash_profile配置文件

PATH改为$HOME/.bin

1
2
3
4
5
6
7
8
9
10
11
12
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

# PATH=$PATH:$HOME/.local/bin:$HOME/bin
PATH=$HOME/.bin
export PATH
  • 创建用户.bin目录
1
# mkdir /home/test/.bin
  • 将允许执行的命令链接到/home/test/.bin目录
1
2
3
4
5
6
7
8
9
ln -s /usr/bin/wc /home/test/.bin/wc
ln -s /usr/bin/tail /home/test/.bin/tail
ln -s /usr/bin/more /home/test/.bin/more
ln -s /usr/bin/cat /home/test/.bin/cat
ln -s /usr/bin/grep /home/test/.bin/grep
ln -s /usr/bin/find /home/test/.bin/find
ln -s /usr/bin/pwd /home/test/.bin/pwd
ln -s /usr/bin/ls /home/test/.bin/ls
ln -s /usr/bin/less /home/test/.bin/less

之后使用创建的用户登录系统,用户只拥有只读权限,只能使用软连接的命令。