用户账号(User Accounts)

用户类型

  1. the super user
  2. the day-to-day user
  3. 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

  1. <a name="WUkEj"></a>
  2. ### 普通用户(The Regular User)
  3. 即登录用户,有账号和密码,以及 home 目录
  4. <a name="jv9rl"></a>
  5. ### 系统用户(The System User)
  6. 非人用户,也被称为 logical users,pseudo-users<br />例如 www-data,只有该系统用户和 root 用户可以访问 Apache web server 相关的文件。<br />可以在 /etc/passwd 里看到所有的系统用户、普通用户和 root 用户。
  7. <a name="lAiBq"></a>
  8. ### User IDs & Groups IDs
  9. root用户:UID=0,GID=0<br />系统用户:UID=1-499,65534<br />普通用户:UID=1000+,GID=UID;Ubuntu 自动为每个普通用户创建一个私有 Group。
  10. root用户可以把某个用户加到某个 Group 里;也可以新建一个 Group,把某些用户加入该 Group。Ubuntu中,Group 不能是其他 Group 的成员。
  11. <a name="l4dYR"></a>
  12. ## 管理用户组(Managing Groups)
  13. <a name="zT1u7"></a>
  14. ### 用户组列表
  15. Ubuntu 为每一个普通用户创建一个 GID、Group Name 与 UID、User Name 一致的 Group,(User Private Group)。
  16. ```bash
  17. cat /etc/group # 所有用户组列表
  18. groups # 当前用户所属的用户组列表
  19. 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 —— …

    创建新用户

    sudo useradd john -p xxxxxx -u 1024
    
    监视用户活动 ```bash

    w — 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

磁盘配额管理(Disk Quotas)

  1. quota quotatool packages
  2. quotacheck edquota setquota quotaon quotaoff warnquota … commands
  3. 启动磁盘配额: 略
  4. Users and Groups

    User accounts are defined in the /etc/passwd file, and groups are defined in the /etc/group file.

    id

    print real and effective user and group IDs
    id [OPTION]... [USER]