Linux用户及用户组权限

一、用户和用户组

|-------------|-------------------------------|----------------------------------------|
| 功能项 | 命令实例 | 作用 |
| 用户组 | 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才能操作 |
| | | |

相关推荐
jay13 分钟前
ens2f0 IP 远程连线,balance-alb 模式配置双网卡(ens2f0 + ens6f0)Bond,避免断网
linux·运维·服务器·网络·tcp/ip
Evan芙44 分钟前
用Shell脚本破解经典鸡兔同笼问题
linux·运维·网络
꧁坚持很酷꧂2 小时前
Ubuntu系统下Qt程序连接串口设备没有问题,但运行时出现Permission denied的解决方法
linux·qt·ubuntu
jerryinwuhan2 小时前
机器人控制程序
linux·运维·网络
honeysuckle_luo2 小时前
香橙派ai pro安装支持昇腾NPU的ollama
linux·运维·服务器
池央2 小时前
从“算子不支持”到“NPU高效执行”:CANN 8.0 TBE 自定义算子落地实践
linux·人工智能
小锋学长生活大爆炸2 小时前
【教程】CentOS在不知道root密码的情况下进入shell和重置密码
linux·运维·centos
代码游侠2 小时前
学习笔记——GDB调试工具
linux·开发语言·笔记·学习
通义灵码3 小时前
Qoder Linux 版公测上线
linux·运维·服务器
福尔摩斯张3 小时前
从Select到Epoll:深度解析Linux I/O多路复用演进之路(超详细)
linux·运维·服务器·c语言·网络