目录
当用户(或程序)需要访问系统时,需要进行认证,确认身份是受信任。
![]() |
警告 |
---|---|
PAM 的配置错误可能会锁住你的系统。你必须有一个准备好的救援 CD,或者设立一个替代的 boot 分区。为了恢复系统,你需要使用它们启动系统并纠正错误。 |
![]() |
警告 |
---|---|
本章是基于 2013 年发布的 Debian 7.0 ( |
一般的 Unix 认证由 PAM (Pluggable
Authentication Modules,即可插入的验证模块) 下的
pam_unix(8)
模块提供。它的 3 个重要文件如下,其内的条目使用 “:
” 分隔。
表 4.1. pam_unix(8) 使用的 3 个重要配置文件
文件 | 权限 | 用户 | 组 | 说明 |
---|---|---|---|---|
/etc/passwd
|
-rw-r--r--
|
root
|
root
|
(sanitized) user account information |
/etc/shadow
|
-rw-r-----
|
root
|
shadow
|
secure user account information |
/etc/group
|
-rw-r--r--
|
root
|
root
|
组信息 |
“/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
使用 “,” 分隔的用户名列表
![]() |
注意 |
---|---|
“ |
![]() |
注意 |
---|---|
The actual group membership of a user may be dynamically added if
" |
![]() |
注意 |
---|---|
|
下面是一些管理账号信息的重要命令。
表 4.3. 管理账号信息的命令
命令 | 功能 |
---|---|
getent passwd <user_name>
|
浏览 “<user_name> ” 的账号信息
|
getent shadow <user_name>
|
browse shadowed account information of
"<user_name> "
|
getent group <group_name>
|
浏览 “<group_name> ” 的组信息
|
passwd
|
管理账号密码 |
passwd -e
|
为激活的账号设置一次性的密码 |
chage
|
管理密码有效期信息 |
其中的一些功能只能被 root 使用。密码和数据的加密参见 crypt(3)。
![]() |
注意 |
---|---|
On the system set up with PAM and NSS as the Debian alioth machine, the content of local
" |
在系统安装时建立一个账号或使用 passwd(1) 命令时,你应该选择一个好密码,它应该由 6 到 8 个字符组成,其中包含下列根据 passwd(1) 设定的每个组合中的一个或多个字符。
小写字母
数字 0 到 9
标点符号
![]() |
警告 |
---|---|
密码中不要使用可以猜到的词。账号名、身份证号码、电话号码、地址、生日、家庭成员或宠物的名字、字典单词、简单的字符序列(例如 “12345” 或 “qwerty”)等都是糟糕的选择。 |
下面是一些用于 生成加盐的加密密码 的独立工具。
表 4.4. 生成密码的工具
软件包 | 流行度 | 大小 | 命令 | 功能 |
---|---|---|---|---|
whois
|
V:67, I:898 | 266 |
mkpasswd
|
over-featured front end to the crypt(3) library |
openssl
|
V:772, I:987 | 1127 |
openssl passwd
|
compute password hashes (OpenSSL). passwd(1ssl) |
现代的类 Unix 系统(例如 Debian 系统)提供 PAM (Pluggable Authentication Modules,插入式验证模块) 和 NSS(Name Service Switch,名称服务切换) 机制给本地系统管理员,使他们能够配置自己的系统。它们的功能可以概括为以下几点。
PAM 给应用软件提供了一个灵活的认证机制,因此涉及到了密码数据的交换。
NSS 提供了一个灵活的名称服务机制,它经常被 C 标准库使用,使例如 ls(1) 和 id(1) 这样的程序获得用户和组名称。
PAM 和 NSS 系统必须保持配置一致。
PAM 和 NSS 系统中重要的软件包如下。
表 4.5. PAM 和 NSS 系统中重要的软件包
软件包 | 流行度 | 大小 | 说明 |
---|---|---|---|
libpam-modules
|
V:693, I:999 | 870 | 插入式验证模块(基础服务) |
libpam-ldap
|
V:3, I:18 | 251 | 允许 LDAP 接口的插入式验证模块 |
libpam-cracklib
|
V:1, I:17 | 113 | 启用 cracklib 支持的插入式验证模块 |
libpam-doc
|
I:2 | 1019 | 插入式验证模块(html 和 文本文档) |
libc6
|
V:927, I:998 | 10670 | GNU C 库:同样提供“名称服务切换”服务的共享库 |
glibc-doc
|
I:14 | 3126 | GNU C 库:帮助页面 |
glibc-doc-reference
|
I:6 | 11934 | GNU C 库:参考手册,有 info、pdf 和 html 格式(non-free) |
libnss-mdns
|
I:565 | 127 | 用于解析组播 DNS 名称的 NSS 模块 |
libnss-ldap
|
I:16 | 255 | NSS 模块,用于使用 LDAP 作为一个名称服务的 |
libnss-ldapd
|
I:21 | 143 |
NSS 模块,用于使用 LDAP 作为一个名称服务的(libnss-ldap 的新 fork)
|
libpam-doc
中 “The Linux-PAM System Administrators' Guide”
是了解 PAM 配置的必要文档。
glibc-doc-reference
中的 “System Databases and Name Service
Switch” 是了解 NSS 配置的重要文档。
![]() |
注意 |
---|---|
你可以使用 “ |
![]() |
注意 |
---|---|
PAM 是为每个程序初始化环境变量为系统默认值的最基础方法。 |
下面是一些 PAM 和 NSS 访问的重要配置文件。
表 4.6. PAM 和 NSS 访问的配置文件
配置文件 | 功能 |
---|---|
/etc/pam.d/<program_name>
|
为 “<program_name> ” 程序设置 PAM 配置;参加
pam(7)
和
pam.d(5)
|
/etc/nsswitch.conf
|
set up NSS configuration with the entry for each service. See nsswitch.conf(5) |
/etc/nologin
|
通过 pam_nologin(8) 模块限制用户登陆 |
/etc/securetty
|
limit the tty for the root access by the pam_securetty(8) module |
/etc/security/access.conf
|
通过 pam_access(8) 模块设置访问限制 |
/etc/security/group.conf
|
通过 pam_group(8) 模块设置基于组的限制 |
/etc/security/pam_env.conf
|
通过 pam_env(8) 模块设置环境变量 |
/etc/environment
|
通过带有 “readenv=1 ” 参数的
pam_env(8)
模块设置额外的环境变量
|
/etc/default/locale
|
通过带有 “readenv=1 envfile=/etc/default/locale ” 参数的
pam_env(8)
模块设置语言环境值(在 Debian 系统中)
|
/etc/security/limits.conf
|
通过 pam_linits(8) 模块设置资源限制(ulimit、core 等等) |
/etc/security/time.conf
|
通过 pam_time(8) 模块设置时间限制 |
密码选择的限制是通过 PAM 模块 pam_unix(8) 和 pam_cracklib(8) 来实现的。它们可以通过各自的参数进行配置。
![]() |
提示 |
---|---|
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'
”。
类似地,你可以使用其它方法来设置另一种集中式的系统。
Integration of user and group with the Windows system.
Access Windows domain services by the
winbind
and libpam_winbind
packages.
参见 winbindd(8) 和 Integrating MS Windows Networks with Samba。
Integration of user and group with the legacy Unix-like system.
Access NIS (originally called
YP) or NIS+ by the nis
package.
This is the famous phrase at the bottom of the old "info
su
" page by Richard M. Stallman. Not to worry: the current
su
command in Debian uses PAM, so that one can restrict
the ability to use su
to the root
group by enabling the line with "pam_wheel.so
" in
"/etc/pam.d/su
".
安装 libpam-cracklib
软件包你能够强制使用严格的密码规则,例如,通过在
“/etc/pam.d/common-password
” 中添加下列行。
对于 squeeze
发行版:
password required pam_cracklib.so retry=3 minlen=9 difok=3 password [success=1 default=ignore] pam_unix.so use_authtok nullok md5 password requisite pam_deny.so password required pam_permit.so
![]() |
注意 |
---|---|
参见 第 9.3.15 节 “Alt-SysRq 键” 来限制内核的安全警告密钥(SAK)功能。 |
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.
configuration files: "/etc/default/<program_name>
"
runlevel configuration for daemon
"/etc/inetd.conf
" for super-server
"/etc/hosts.deny
" and
"/etc/hosts.allow
" for TCP
wrapper,
tcpd(8)
"/etc/rpc.conf
" for Sun RPC
"/etc/at.allow
" and "/etc/at.deny
" for
atd(8)
"/etc/cron.allow
" and "/etc/cron.deny
"
for
crontab(1)
Network firewall of netfilter infrastructure
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”.
![]() |
提示 |
---|---|
If you have problems with remote access in a recent Debian system, comment
out offending configuration such as "ALL: PARANOID" in
" |
![]() |
注意 |
---|---|
The information here may not be sufficient for your security needs but it should be a good start. |
Many popular transportation layer services communicate messages including password authentication in the plain text. It is very bad idea to transmit password in the plain text over the wild Internet where it can be intercepted. You can run these services over "Transport Layer Security" (TLS) or its predecessor, "Secure Sockets Layer" (SSL) to secure entire communication including password by the encryption.
表 4.7. List of insecure and secure services and ports
insecure service name | port | secure service name | port |
---|---|---|---|
www (http) | 80 | https | 443 |
smtp (mail) | 25 | ssmtp (smtps) | 465 |
ftp-data | 20 | ftps-data | 989 |
ftp | 21 | ftps | 990 |
telnet | 23 | telnets | 992 |
imap2 | 143 | imaps | 993 |
pop3 | 110 | pop3s | 995 |
ldap | 389 | ldaps | 636 |
The encryption costs CPU time. As a CPU friendly alternative, you can keep communication in plain text while securing just the password with the secure authentication protocol such as "Authenticated Post Office Protocol" (APOP) for POP and "Challenge-Response Authentication Mechanism MD5" (CRAM-MD5) for SMTP and IMAP. (For sending mail messages over the Internet to your mail server from your mail client, it is recently popular to use new message submission port 587 instead of traditional SMTP port 25 to avoid port 25 blocking by the network provider while authenticating yourself with CRAM-MD5.)
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)”.
Even when you run secure services such as Secure Shell (SSH) and Point-to-point tunneling protocol (PPTP) servers, there are still chances for the break-ins using brute force password guessing attack etc. from the Internet. Use of the firewall policy (see 第 5.9 节 “Netfilter infrastructure”) together with the following secure tools may improve the security situation.
表 4.8. List of tools to provide extra security measures
软件包 | 流行度 | 大小 | 说明 |
---|---|---|---|
knockd
|
V:0, I:2 | 164 | small port-knock daemon knockd(1) and client konck(1) |
fail2ban
|
V:92, I:103 | 1253 | ban IPs that cause multiple authentication errors |
libpam-shield
|
V:0, I:0 | 106 | lock out remote attackers trying password guessing |
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.
Move the hard disk to a PC with CD bootable BIOS.
Boot system with a rescue media (Debian boot disk, Knoppix CD, GRUB CD, …).
Mount root partition with read/write access.
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.
Boot system with the kernel parameter changed to something like
"root=/dev/hda6 rw init=/bin/sh
".
Edit "/etc/passwd
" and make the second entry for the
root
account empty.
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
|
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.