用户权限管理的核心是用户-组-权限的绑定,需先完成用户和组的创建与关联。
useradd命令,-m选项自动创建用户主目录(如/home/username)。sudo useradd -m newusergroupadd命令创建用户组。sudo groupadd devgrouppasswd命令为用户设置初始密码(需符合复杂度要求)。sudo passwd newuserusermod -aG命令(-a表示追加,避免移除原有组)。newuser添加到devgroup组:sudo usermod -aG devgroup newuserusermod -g命令(-g指定主组,会覆盖原有主组)。newuser的主组改为devgroup:sudo usermod -g devgroup newuserid命令查看用户的UID、GID及所属组。id newuser文件/目录的权限分为**读(r)、写(w)、执行(x)**三类,分别对应用户(owner)、组(group)、其他用户(others)三级权限。
使用ls -l命令查看文件/目录的详细权限信息(如-rw-r--r--表示所有者有读写权限,组和其他用户有读权限)。
示例:ls -l /path/to/file
u(用户)、g(组)、o(其他)、a(所有)指定对象,+(添加)、-(移除)、=(设置)权限。sudo chmod u+x script.sh755表示所有者有rwx,组和其他用户有r-x。755权限:sudo chmod 755 /path/to/dirchown命令,格式为chown new_owner:group file(可同时修改所有者和组)。file.txt的所有者改为admin,组改为devgroup:sudo chown admin:devgroup file.txtchgrp命令。file.txt的组改为devgroup:sudo chgrp devgroup file.txt/usr/bin/passwd)。sudo chmod u+s /path/to/executablesudo chmod g+s /path/to/dir/tmp目录)。sudo chmod +t /path/to/dir传统权限无法满足细粒度需求时,可使用ACL(访问控制列表)和sudo机制。
getfacl命令查看文件/目录的ACL规则。getfacl /path/to/filesetfacl命令添加/删除权限。alice添加读写权限:sudo setfacl -m u:alice:rw /path/to/filedevgroup添加读权限:sudo setfacl -m g:devgroup:r /path/to/filebob的权限:sudo setfacl -x u:bob /path/to/filewheel组的用户可使用sudo执行管理员命令。sudo usermod -aG wheel usernamevisudo命令编辑/etc/sudoers文件(避免语法错误),添加特定命令的权限。deploy用户无需密码执行systemctl restart nginx:deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginxSELinux(安全增强型Linux)提供强制访问控制(MAC),进一步提升系统安全性。
使用sestatus命令查看SELinux是否启用及模式(Enforcing/Permissive/Disabled)。
示例:sestatus
sudo setenforce 0(重启后恢复)/etc/selinux/config文件,将SELINUX=enforcing改为SELINUX=disabled(需重启系统)若需允许应用访问特定目录,可使用semanage fcontext添加策略,再用restorecon应用。
示例:允许httpd服务访问/var/www/html/custom目录:
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/custom(/.*)?"
sudo restorecon -Rv /var/www/html/custom
弱密码是系统安全的重大隐患,需通过以下措施强化口令安全。
要求用户口令包含大写字母、小写字母、数字、特殊字符,长度≥10位。可通过passwd命令强制用户修改口令:sudo passwd -e username(强制下次登录修改)
使用awk命令查找/etc/shadow文件中空口令的账户(第二字段为空),并强制设置口令:
awk -F: '($2 == "") {print $1}' /etc/shadow
使用chattr命令给/etc/shadow文件添加不可更改属性,防止非授权修改:
sudo chattr +i /etc/shadow(如需修改,需先取消属性:sudo chattr -i /etc/shadow)
以上步骤覆盖了CentOS Stream 8用户权限管理的核心场景,遵循最小权限原则(仅授予必要权限),可有效提升系统安全性。操作前建议备份重要数据,避免误操作导致系统故障。