2020-05-11 17:56:07 +08:00
|
|
|
# Linux 用户和组操作
|
|
|
|
|
|
|
|
## 添加/删除用户和组
|
2019-07-25 10:50:03 +08:00
|
|
|
|
|
|
|
使用以下命令添加用户和组:
|
|
|
|
|
|
|
|
```sh
|
2020-05-11 17:56:07 +08:00
|
|
|
groupadd <name name>
|
|
|
|
useradd <user name> -d <home dir> -g <group name>
|
2019-07-25 10:50:03 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
useradd 和 groupadd 命令使用比较复杂,可使用 adduser 和 addgroup 命令简化操作:
|
|
|
|
|
|
|
|
```sh
|
2020-05-11 17:56:07 +08:00
|
|
|
addgroup <group name>
|
|
|
|
adduser <user name>
|
2019-07-25 10:50:03 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
adduser 和 addgroup 提供了一个脚本,根据与用户的交互信息调用更底层的 useradd 和 groupadd 命令执行实际操作。
|
|
|
|
|
|
|
|
使用以下命令可删除用户和组:
|
|
|
|
|
|
|
|
```sh
|
2020-05-11 17:56:07 +08:00
|
|
|
userdel -r <user name>
|
|
|
|
groupdel <group name>
|
|
|
|
```
|
|
|
|
|
2020-05-12 10:28:01 +08:00
|
|
|
## 组操作
|
|
|
|
|
|
|
|
### 将用户添加到组
|
|
|
|
|
|
|
|
```sh
|
|
|
|
gpasswd --add <user name> <group name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### 从组中删除用户
|
|
|
|
|
|
|
|
```sh
|
|
|
|
gpasswd -d <user name> <group name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### 查看用户所属组
|
2020-05-11 17:56:07 +08:00
|
|
|
|
|
|
|
```sh
|
|
|
|
groups <user name>
|
|
|
|
```
|
|
|
|
|
2020-05-12 10:28:01 +08:00
|
|
|
### 修改组名
|
2020-05-11 17:56:07 +08:00
|
|
|
|
|
|
|
```sh
|
2020-05-12 10:28:01 +08:00
|
|
|
groupmod -n <new group name> <group name>
|
|
|
|
```
|
|
|
|
|
|
|
|
## 用户操作
|
|
|
|
|
|
|
|
### 为用户设置密码
|
|
|
|
|
|
|
|
```sh
|
|
|
|
passwd <user name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### 显示用户信息
|
|
|
|
|
|
|
|
```sh
|
|
|
|
id <user name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### 修改用户名
|
|
|
|
|
|
|
|
```sh
|
|
|
|
usermod -l <new user name> <user name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### 修改用户 Home 目录
|
|
|
|
|
|
|
|
```sh
|
|
|
|
usermod -d <new home dir> <user name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### 修改用户 ID
|
|
|
|
|
|
|
|
```sh
|
|
|
|
usermod -u <new ID> <user name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### 更改用户信息
|
|
|
|
|
|
|
|
```sh
|
|
|
|
chfn <user name>
|
2019-07-25 10:50:03 +08:00
|
|
|
```
|