Files
Linux/Docs/Linux用户和组配置.md
offends cee91802b3
Some checks failed
continuous-integration/drone Build is failing
synchronization
2025-08-25 15:57:40 +08:00

163 lines
3.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

> 本文作者:丁辉
# Linux用户和组配置
> 常用命令
查询当前用户
```bash
whoami
```
## linux 创建用户,群组,权限
- 创建用户
```bash
useradd demo
```
- 创建群组
```bash
groupadd demo
```
- 修改用户账户
```bash
usermod
```
| 参数 | 解释 |
| :--: | :----------------------------------------- |
| -l | 用户重命名 (用户家目录名需要手动修改) |
| -g | 修改用户所在的群组 |
| -G | 将用户添加到多个群组(群组之间,用逗号隔开) |
| -a | 追加 |
- 删除用户
```bash
userdel demo
```
> `-r` 选项会删除用户的主目录及其内容。
- 删除群组
```bash
groupdel demo
```
- 改变文件的所有者
```bash
chown
```
> -R参数递归设置子目录和子文件 (R 只能是大写,小写不起作用)
> 假如想要把用户demo的家目录的所有子目录和文件一起修改
```bash
chown -R demo:demo /home/demo
```
> 则/home/demo都归我(demo)所有了
- 改变文件的群组
```bash
chgrp
```
> chown命令可以改变文件的群组
```bash
chown demo:root demo.txt
```
> 就是把file.txt文件的所有者改为demo ,群组改为 root
- 修改访问权限
```bash
chmod
```
- 查看访问权限
```bash
ls -l
```
> 文件信息较复杂可看到d,w,r,l,x等字母。不细分的话这些可以通称为 文件访问权限符。
| 字符 | 含义 |
| ---- | -------- |
| d | 目录 |
| l | 链接 |
| u | 所有者 |
| g | 群组用户 |
| o | 其他用户 |
| a | 所有用户 |
| + | 添加权限 |
| - | 移除权限 |
| = | 分配权限 |
例如:
```bash
chmod u+rx file #文件file的所有者增加可读可执行的权限
chomd g +r file #文件file的群组用户增加可读的权限
chmod o-r file #文件file的其他用户移除可读的权限
chmod g+r o-r file #文件 file 的群组用户增加可读的权限,其他用户移除可读的权限
chmod go-r file #文件file 的群组用户,其他用户均移除可读的权限
chmod +r file #文件file的所有的用户都增加可读的权限
chmod u=rwx,g=r,o=- file #文件 file的所有者分配可读可写可执行的权限群组分配可读的权限其他用户没有权限。
```
## linux给用户添加root权限
1. 添加用户, 并添加密码
```bash
useradd demo
passwd demo
```
2. 赋予root权限
**方法一**
- 修改 /etc/sudoers 文件, 解除注释
```bash
%wheel ALL=(ALL) ALL
```
- 然后修改用户, 使其属于root组wheel
```bash
usermod -g root demo
```
**方法二**
- 修改 /etc/sudoers 文件, 添加内容
```bash
demo ALL=(ALL) ALL
```
**方法三**
- 修改 /etc/passwd 文件把用户ID修改为 0
```bash
demo:x:0:500:demo:/home/demo:/bin/bash
```