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

相关推荐
Nebula嵌入式7 小时前
【C语言】09-深入解析main函数
linux·c语言·开发语言·嵌入式
Dxy12393102168 小时前
Linux 编译安装 Python 3.12.10(多版本共存,不破坏系统Python)
linux·运维·python
xz驱动分享9 小时前
Linux DMA 子系统学习笔记
linux·dma·rk3588·嵌入式软件
w678200710 小时前
Prisma不能优雅的支持DTO,试试Vona ORM吧
linux·运维·ubuntu
Darkwanderor11 小时前
Linux系统编程实战项目:模拟实现shell
linux·c++
杨某不才12 小时前
如何能让Linux服务器对shell 终端 + sftp 文件传输长期保活
linux·运维·服务器
vance0412 小时前
免费Cloudflare隧道隐藏公网IP
linux·tcp/ip·github
杨云龙UP12 小时前
生产环境Oracle表空间扩容实战:使用Toad for Oracle图形界面新增Datafile(ASM+OMF)
linux·运维·数据库·oracle·表空间·asm 存储管理·toad
三言老师13 小时前
文本工具组合统计服务器日志数据
linux·运维·服务器
mounter62514 小时前
认识 Tetragon:基于 eBPF 的安全监控与强制执行工具
linux·ebpf·cve·kernel