第 4 章 认证

目录

4.1. 一般的 Unix 认证
4.2. 管理账号和密码信息
4.3. 好密码
4.4. 设立加密的密码
4.5. PAM 和 NSS
4.5.1. PAM 和 NSS 访问的配置文件
4.5.2. 现代的集中式系统管理
4.5.3. “为什么 GNU su 不支持 wheel 组”
4.5.4. 严格的密码规则
4.6. 其它的访问控制
4.6.1. sudo
4.6.2. PolicyKit
4.6.3. SELinux
4.6.4. Restricting access to some server services
4.7. Security of authentication
4.7.1. Secure password on the Internet
4.7.2. Secure Shell
4.7.3. Extra security measures for the Internet
4.7.4. Securing the root password

当用户(或程序)需要访问系统时,需要进行认证,确认身份是受信任。

[警告] 警告

PAM 的配置错误可能会锁住你的系统。你必须有一个准备好的救援 CD,或者设立一个替代的 boot 分区。为了恢复系统,你需要使用它们启动系统并纠正错误。

[警告] 警告

本章是基于 2013 年发布的 Debian 7.0 (Wheezy) 编写的,所以其内容正在变得过时。

一般的 Unix 认证由 PAM (Pluggable Authentication Modules,即可插入的验证模块) 下的 pam_unix(8) 模块提供。它的 3 个重要文件如下,其内的条目使用 “:” 分隔。


/etc/passwd” 包含下列内容。

 ...
user1:x:1000:1000:User1 Name,,,:/home/user1:/bin/bash
user2:x:1001:1001:User2 Name,,,:/home/user2:/bin/bash
 ...

passwd(5) 中所述,这个文件中被 “:” 分隔的每项含义如下。

  • 登录名

  • 密码形式说明

  • 数字形式的用户 ID

  • 数字形式的组 ID

  • 用户名或注释字段

  • 用户家目录

  • 可选的用户命令解释器

/etc/passwd” 的第二项曾经被用来保存加密后的密码。在引入了 “/etc/shadow” 后,该项被用来说明密码形式。


/etc/shadow” 包含下列内容。

 ...
user1:$1$Xop0FYH9$IfxyQwBe9b8tiyIkt2P4F/:13262:0:99999:7:::
user2:$1$vXGZLVbS$ElyErNf/agUDsm1DehJMS/:13261:0:99999:7:::
 ...

shadow(5) 中所述,这个文件中被 “:” 分隔的每项含义如下。

  • 登录名

  • 加密后的密码(开头的 “$1$” 表示使用 MD5 加密。“*” 表示无法登陆。)

  • 最后一次修改密码的时间,其表示从 1970 年 1 月 1 日起的天数

  • 允许用户再次修改密码的天数间隔

  • 用户必须修改密码的天数间隔

  • 密码失效前的天数,在此期间用户会被警告

  • 密码失效后的天数,在次期间密码依旧会被接受

  • 账号失效的时间,其表示从 1970 年 1 月 1 日起的天数

/etc/group” 包含下列内容。

group1:x:20:user1,user2

group(5) 中所述,这个文件中被 “:” 分隔的每项含义如下。

  • 组名称

  • 加密后的密码(不会被真正使用)

  • 数字形式的组 ID

  • 使用 “,” 分隔的用户名列表

[注意] 注意

/etc/gshadow” 为 “/etc/group” 提供了与 “/etc/shadow” 相似的功能,但没有被真正地使用。

[注意] 注意

The actual group membership of a user may be dynamically added if "auth optional pam_group.so" line is added to "/etc/pam.d/common-auth" and set it in "/etc/security/group.conf". See pam_group(8).

[注意] 注意

base-passwd 软件包包含了一份用户和组的官方文档:“/usr/share/doc/base-passwd/users-and-groups.html”。

下面是一些管理账号信息的重要命令。


其中的一些功能只能被 root 使用。密码和数据的加密参见 crypt(3)

[注意] 注意

On the system set up with PAM and NSS as the Debian alioth machine, the content of local "/etc/passwd", "/etc/group" and "/etc/shadow" may not be actively used by the system. Above commands are valid even under such environment.

在系统安装时建立一个账号或使用 passwd(1) 命令时,你应该选择一个好密码,它应该由 6 到 8 个字符组成,其中包含下列根据 passwd(1) 设定的每个组合中的一个或多个字符。

  • 小写字母

  • 数字 0 到 9

  • 标点符号

[警告] 警告

密码中不要使用可以猜到的词。账号名、身份证号码、电话号码、地址、生日、家庭成员或宠物的名字、字典单词、简单的字符序列(例如 “12345” 或 “qwerty”)等都是糟糕的选择。

下面是一些用于 生成加盐的加密密码 的独立工具。


现代的类 Unix 系统(例如 Debian 系统)提供 PAM (Pluggable Authentication Modules,插入式验证模块)NSS(Name Service Switch,名称服务切换) 机制给本地系统管理员,使他们能够配置自己的系统。它们的功能可以概括为以下几点。

  • PAM 给应用软件提供了一个灵活的认证机制,因此涉及到了密码数据的交换。

  • NSS 提供了一个灵活的名称服务机制,它经常被 C 标准库使用,使例如 ls(1)id(1) 这样的程序获得用户和组名称。

PAM 和 NSS 系统必须保持配置一致。

PAM 和 NSS 系统中重要的软件包如下。


  • libpam-doc 中 “The Linux-PAM System Administrators' Guide” 是了解 PAM 配置的必要文档。

  • glibc-doc-reference 中的 “System Databases and Name Service Switch” 是了解 NSS 配置的重要文档。

[注意] 注意

你可以使用 “aptitude search 'libpam-|libnss-'” 命令查看更多的相关软件包。NSS 缩写也可能意味着 “Network Security Service,网络安全服务”,它不同于 “Name Service Switch,名称服务切换”。

[注意] 注意

PAM 是为每个程序初始化环境变量为系统默认值的最基础方法。

现代的集中式系统管理可以使用集中式的轻量目录访问协议(LDAP)服务器进行部署,从而通过网络管理许多类 Unix 和 非类 Unix 系统。轻量目录访问协议的开源实现是 OpenLDAP 软件

The LDAP server provides the account information through the use of PAM and NSS with libpam-ldap and libnss-ldap packages for the Debian system. Several actions are required to enable this (I have not used this setup and the following is purely secondary information. Please read this in this context.).

  • 你通过运行一个程序,例如独立的 LDAP 守护进程 slapd(8),来建立集中式的 LDAP 服务器。

  • 你在 “/etc/pam.d/” 目录中的 PAM 配置文件里,使用 “pam_ldap.so” 替代默认值 “pam_unix.so”。

    • Debian 使用 “/etc/pam_ldap.conf” 作为 libpam-ldap 的配置文件,“/etc/pam_ldap.secret” 作为保存 root 密码的文件。

  • 你在 “/etc/nsswitch.conf” 文件中改变 NSS 配置,使用 “ldap” 替代默认值(“compat” 或 “file”)。

    • Debian 使用 “/etc/libnss-ldap.conf” 作为 libnss-ldap 的配置文件。

  • 为了密码的安全,你必须让 libpam-ldap 使用 SLL(或 TLS)连接。

  • 为了确保 LDAP 网络开销数据的完整性,你必须让 libpam-ldap 使用 SLL(或 TLS)连接。

  • 为了减少 LDAP 网络流量,你应该在本地运行 nscd(8) 来缓存任何 LDAP 搜索结果。

参见由 libpam-doc 软件包提供的 pam_ldap.conf(5) 中的文档和 “/usr/share/doc/libpam-doc/html/”,以及 glibc-doc 软件包提供的 “info libc 'Name Service Switch'”。

类似地,你可以使用其它方法来设置另一种集中式的系统。

[注意] 注意

参见 第 9.3.15 节 “Alt-SysRq 键” 来限制内核的安全警告密钥(SAK)功能。

[提示] 提示

使用 systemd 的系统中,可以使用 logind 来管理用户的登陆。参见 systemd-logind(8)

sudo(8) 程序是为了使一个系统管理员可以给用户受限的 root 权限并记录 root 活动而设计的。sudo 只需要一个普通用户的密码。安装 sudo 软件包并通过设置 “/etc/sudoers” 中的选项来使用它。参见 “/usr/share/doc/sudo/examples/sudoers” 和 第 1.1.12 节 “sudo 配置” 中的配置示例。

我将 sudo 用于单用户系统(参见 第 1.1.12 节 “sudo 配置”)是为了防止自己可能做出的愚蠢行为。就我个人而言,我认为使用 sudo 会比使用 root 账号操作系统来得好。例如,下列命令将 “<some_file>” 的拥有者改变为 “<my_name>”。

$ sudo chown <my_name> <some_file>

当然如果你知道 root 密码(比如自行安装 Debian 的用户所做的),任何用户账号都可以使用 “su -c” 让任何命令以 root 运行。

PolicyKit 是在类 Unix 操作系统中控制整个系统权限的一个操作系统组件。

Newer GUI applications are not designed to run as privileged processes. They talk to privileged processes via PolicyKit to perform administrative operations.

在 Debian 系统中,PolicyKit 限制了属于 sudo 组的用户账号的这种操作。

参见 polkit(8)

Security-Enhanced Linux (SELinux) is a framework to tighten privilege model tighter than the ordinary Unix-like security model with the mandatory access control (MAC) policies. The root power may be restricted under some conditions.

For system security, it is a good idea to disable as much server programs as possible. This becomes critical for network servers. Having unused servers, activated either directly as daemon or via super-server program, are considered security risks.

Many programs, such as sshd(8), use PAM based access control. There are many ways to restrict access to some server services.

See 第 3.2.3 节 “运行级别管理示例”, 第 3.2.4 节 “The default parameter for each init script”, 第 4.5.1 节 “PAM 和 NSS 访问的配置文件”, 第 3.2.8 节 “网络服务初始化”, and 第 5.9 节 “Netfilter infrastructure”.

[提示] 提示

Sun RPC services need to be active for NFS and other RPC based programs.

[提示] 提示

If you have problems with remote access in a recent Debian system, comment out offending configuration such as "ALL: PARANOID" in "/etc/hosts.deny" if it exists. (But you must be careful on security risks involved with this kind of action.)

[注意] 注意

The information here may not be sufficient for your security needs but it should be a good start.

The Secure Shell (SSH) program provides secure encrypted communications between two untrusted hosts over an insecure network with the secure authentication. It consists of the OpenSSH client, ssh(1), and the OpenSSH daemon, sshd(8). This SSH can be used to tunnel an insecure protocol communication such as POP and X securely over the Internet with the port forwarding feature.

The client tries to authenticate itself using host-based authentication, public key authentication, challenge-response authentication, or password authentication. The use of public key authentication enables the remote password-less login. See 第 6.9 节 “服务器远程访问和工具 (SSH)”.

To prevent people to access your machine with root privilege, you need to make following actions.

  • Prevent physical access to the hard disk

  • Lock BIOS and prevent booting from the removable media

  • Set password for GRUB interactive session

  • Lock GRUB menu from editing

With physical access to hard disk, resetting the password is relatively easy with following steps.

  1. Move the hard disk to a PC with CD bootable BIOS.

  2. Boot system with a rescue media (Debian boot disk, Knoppix CD, GRUB CD, …).

  3. Mount root partition with read/write access.

  4. Edit "/etc/passwd" in the root partition and make the second entry for the root account empty.

If you have edit access to the GRUB menu entry (see 第 3.1.2 节 “第二阶段:引载加载程序”) for grub-rescue-pc at boot time, it is even easier with following steps.

  1. Boot system with the kernel parameter changed to something like "root=/dev/hda6 rw init=/bin/sh".

  2. Edit "/etc/passwd" and make the second entry for the root account empty.

  3. Reboot system.

The root shell of the system is now accessible without password.

[注意] 注意

Once one has root shell access, he can access everything on the system and reset any passwords on the system. Further more, he may compromise password for all user accounts using brute force password cracking tools such as john and crack packages (see 第 9.4.11 节 “系统安全性和完整性检查”). This cracked password may lead to compromise other systems.

The only reasonable software solution to avoid all these concerns is to use software encrypted root partition (or "/etc" partition) using dm-crypt and initramfs (see 第 9.8 节 “Data encryption tips”). You always need password to boot the system, though.