服务器root权限安全策略配置

2023-11-03

服务器root权限安全策略配置

linux系统中,root具有至高无上的权限,为了安全起见,项目生产环境一般会要求禁止root用户直接ssh登录,使用普通用户登录,有特殊需求时,特定组用户可以使用su切换到root用户,或sudo切换到root权限进行操作。
image-20220215164810497
❗️❗️❗️ 注意: 整改有风险,请确认整改后的可能影响,并谨慎操作!

禁止root用户直接ssh登录

  1. 新建用户并设置密码
useradd admin && echo password123 | passwd --stdin admin

image-20220216100448856
2. 将新用户加入sudoers

cat /etc/sudoers |grep -A 2 "## Allow root to run"                    # 显示匹配行和之后的2行
sed -i '/## Allow root to run/a\admin\tALL=(ALL)\tALL' /etc/sudoers   # 关键字匹配行之后插入一行
cat /etc/sudoers |grep -A 2 "## Allow root to run"

image-20220216100531270
3. 用户登录和su测试

echo $UID
su
echo $UID

image-20220216100707840
4. 日常运维操作测试

kubectl get pod -A
sudo kubectl get pod -A | grep -iv run
sudo kubectl get pod -A |wc -l
sudo kubectl get pod -A

image-20220216102725839
5. 禁用root用户ssh登录

sed -i 's/PermitRootLogin yes/#PermitRootLogin yes/g' /etc/ssh/sshd_config    # 添加注释
cat /etc/ssh/sshd_config |grep PermitRootLogin
systemctl restart sshd

image-20220216103012866
6. 再次登录测试
预期:
root用户无法ssh登录,新建用户ssh登录正常,sudo权限使用正常,可以su切换到root
验证:
root用户ssh登录失败
image-20220216103702746
已加入sudoers的新建用户ssh登录正常,sudo权限使用正常,su切换到root正常
image-20220216104627426
未加入sudoers用户ssh登录正常,不能使用sudo,但su切换到root正常
image-20220216104818350
如果需要设置sudo的超时时间,请点击文末的链接查阅参考。

限制用户su切换到root

  1. 新建用户并加入wheel组
useradd abc && echo password123 | passwd --stdin abc
usermod -G wheel abc

或将已有用户加入wheel组

usermod -G wheel admin

image-20220216112524719
2. 设置只允许wheel组用户使用su命令切换到root

cat /etc/pam.d/su |grep "pam_wheel.so use_uid"
sed -i '/pam_wheel.so use_uid/s/^#//' /etc/pam.d/su          # 去掉注释
cat /etc/pam.d/su |grep "pam_wheel.so use_uid"

image-20220216112621524

echo "SU_WHEEL_ONLY yes" >> /etc/login.defs                  # 文末追加
tail /etc/login.defs

image-20220216113055666
3. 验证测试
预期:
wheel组用户su切换root正常,sudo权限正常;
新建非wheel组用户,加入sudoers后,无法su切换到root,但sudo权限使用正常;
未加入sudoers且非wheel组用户,既无法su切换到root,也无法使用sudo权限

cat /etc/group |grep wheel                      # 查看wheel组用户

image-20220216113604777
验证:
wheel组用户,su切换root正常,sudo权限正常
image-20220216114105877
image-20220216114424770
非wheel组用户-新建用户加入sudoers,sudo权限使用正常,不能su切换到root

useradd qaz && echo password123 | passwd --stdin qaz
cat /etc/sudoers |grep -A 3 "## Allow root to run"                    # 显示匹配行和之后的3行
sed -i '/## Allow root to run/a\qaz\tALL=(ALL)\tALL' /etc/sudoers     # 关键字匹配行之后插入一行
cat /etc/sudoers |grep -A 3 "## Allow root to run"

image-20220216115735932
image-20220216120507090
非wheel组用户-原有用户未加入sudoers,sudo和su切换都无法使用
image-20220216115205314

参考文章

sudo 密码超时时间
只允许特定的组用户su切换到root

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

服务器root权限安全策略配置 的相关文章

随机推荐