用户账号(User Accounts)
用户类型
- the super user
- the day-to-day user
- the system user
/etc/passwd
此文件里保存了用户的账号信息,例如用户名,密码(X 表示有密码,真实的密码存在 /etc/shadow 里),UID,GID,用户默认shell,用户home目录等等信息超级用户(The Super/Root User)
UID=0
GID=0 ```bash sudo apt update # 临时使用 root 权限
sudo -i # 切换到 root 账号, exit 退出
IMPORTANT
如果要用到 root 权限,应该始终使用 sudo 来临时获取
而不是以 root 账号进行
以免误操作导致系统被破坏
IMPORTANT
<a name="WUkEj"></a>### 普通用户(The Regular User)即登录用户,有账号和密码,以及 home 目录<a name="jv9rl"></a>### 系统用户(The System User)非人用户,也被称为 logical users,pseudo-users<br />例如 www-data,只有该系统用户和 root 用户可以访问 Apache web server 相关的文件。<br />可以在 /etc/passwd 里看到所有的系统用户、普通用户和 root 用户。<a name="lAiBq"></a>### User IDs & Groups IDsroot用户:UID=0,GID=0<br />系统用户:UID=1-499,65534<br />普通用户:UID=1000+,GID=UID;Ubuntu 自动为每个普通用户创建一个私有 Group。root用户可以把某个用户加到某个 Group 里;也可以新建一个 Group,把某些用户加入该 Group。Ubuntu中,Group 不能是其他 Group 的成员。<a name="l4dYR"></a>## 管理用户组(Managing Groups)<a name="zT1u7"></a>### 用户组列表Ubuntu 为每一个普通用户创建一个 GID、Group Name 与 UID、User Name 一致的 Group,(User Private Group)。```bashcat /etc/group # 所有用户组列表groups # 当前用户所属的用户组列表groups username # username 所属的用户组列表
用户组管理工具
- groupadd —— 创建新的用户组
- groupdel —— 删除已存在的用户组
- groupmod ——
- gpasswd —— 为用户组设置密码;-A username 为用户组设置管理员
- useradd -G —— 创建新用户时指所属定用户组
- usermod -G —— 添加用户到某个用户组(该用户不是登录状态)
grpck —— 检查 /etc/group 有没有错误
### 示例 ### sudo groupadd dvdrw # 创建新的用户组 dvdrw sudo chgrp dvdrw /dev/scd0 # DVD-RW 设备(/dev/scd0)所属于 dvdrw 用户组 sudo usermod -G dvdrw ronnie # 把用户 ronnie 添加到 用户组 dvdrw 中,此时 ronnie 有权限操作 /dev/scd0 了 sudo gpasswd -A ronnie dvdrw # 把 ronnie 设置为 dvdrw 用户组的管理员管理用户
用户必须要有一个 UID,/home 目录以及初始化文件,被分配到用户组里。
用户管理工具
useradd [OPTION]… —— 创建新的用户
- useradd —— 不提供任何参数:显示默认用户设置(/etc/skel)
- useradd -D —— 设置创建用户时的默认设置
- deluser —— 删除用户,—remove-home,—remove-all-files,—backup
- passwd —— 更新密码; -l 锁定用户; -u 解锁用户
- usermod —— 修改用户属性。-s shell;-u UID
- chsh —— 修改用户默认 shell
- newusers —— …
创建新用户
监视用户活动 ```bashsudo useradd john -p xxxxxx -u 1024w — display who is logged in and what they are doing
w [-hin] [user …]
w w ronnie
ac — display connect-time accounting
ac [-d] [-p] [-w file] [users …]
/var/log/wtmp
ac
<a name="9344X"></a>
## 管理密码
<a name="MIbMB"></a>
### 系统密码策略(System Password Policy)
1. 允许和禁止的密码
1. 密码修改的频率
1. 密码找回和重置
1. 用户处理密码
<a name="bEZ1M"></a>
### /etc/passwd 密码文件
username:password:uid:gid:gecos:homedir:shell
gecos
- 用户的全名、公司地址、公司和家庭号码、简要描述等;
- 此字段已经不常使用了。但是传统 UNIX 程序(如 finger,mail)会用到此字段。因此此字段也被叫做 finger infomation field;
- 此字段是逗号分隔的,可以使用 `chfn` 命令修改(change finger)
password
- 如果此字段是 `*` ,说明该用户被禁止登陆
- 传统 UNIX 通过直接编辑 /etc/passwd 来实现此功能
- Ubuntu 提供 `passwd -l` 来禁止用户登录
- x,表示 shadow password
shell
- 对于系统或逻辑用户,此字段是 /sbin/nologin 或 /bin/false,用来禁止此类用户登录。
<a name="VRty4"></a>
### Shadow Passwords
username:encrypted_password:day1:day2:day3:day4:day5:day6:reserved
day1 —— The day on which the last password change occurred<br />day2 —— The number of days before the password can be changed<br />day3 —— The number of days after which the password must be changed<br />day4 —— The number of days before the password expiration that the user is warned it will expire.<br />day5 —— The number of days after the password expires that the account is disabled (for security).<br />day6 —— Similar to the password change date, although this is the number of days since January 1, 1970, that the account has been disabled.
<a name="ilGTX"></a>
### 用户密码安全管理
```bash
# 更改 /etc/shadow 文件
# chage - change user password expiry information
# chage [options] LOGIN
批量修改密码
sudo chpasswd username:password username2:password2 username3:password3
