Linux用户及用户组权限

发布于:2024-03-29 ⋅ 阅读:(19) ⋅ 点赞:(0)

一、用户和用户组

功能项 命令实例 作用
用户组 cat /etc/group 查看当前系统存在的用户组
groupadd testing 添加一个新的用户组testing
cat /etc/group 查看组是否被新增成功
groupmod -n test testing 将testing重命名成test
groupdel test 删除组test
groups root 查看用户root所在的所有组
useradd cat /etc/passwd 查看当前系统的用户信息
useradd zhangsan

新增一个用户zhangsan(默认时将新增一个对应名为zhangsan的组)

useradd -g test lisi 新增一个用户lisi并将其加入test组
useradd -g test -G dev wangwu 新增一个用户wangwu,其主组为test,并附加到组dev中
usermod usermod -g dev zhangsan  将用户zhangsan换到dev组
usermod -G 502 zhangsan 将用户zhangsan附加到gid为502的组
usermod -d /home/temp/lisi 将lisi的主目录从/home/lisi改为/home/temp
userdel userdel zhangsan 删除用户zhangsan
userdel -f zhangsan 强制删除用户zhangsan(即使该用户已经登录)
userdel -r zhangsan 删除用户zhangsan并删除其主目录

二、文件与文件夹权限

第一栏的10个字符,共分为4个部分:

第1个字母:-代表普通文件,d代表目录,l代表链接文件

第2、3、4:代表当前文件或文件夹所属用户(user)的权限,用u表示

第5、6、7:代表当前文件或文件夹所属的用户组(group)的权限,用g表示

第8、9、10:代表其他用户组和其他用户(other)的权限,用o表示

权限的表示方式:

r(读):也可用数字4来表示

w(写):也可用数字2来表示

x(执行):也可用数字1来表示

功能项 命令实例 作用
chmod chmod[权限][文件或目录] 更改文件或目录的权限
chmod u+x hello.txt 为hello.txt 文件所有者添加可执行权限
chmod u-x hello.txt 为hello.txt 文件所有者删除可执行权限
chmod g+r hello.txt 为hello.txt 文件所在组添加可读权限
chmod o+w hello.txt 为hello.txt 文件所在组的其他组添加可写权限
chmod a+wx hello.txt 为所有三种角色添加可写和可执行文件
chmod a-rwx hello.txt 删除hello.txt的所有权限(此时仅root可编辑)
chmod 777 hello.txt 将hello.txt的权限设置为rwxrwxrwx
chmod 643 hello.txt 将hello.txt的权限设置为rw-r--wx
chmod 777 /opt/test 将目录/opt/test的权限更改为777
chmod -R 755 /opt/test 将目录/opt/test及其下所有文件和子目录的权限修改为755
chown chown lisi hello.txt 将hello.txt的文件所有者改为lisi
chown lisi /opt/test 将目录/opt/test所有者改为lisi
chown -R lisi /opt/test 将目录/opt/test及其所有子目录和文件的所有者改为lisi
chgrp chgrp test hello.txt 将hello.txt所在的组改为test
chgrp lisi /opt/test 将目录/opt/test所在的组改为lisi
chgrp -R lisi /opt/test 将目录/opt/test及其所有子目录和文件所在的组改为lisi
passwd passwd lisi 修改lisi的密码 /etc/shadow保存了所有用户的密码,默认只有root才能操作

本文含有隐藏内容,请 开通VIP 后查看