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才能操作 |
| | | |

相关推荐
chlk12318 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑19 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件19 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash1 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行2 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux
崔小汤呀4 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应4 天前
vi编辑器使用
linux·后端·操作系统