当用户(或程序)需要访问系统时,需要进行认证,确认身份是受信任。
![]() |
警告 |
---|---|
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
|
(明文的)用户账号信息 |
/etc/shadow
|
-rw-r-----
|
root
|
shadow
|
安全加密的用户账号信息 |
/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
使用 “,” 分隔的用户名列表
![]() |
注意 |
---|---|
“ |
![]() |
注意 |
---|---|
如果" |
![]() |
注意 |
---|---|
|
下面是一些管理账号信息的重要命令。
表 4.3. 管理账号信息的命令
命令 | 功能 |
---|---|
getent passwd <user_name>
|
浏览 “<user_name> ” 的账号信息
|
getent shadow <user_name>
|
浏览用户 "<user_name> " 隐藏的账户信息
|
getent group <group_name>
|
浏览 “<group_name> ” 的组信息
|
passwd
|
管理账号密码 |
passwd -e
|
为激活的账号设置一次性的密码 |
chage
|
管理密码有效期信息 |
其中的一些功能只能被 root 使用。密码和数据的加密参见 crypt(3)。
![]() |
注意 |
---|---|
在设置了 PAM 和 NSS 的系统上(例如 Debian alioth 机器),本地的
“ |
在系统安装时建立一个账号或使用 passwd(1) 命令时,你应该选择一个好密码,它应该由 6 到 8 个字符组成,其中包含下列根据 passwd(1) 设定的每个组合中的一个或多个字符。
小写字母
数字 0 到 9
标点符号
![]() |
警告 |
---|---|
密码中不要使用可以猜到的词。账号名、身份证号码、电话号码、地址、生日、家庭成员或宠物的名字、字典单词、简单的字符序列(例如 “12345” 或 “qwerty”)等都是糟糕的选择。 |
下面是一些用于 生成加盐的加密密码 的独立工具。
表 4.4. 生成密码的工具
软件包 | 流行度 | 大小 | 命令 | 功能 |
---|---|---|---|---|
whois
|
V:68, I:896 | 338 |
mkpasswd
|
具备 crypt(3) 库所有特性的前端 |
openssl
|
V:777, I:988 | 1248 |
openssl passwd
|
计算密码哈希 (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:675, I:999 | 870 | 插入式验证模块(基础服务) |
libpam-ldap
|
V:3, I:17 | 251 | 允许 LDAP 接口的插入式验证模块 |
libpam-cracklib
|
V:1, I:17 | 113 | 启用 cracklib 支持的插入式验证模块 |
libpam-doc
|
I:2 | 1019 | 插入式验证模块(html 和 文本文档) |
libc6
|
V:928, 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
|
为每个服务条目设置 NSS 配置。参见 nsswitch.conf(5) |
/etc/nologin
|
通过 pam_nologin(8) 模块限制用户登陆 |
/etc/securetty
|
通过 pam_securetty(8) 模块限制 root 访问 tty |
/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 软件。
LDAP 服务器使用带有 PAM 和 NSS 的libpam-ldap
和
libnss-ldap
软件包为 Debian 系统提供账号信息。需要一些动作来启用
LDAP(我没有使用过这个设置,并且下面的信息纯粹是第二手的信息。请在这种前提下阅读下列内容。)。
你通过运行一个程序,例如独立的 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'
”。
类似地,你可以使用其它方法来设置另一种集中式的系统。
同 Windows 系统集成用户和组。
通过 winbind
和 libpam_winbind
软件包访问
Windows domain 服务.
参见 winbindd(8) 和 Integrating MS Windows Networks with Samba。
同古老的类 Unix 系统集成用户和组。
通过 nis
软件包访问 NIS
(之前叫 YP) 或 NIS+。
这是在旧的 “info su
” 底部 Richard M. Stallman 所说的一句名言。别担心:Debian
系统中当前的 su
命令使用了 PAM,这样当在
“/etc/pam.d/su
” 中启用了带有 “pam_wheel.so
”
的行后,就能够限制非 wheel 组的用户 su
到 root
组的能力。
安装 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 操作系统中控制整个系统权限的一个操作系统组件。
新的 GUI 图形界面程序,在设计的时候,不是作为特权进程来运行。它们通过 PolicyKit 来和特权进程通讯,执行管理操作。
在 Debian 系统中,PolicyKit 限制了属于 sudo
组的用户账号的这种操作。
参见 polkit(8)。
Security-Enhanced Linux (SELinux) 是一个收紧权限模块的框架,它比普通的类 Unix 安全模块 mandatory access control (MAC) 策略更严格。 root 权限在某些条件下被限制。
对系统安全而言,尽可能的禁用服务程序,是一个好的主意。网络服务是危险的。有不使用的服务,不管是直接由后台守护进程(daemon)激活,还是通过super-server 程序激活,都被认为是安全风险。
许多程序,比如说 sshd(8), 使用基于 PAM 的访问控制。也还有许多方式来限制访问一些服务端的程序。
配置文件: "/etc/default/<program_name>
"
后台守护进程(daemon)运行级别配置
super-server 使用
"/etc/inetd.conf
"
TCP wrapper 使用
"/etc/hosts.deny
" 和 "/etc/hosts.allow
"
,
tcpd(8)
Sun RPC" 使用 /etc/rpc.conf
"
atd(8)
使用 "/etc/at.allow
" 和 "/etc/at.deny
"
crontab(1)
使用 "/etc/cron.allow
" 和
"/etc/cron.deny
"
参见 第 3.2.3 节 “运行级别管理示例”, 第 3.2.4 节 “The default parameter for each init script”, 第 4.5.1 节 “PAM 和 NSS 访问的配置文件”, 第 3.2.8 节 “网络服务初始化” 和 第 5.9 节 “Netfilter 网络过滤框架”.
![]() |
提示 |
---|---|
如果你远程访问最新的 Debian 系统有问题,看下在" |
![]() |
注意 |
---|---|
这里的信息也许不够完全满足你的安全需求,但这也是一个好的开始. |
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 网络过滤框架”) together with the following secure tools may improve the security situation.
表 4.8. List of tools to provide extra security measures
软件包 | 流行度 | 大小 | 说明 |
---|---|---|---|
knockd
|
V:1, I:2 | 89 | 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 |
为阻止人们使用 root 权限访问你的机器,你需要做下面的操作。
阻止对硬盘的物理访问
锁住 BIOS 来阻止从可移动介质启动
为 GRUB 交互式会话设置密码
锁住 GRUB 菜单,禁止编辑
如果可以物理访问硬盘,则可以使用下面的步骤,相对简单的重置密码。
将硬盘拿到一个可以设置 BIOS 从CD 启动的电脑。
使用紧急介质启动系统(Debian 启动磁盘, Knoppix CD, GRUB CD, …)。
用读写访问挂载根分区。
编辑根分区的"/etc/passwd
"文件,使 root
账户条目的第二段为空。
对于 grub-rescue-pc
,即使用紧急介质启动的电脑,如果有编辑 GRUB 菜单条目 (参见 第 3.1.2 节 “第二阶段:引载加载程序”) 的权限,在启动时,使用下面的步骤更加简单。
使用内核参数启动系统来修改一些事情,比如说,"root=/dev/hda6 rw init=/bin/sh
".
编辑 "/etc/passwd
" 文件,使 root
账户条目的第二段为空。
重启系统。
系统的 root shell 现在可以无密码访问了。
![]() |
注意 |
---|---|
一旦某人拥有 root shell 访问权限,他能够访问任何内容,并可以重设系统上的任何密码。此外,他可以使用
|
为避免这些相关问题,仅有的理论上的软件解决方案是使用 dm-crypt 和
initramfs (参见 第 9.8 节 “Data encryption tips”)加密 root 分区(或
"/etc
" 分区) 。这样的话,你总是需要密码来启动系统。