账号、认证与 SSH 加固:把好第一道门
0. 元信息
- 主题路径:
docs/topics/linux-hardening/subtopics/identity-and-ssh/ - 父主题:
linux-hardening - 主分类:安全与可靠性
- 辅助分类:工程技术
- 适合对象:会用
usermod/usermod/passwd,知道ssh user@host,能读简单配置文件的运维 / 后端 / 安全工程师 - 建议周期:1~1.5 周(每周 8~10 小时,含本地 sshd 实验)
- 前置知识:
linux-dev-env;network基础 - 最终目标:能对一台 Linux 服务器完成账号清理(root 禁登、wheel sudo 限定)、密码策略(minlen=14 + 复杂度 + 失败锁定)、SSH 全面加固(仅密钥 + AllowUsers + MaxAuthTries + Banner + ChrootDirectory + ForceCommand)+ 一次可选 MFA 接入;改后能自查 CIS 1.x / 5.x 控制项
1. 学习路线
账号生命周期(usermod/usermod/usermod、UID 范围、shell、nologin)
→ /etc/passwd 与 /etc/shadow 结构(7+9 字段)
→ PAM 框架(auth/account/session/password 四类)
→ pam_pwquality(密码复杂度)+ pam_faillock(失败锁定)
→ wheel + sudoers(受限提权)
→ SSH 协议与密钥(ed25519 / RSA 4096 / ssh-agent)
→ sshd_config 关键指令(认证、登录、转发、Tunnel)
→ SSH 高级(Banner / ChrootDirectory / ForceCommand / Match)
→ MFA:Google Authenticator / YubiKey(可选)
按”先关闭弱门,再限制强门”的顺序:先关 root 直登、密码登录、telnet;再限制能用 SSH 进的人、能做的事、能转发的端口。
2. 阶段周数分配(1~1.5 周,建议 1.5 周)
| 阶段 | 1 周速通 | 1.5 周稳推 | 备注 |
|---|---|---|---|
| 1. 账号清理与 usermod | 0.5 天 | 1 天 | 含 /usr/sbin/nologin、UID 范围 |
2. /etc/shadow 与密码哈希 | 0.5 天 | 0.5 天 | chmod 000 shadow |
| 3. PAM 框架 | 0.5 天 | 0.5 天 | 读 pam.d 栈顺序 |
4. pam_pwquality 密码策略 | 0.5 天 | 1 天 | 含 /etc/security/pwquality.conf |
5. pam_faillock 失败锁定 | 0.5 天 | 0.5 天 | 输错 5 次锁 15 分钟 |
| 6. sudoers 与 wheel | 0.5 天 | 0.5 天 | visudo + sudo -l 验证 |
| 7. SSH 密钥(ed25519) | 0.5 天 | 1 天 | ssh-copy-id + ssh-agent |
| 8. sshd_config 加固 | 1 天 | 1 天 | PermitRootLogin no + AllowUsers + MaxAuthTries |
| 9. ForceCommand / ChrootDirectory / MFA | 1 天 | 1.5 天 | MFA 可选;先验证 sshd -t |
每天 1.5 小时;1 周速通每天 2 小时能跑完,但容易漏 sshd -T 验证与持久化步骤。建议 1.5 周:把 MFA 接入与故障排查(MFA 卡 OTP、AllowUsers 漏掉自己)留时间。
3. 九阶段表
| 阶段 | 核心知识 | 实践产出 | 可观察学会标准 |
|---|---|---|---|
| 1. 账号 | /etc/passwd、UID 0 唯一、/etc/skel、usermod 参数、nologin shell、UID 范围 | 创建一个 opsuser 并禁用 root 直登 | awk -F: '($3==0) {print}' 只有 root;非活动账号 shell 是 /usr/sbin/nologin |
| 2. shadow 与密码 | /etc/shadow 字段、密码哈希、chpasswd、passwd -S | 把 shadow 权限 000;root 之外的密码哈希不可见 | stat -c %a /etc/shadow = 0;grep root /etc/shadow 仍可由 root 看到 |
| 3. PAM 框架 | pam.d 目录、四类管理、include、substack、control flag(required / requisite / sufficient / optional) | 画一张 sshd 的 PAM 栈 | 知道每行 pam_xxx.so control 的执行含义 |
| 4. 密码策略 | pam_pwquality + /etc/security/pwquality.conf | 改 minlen=14 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1 | 新建用户试 7 个 1 的密码被拒;passwd -S user 显示上次设置日期 |
| 5. 失败锁定 | pam_faillock(取代 pam_tally2) | 失败 5 次锁 15 分钟;root 可手动 unlock | 输错 5 次后 faillock --user <u> 显示锁定;faillock --user <u> --reset 解锁 |
| 6. sudoers | wheel 组、/etc/sudoers、visudo、%wheel ALL=(ALL) ALL 与 NOPASSWD | 把 opsuser 加入 wheel;root 用 visudo 校验 | sudo -l -U opsuser 显示受限规则;非 wheel 用户 sudo 失败 |
| 7. SSH 密钥 | ssh-keygen -t ed25519、ssh-copy-id、authorized_keys、ssh-agent、ssh-add、-o 选项 | 用 ed25519 密钥登录成功 | 密钥无密码时仍能登录;私钥权限 600 |
| 8. sshd_config 加固 | PermitRootLogin PasswordAuthentication KbdInteractiveAuthentication ChallengeResponseAuthentication MaxAuthTries LoginGraceTime ClientAliveInterval ClientAliveCountMax AllowUsers X11Forwarding AllowTcpForwarding PermitTunnel Banner | 改完能密钥登录;密码登录被拒;root 登录被拒 | sshd -T 输出与配置一致;ssh -o PreferredAuthentications=password user@host 失败 |
| 9. SSH 高级与 MFA | ChrootDirectory ForceCommand Match 块、ForceCommand internal-sftp、MFA(Google Authenticator 或 YubiKey FIDO2) | 用 ForceCommand 限定用户只能跑指定命令;或加 MFA | 登录后 w 看 bash 不在自己家目录;或第一次密码 + 第二次 OTP |
关键陷阱:PAM 改了不重启 sshd;sshd_config 多块覆盖顺序错;关密码登录前没配密钥锁死自己;AllowUsers 把当前用户漏掉;MFA 与 ForceCommand 冲突时记错误日志。
4. 第一周任务(每天 1.5~2 小时)
Day 1 约定:用一台 Ubuntu 22.04 或 RHEL 9.x 虚机/容器,先建快照;记录发行版、内核、sshd 版本:
cat /etc/os-release、uname -r、sshd -V。所有改动必须以非 root 用户 +sudo执行;保留 console 或第二 SSH 会话,避免锁外。
| 日 | 任务 | 当天交付 | 自检命令 |
|---|---|---|---|
| Day 1 | 建 opsuser(UID 1001),禁 root 直登;usermod -m -s /usr/sbin/nologin -u 1001 opsuser;passwd -S opsuser 看状态;awk -F: '($3==0)' /etc/passwd 验证 root 唯一 | notes/day1.md 含 uid/shell 验证 | id opsuser、grep opsuser /etc/passwd、awk -F: '($3==0)' /etc/passwd | wc -l |
| Day 2 | 解读 /etc/shadow 9 字段;chmod 000 /etc/shadow + 属主 root:shadow;stat -c '%a %U %G' /etc/shadow;passwd -l testuser 看 !;passwd -u testuser 解锁 | shadow 权限清单 | ls -l /etc/shadow、stat -c '%a %U %G' /etc/shadow、passwd -S testuser |
| Day 3 | 装 libpam-pwquality;改 /etc/security/pwquality.conf(minlen=14 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1);passwd opsuser 试 7 个 1 被拒;试 Abcdef!1234 通过 | pwquality.conf diff + 拒绝/通过证据 | grep -E 'minlen|credit' /etc/security/pwquality.conf、passwd -S opsuser 看最近修改时间 |
| Day 4 | /etc/pam.d/common-auth 加 pam_faillock.so preauth deny=5 unlock_time=900;故意输错 5 次锁 opsuser;faillock --user opsuser 看状态;faillock --user opsuser --reset 解锁;root 验证 reset | notes/faillock.md 含状态截图 | faillock --user opsuser、journalctl -u sshd --since today | grep -i fail |
| Day 5 | usermod -aG wheel opsuser;visudo -c 检查后改 /etc/sudoers 加 %wheel ALL=(ALL) ALL;sudo -l -U opsuser 验证;非 wheel 用户 sudo 失败 | sudoers diff + sudo -l 输出 | groups opsuser、sudo -l -U opsuser、sudo -l -U nobody 应被拒 |
| Day 6 | ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519;ssh-copy-id opsuser@host;本机密钥登录成功;chmod 600 ~/.ssh/id_ed25519;ssh-agent + ssh-add 试免二次输 passphrase | notes/day6.md 含 ssh-copy-id 输出与登录日志 | ls -l ~/.ssh/id_ed25519、ssh-add -l、ssh -o PreferredAuthentications=publickey opsuser@localhost echo ok |
| Day 7 | 项目:在 /etc/ssh/sshd_config.d/99-hardening.conf 写入:PermitRootLogin no、PasswordAuthentication no、KbdInteractiveAuthentication no、MaxAuthTries 3、LoginGraceTime 30、ClientAliveInterval 300、ClientAliveCountMax 2、AllowUsers opsuser、X11Forwarding no、AllowTcpForwarding no、PermitTunnel no、PermitUserEnvironment no、Banner /etc/issue.net、UsePAM yes、IgnoreRhosts yes;/etc/issue.net 写”Authorized access only”;sshd -t && systemctl restart sshd;ssh -o PreferredAuthentications=password opsuser@localhost 应被拒;ssh opsuser@localhost 密钥登录成功;sshd -T | grep -Ei 'permitroot|passwordauth|maxauth|allowusers|x11forwarding|forwarding' 看最终值 | 一份 99-hardening.conf + sshd 验证输出 | sudo sshd -T(看生效值)、sudo sshd -t(看配置合法性)、ssh -v opsuser@localhost 2>&1 | grep -i auth(看走 publickey) |
Day 7 拆解
步骤 A —— 最小可用(90 分钟)
- 备份:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F); - 新建
/etc/ssh/sshd_config.d/99-hardening.conf,先放 5 条核心:PermitRootLogin no、PasswordAuthentication no、MaxAuthTries 3、AllowUsers opsuser、Banner /etc/issue.net; - 写
/etc/issue.net:Authorized access only. All activity is monitored and recorded. Violations will be prosecuted.; sudo sshd -t && sudo systemctl restart sshd,保另一会线;ssh opsuser@localhost用密钥登录应成功;ssh -o PreferredAuthentications=password opsuser@localhost应得Permission denied (publickey);sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|allowusers|maxauthtries|banner'验证生效。
步骤 B —— 强化 + 边界(30~45 分钟)
- 加
KbdInteractiveAuthentication noChallengeResponseAuthentication no,与 MFA 协同时不卡 OTP; - 加
LoginGraceTime 30ClientAliveInterval 300ClientAliveCountMax 2,挡慢攻击与死连接; - 加
X11Forwarding noAllowTcpForwarding noPermitTunnel noPermitUserEnvironment noUsePAM yesIgnoreRhosts yes; sudo sshd -t && sudo systemctl restart sshd;sudo sshd -T | grep -Ei 'x11forwarding|allowtcpforwarding|permittunnel|permiteenvironment|usepam|ignorerhosts|clientalive'看每条生效;ssh -v opsuser@localhost 2>&1 | grep -E 'auth|method'验证只走 publickey;- 把
AllowUsers临时删掉 opsuser 重启 sshd,验证自己被拒;再加回去恢复。
5. 阶段通用验收(每一阶段都通过)
- 能用 1 张表说出 CIS Benchmark 1.x(账号)与 5.x(SSH)核心控制项;
- 改完配置用
visudo -c、sshd -t、faillock --user、passwd -S、grep /etc/shadow看证据,存进notes/; - 制造 1 个对照实验:留
PasswordAuthentication yes→ 用sshpass -p wrongpass ssh opsuser@localhost攻击可见;改no→ 攻击被Permission denied (publickey); - 至少 3 组自定义数据:故意输错密码被锁、非 opsuser 试
sudo -l被拒、私钥 600 vs 644 在 ssh 时表现差异; - 记录变更:每改一个文件在
notes/changes.md写 1 条(who/when/what/why/CIS 控制项号); - 能修改已有加固清单(删/加一条 sshd 指令并解释影响),而不是只照搬。
6. 最终验收(学完 9 阶段后)
- 独立完成:账号与 PAM 改完符合 CIS 1.x 至少 8 项;sshd 改完符合 CIS 5.x 至少 15 项;能区分
pam_faillock与fail2ban的作用范围; - 完成至少 30 条 CIS 1.x/5.x 控制项的对照测试:在
notes/cis-1x5x-gap.md写 gap matrix;用sshd -T、faillock --user、awk -F: '($3==0)' /etc/passwd、sudo -l -U <user>、stat -c '%a %U %G' /etc/shadow命令验证; - 完成 1 个综合项目:在一台新机器上按 CIS 1.x/5.x 加固并 30 分钟内能恢复默认 / 或在已加固机上演练”密钥 + MFA + 应急密码”三选一回退流程;
- 用 15 分钟讲清”账号 → PAM → sudoers → sshd → MFA”五层防御各堵哪类威胁、为什么关密码登录前必须先配密钥、
AllowUsers漏掉自己为什么会锁外。
7. 综合项目
首选:写一份 cis-account-ssh.md —— 跑基线(sshd -T、awk -F: '($3==0)' /etc/passwd、grep -L nologin /etc/passwd、stat -c '%a' /etc/shadow、sudo -l -U opsuser),按 CIS 1.x/5.x 改完,用同一组命令对比前后结果,输出 diff 报告。
要求:
- 准备
notes/baseline-before.md与notes/baseline-after.md; - 逐项改配置(账号 → shadow → PAM → sudoers → sshd → MFA),每改一项就重跑对应命令;
- 关键配置持久化:
/etc/ssh/sshd_config.d/99-hardening.conf、/etc/security/pwquality.conf、/etc/pam.d/common-auth、/etc/sudoers.d/opsuser(如需单独规则); - 验证:故意输错 5 次 →
faillock --user opsuser显示锁定;非 wheel 用户sudo失败;root SSH 被拒;密码 SSH 被拒;ssh -o PreferredAuthentications=password opsuser@localhost拒绝; - 输出
notes/retrospective.md:哪些被坑过(AllowUsers 漏用户、MFA 卡 OTP、sshd_config 块覆盖)、下一步。
备选:在一台故意”老配置”的机器(如 /etc/ssh/sshd_config 默认 PermitRootLogin prohibit-password、PasswordAuthentication yes)上接入 Google Authenticator MFA,验证 OTP 第二次验证生效。
任何综合项目都必须包含:
- 需求与成功标准(含 CIS 1.x / 5.x 控制项号 + 期望证据命令);
- 基线(前)vs 目标(后)差距表;
- 配置按层(账号 / PAM / sudoers / sshd / MFA);
- 脚本化(
scripts/harden-account-ssh.sh+scripts/verify-account-ssh.sh),幂等可重跑; - 正常 + 异常验证(关掉加固 →
sshpass攻击成功;打开 →Permission denied (publickey)); - 报告:基线 / 目标 / 结果 / 复盘;
- README;
- 复盘记录。
本主题贡献(Loop 6-D · linux-hardening / identity-and-ssh)
本主题交付一套”账号 + PAM + sudoers + sshd + MFA”五层加固脚本化包,能直接灌到 RHEL / Ubuntu 双发行版并验证 sshpass 攻击被拦。
3 项核心职责
- 账号 + PAM 加固:禁用 root 直登 / 弱口令 / 长期不活动账号,配 PAM 的
pam_pwquality(密码复杂度)+pam_faillock(登录失败锁定)+pam_lastlog(长期不活动检查),按 CIS 5.x 控制项对账。 - sshd + 密钥策略:
/etc/ssh/sshd_config强制PermitRootLogin no/PasswordAuthentication no/KbdInteractiveAuthentication no/AllowGroups sshusers,配ed25519/ecdsa密钥(禁 DSA / RSA < 3072),用sshd -T校验生效。 - MFA + sudoers:用 Google Authenticator 配 TOTP(
/etc/pam.d/sshd+pam_google_authenticator.so),配合sudoers仅放行 wheel 组 + 单命令 +Defaults log_output。
4 项交付物
- 基线 vs 目标差距表:对照 CIS 1.x / 5.x 控制项号,每项配期望证据命令(
passwd -S/faillock --user/sshd -T/sudo -ll)。 - 账号 / PAM / sudoers 配置三层落档:
/etc/pam.d/login+/etc/pam.d/sshd+/etc/security/pwquality.conf+/etc/security/faillock.conf+/etc/sudoers+/etc/sudoers.d/。 - sshd_config + 密钥策略:
/etc/ssh/sshd_config(含Match块)+ 客户端 ed25519 / ecdsa 密钥生成脚本 + 旧 DSA / RSA < 3072 密钥拒绝规则,配sshd -T校验。 - MFA + 攻击验证脚本:
scripts/harden-account-ssh.sh(幂等)+scripts/verify-account-ssh.sh(用 sshpass 暴力验证被拦,Permission denied (publickey)),含 TOTP 第二次验证演示。
3 个验收指标
passwd -S全账号无NP/L标记,faillock --user <u>失败 ≥ 5 次后锁定。sshd -T输出符合加固清单(permitrootlogin no/passwordauthentication no/pubkeyacceptedalgorithms.*ssh-ed25519在列),sshpass 攻击失败率 100%。- MFA:开 TOTP 后首次 SSH 走 publickey + keyboard-interactive,OTP 错误立即断连;sudo 仅 wheel + log_output 命中。
8. 推荐开源资料(按角色分工,避免堆链接)
| 阶段 | 角色 | 资料 | 链接 | 用法 |
|---|---|---|---|---|
| 1~9 | PAM / SSH 权威 man page | man pam.d(5)、man sshd_config(5)、man pam_pwquality(8)、man pam_faillock(8)、man sudoers(5) | https://man7.org/linux/man-pages/ | 字段解释第一站;先 man 后 wiki |
| 1~9 | CIS 1.x / 5.x 基线 | CIS Linux Benchmark v3.0.0 | https://www.cisecurity.org/ | 注册免费账号下载;逐条对照 |
| 1~4 | PAM 配置实例 | Arch Wiki: PAM | https://wiki.archlinux.org/title/PAM | 字段含义;用 pam-auth-update 生成基线 |
| 5 | sshd_config 实战 | Red Hat Security Guide: SSH | https://docs.redhat.com/en/documentation/ | sshd_config 块覆盖顺序;Match 块 |
| 6 | sudoers 写法 | Arch Wiki: Sudo | https://wiki.archlinux.org/title/Sudo | User_Alias Runas_Alias Cmnd_Alias |
| 7 | OpenSSH 密钥与 agent | OpenSSH Manual | https://www.openssh.com/manual.html | ssh-keygen / ssh-agent / -o 选项 |
| 9 | MFA 接入 | Google Authenticator PAM | https://github.com/google/google-authenticator | libpam-google-authenticator.so 接 common-auth |
| 9 | FIDO2 / YubiKey | ssh-keygen -O webauthn | https://man.openbsd.org/ssh-keygen | FIDO2 resident key;硬件密钥 |
| 9 | SSH 高级 | O’Reilly《SSH The Secure Shell: The Definitive Guide》 | O’Reilly 出版 | sshd_config / 密钥 / 隧道系统讲解 |
| 全部 | 攻击者视角 | Hanno Böck《Linux Hardening in Hostile Networks》 | https://michael-wolf.de/ | 解释 CIS 为什么这么定;攻击者链 |
| 全部 | 命令速查 | Daniel J. Barrett《Linux Pocket Guide》(O’Reilly) | O’Reilly 出版 | ssh / ssh-keygen / passwd 速查 |
| 全部 | 中文友好 | 鸟哥《鸟哥的 Linux 私房菜》基础篇 | 出版社公开渠道 | sshd / PAM / 账号章节中文环境入门 |
许可证提示:CIS Benchmarks 注册免费账号下载;Google Authenticator Apache-2.0 可直接用;OpenSSH 是 BSD 风格(保留版权即可);Linux-PAM 是 BSD/GPL 双许可。复制 CIS 控制点脚本到生产前先在 staging 跑一遍 —— 默认读思路后自己写。
默认使用顺序:
manpage → Arch Wiki / Red Hat Security Guide → CIS Linux Benchmark(注册下载)→ OpenSSH 官方 Manual → MFA 实现(Google Authenticator / YubiKeyssh-keygen -O webauthn)→ 复盘到notes/retrospective.md。
9. 学习资料汇聚(v0.3 自包含)
本节由本计划生成。
9.1 背景与动机
Linux 服务器 90% 的初始攻击面都从”远程登录”开始:SSH 爆破、弱密码、root 直登、空密码账号、泄露的密钥。账号与 SSH 加固是回报率最高的一层:把 80% 的脚本小子挡在门外。
账号层的核心是最小化:root 不直登、密码够长够杂、失败会锁、sudo 受限、用户唯一 UID、可审计的登录历史。SSH 层的核心是拒绝弱认证:只接受密钥、限制来源、限制动作、可见法律告示。
9.2 概念地图
flowchart TB
PAM[PAM 框架]
PwQual[pam_pwquality]
Faillock[pam_faillock]
Unix[pam_unix]
Limits[pam_limits]
Nologin[pam_nologin]
Sudo[sudoers / wheel]
Passwd[/etc/passwd / shadow/]
UserMgr[usermod / usermod / usermod]
SSH[sshd]
Key[authorized_keys / ssh-agent]
Config[sshd_config]
MFA[Google Authenticator / YubiKey]
Banner[Banner / Match]
PAM --> PwQual
PAM --> Faillock
PAM --> Unix
PAM --> Limits
PAM --> Nologin
UserMgr --> Passwd
Sudo --> PAM
SSH --> Config
SSH --> Key
SSH --> MFA
SSH --> Banner
Banner --> Chroot[ChrootDirectory / ForceCommand]
关系:账号 → PAM → sshd → 密钥 / MFA。ChrootDirectory 和 ForceCommand 都在 sshd 层作用,把用户活动范围压缩到最小。
9.3 基础知识讲解
9.3.1 主推资料
| 资料 | 角色 | 评分 |
|---|---|---|
man 5 sshd_config | sshd 字段权威 | 5 |
man 5 pam.d | PAM 框架 | 5 |
| Arch Wiki: SSH / PAM / Security | 字段级解释 | 4 |
| Daniel J. Barrett, Linux Pocket Guide | 速查 | 4 |
| Hanno Böck, Linux Hardening in Hostile Networks | 攻击者视角 | 4 |
| O’Reilly SSH The Secure Shell | sshd 系统讲解 | 4 |
9.3.2 备查
| 资料 | 链接 |
|---|---|
| Red Hat: Configuring sudo | https://docs.redhat.com/en/documentation/ |
| Ubuntu: SSH/OpenSSH/Configuring | https://help.ubuntu.com/ |
| CIS Linux Benchmark 1.x 5.x | https://www.cisecurity.org/ |
| Google Authenticator PAM | https://github.com/google/google-authenticator |
| YubiKey / FIDO2 / ssh-keygen -O webauthn | https://man.openbsd.org/ssh-keygen |
| OpenSSH release notes | https://www.openssh.com/ |
9.3.3 必查 man pages
passwd(5)/shadow(5)/passwd(1)usermod(8)/usermod(8)/usermod(8)/chage(1)pam.d(5)/pam_pwquality(8)/pam_faillock(8)/pam_unix(8)sudoers(5)/visudo(8)/sudo(8)sshd(8)/sshd_config(5)/ssh(1)/ssh-keygen(1)/ssh-agent(1)faillock(8)/pwquality.conf(5)
9.4 经典问题与经典案例
| # | 问题 | 为什么会重要 | 最简答案 / 证据 |
|---|---|---|---|
| 1 | root UID 不唯一 | 多 root 账号 = 提权 | awk -F: '($3==0) {print}' /etc/passwd 应只 1 行 |
| 2 | shadow 权限非 000 | 任何用户可爆破 | chmod 000 /etc/shadow;属主 root |
| 3 | PermitRootLogin yes | root 拿到即最高 | no;或 prohibit-password |
| 4 | PasswordAuthentication yes | 密钥 + 密码共存 | no |
| 5 | PermitEmptyPasswords yes | 空密码登录 | no |
| 6 | 没配 MaxAuthTries | 无限次尝试 | 3 |
| 7 | 没设 LoginGraceTime | 慢攻击拖死 sshd | 30 秒 |
| 8 | ClientAliveInterval 0 | 死连接保留 | 300 / ClientAliveCountMax 2 |
| 9 | AllowUsers 漏掉自己 | 把自己锁外面 | 改前保 console |
| 10 | X11Forwarding yes 不需要 | 套接字泄露 | no |
| 11 | AllowTcpForwarding yes 默认 | 内网穿透 | no |
| 12 | PermitTunnel yes | 用户开 VPN | no |
| 13 | PermitUserEnvironment yes | 攻击者注环境变量 | no |
| 14 | 私钥 644 权限 | 私钥被同用户读 | chmod 600 |
| 15 | 私钥放仓库 | 泄露 | 不放;用 ssh-agent |
| 16 | Banner 没设 | 法律告示缺失 | 设 /etc/issue.net |
| 17 | UsePAM no | sshd 绕过 PAM | yes |
| 18 | IgnoreRhosts yes 没显式 | 信任 .rhosts | yes |
| 19 | 失败 5 次没锁 | 暴力破解无代价 | pam_faillock |
| 20 | wheel 空 | 任何用户 sudo | 只把 opsuser 加进去 |
| 21 | NOPASSWD: ALL | 物理接触即提权 | 必须输密码;或限定命令 |
| 22 | !/bin/false 写成 !/bin/false 错 | shell 仍能登 | /usr/sbin/nologin |
| 23 | 旧账号 games news 还在 | 攻击面变大 | 锁或删 |
| 24 | TMOUT 没设 | 空闲 shell 不超时 | /etc/profile 加 TMOUT=300 |
| 25 | sudo 日志没集中 | IR 看不到提权记录 | 默认 /var/log/auth.log;转发到 SIEM |
| 26 | MFA secret 在仓库 | OTP 失守 | 单独保管 + 备份 |
| 27 | Chroot 路径权限错 | sshd 拒绝启动 | chroot 目录属主 root,权限 755 |
| 28 | ForceCommand 与 MFA 冲突 | 卡在 OTP 阶段 | ForceCommand 在 PAM 后;测试 |
9.5 学习难点
概念难点
| 难点 | 为什么会卡 | 突破路径 |
|---|---|---|
| PAM 控制位 (required/requisite/sufficient/optional) | 一行失败行为不同 | 用 pam-auth-update 重建;查 man |
shadow 第二字段 !/*/!! 含义 | 锁/未设/锁 | 查 shadow(5);passwd -l 加 ! |
sudoers NOPASSWD 顺序 | 后写覆盖前 | 看完整 sudo -l 输出 |
| sshd_config 块覆盖 | Match 块与全局冲突 | 用 sshd -T 看最终生效值 |
| ForceCommand 与 Chroot 一起 | 路径相对 chroot | 路径相对 chroot 根 |
思维难点
| 难点 | 为什么会卡 | 突破路径 |
|---|---|---|
| 关密码登录前没配密钥 | 锁自己 | 提前 console / 第二用户 |
| PAM 改了不重启 | 看似不生效 | 重启 sshd;systemctl restart sshd |
| 失败锁 vs IP 封禁 | faillock 锁用户,fail2ban 锁 IP | 两者都用,但前者更精确 |
工程难点
| 难点 | 为什么会卡 | 突破路径 |
|---|---|---|
| 几百台机器统一 sshd_config | 手工不可行 | Ansible / cloud-init / 配置管理 |
| 老系统 PAM 结构差异 | RHEL vs Ubuntu 模块路径不同 | 用 pam-auth-update 生成基线 |
| MFA 集成 | TOTP 库 / YubiKey 接入 | 选 Google Authenticator + pam_google_authenticator.so |
9.6 技术标准与接口
9.6.1 Entity
| 名称 | 版本 | 组织 | 状态 | 许可证 |
|---|---|---|---|---|
| OpenSSH | 9.x | OpenBSD 项目 | 活跃 | BSD |
| Linux-PAM | 1.5+ | linux-pam 项目 | 活跃 | BSD/GPL 双许可 |
| sudo | 1.9+ | Todd Miller 等 | 活跃 | ISC |
| pam_pwquality | 1.4+ | pwquality 项目 | 活跃 | BSD/GPL |
| pam_faillock | 0.5+ | Linux PAM | 活跃 | BSD/GPL |
| Google Authenticator PAM | 1.0+ | 维护 | Apache-2.0 | |
| FIDO2 / WebAuthn | 2 | FIDO Alliance / W3C | 现行 | 公开 |
| CIS Linux Benchmark 1.x / 5.x | v2.0 | CIS | 现行 | 注册免费 |
| POSIX 1003.1 | 2017 | IEEE / The Open Group | 现行 | 收费 / draft 公开 |
9.6.2 Scope
- OpenSSH:SSH 协议实现 + 客户端 + 工具。
- PAM:可插拔认证模块框架;服务通过它调用后端。
- sudo / sudoers:受限提权。
- pwquality / faillock:具体策略模块。
- CIS 1.x:账号 / 配置;CIS 5.x:SSH 配置。
- 这些标准不替代定期审计(
lynis/oscap)。
9.6.3 Structure
sshd_config关键指令(见 §0/§3)。- PAM 文件结构:
/etc/pam.d/<service>顺序敏感。 sudoers关键指令:User_AliasRunas_AliasCmnd_AliasHost_AliasNOPASSWDPASSWD。/etc/shadow字段:username:password:lastchg:min:max:warn:inactive:expire:reserved。
9.6.4 Ecosystem
- 配置管理:Ansible(含
ansible.builtin.lineinfile/community.general.sudoers)。 - 密钥管理:HashiCorp Vault、SSH Bastion(Teleport / Boundary / Tailscale SSH)。
- MFA:Google Authenticator、YubiKey、PrivacyIDEA、FreeIPA。
- 集中认证:LDAP、FreeIPA、Active Directory、SSSD。
- 集中日志:rsyslog / syslog-ng → SIEM。
9.6.5 Depth Tiers
| 层级 | 能力 |
|---|---|
| L0 | 知道 root / sudo / ssh |
| L1 | 看得懂一段 sshd_config 和 sudoers |
| L2 | 能正确配置 sshd 限制策略、加入 MFA |
| L3 | 能解释 PAM 行为、SSH 攻击向量、sudo 提权限制 |
| L4 | 能为多机集群设计统一登录基线、密钥轮换、应急解锁流程 |
本计划目标:L3。
9.6.6 Source
- OpenSSH:https://www.openssh.com/
- Linux-PAM:https://github.com/linux-pam/linux-pam
- sudo:https://www.sudo.ws/
- CIS Benchmarks:https://www.cisecurity.org/
- Arch Wiki:https://wiki.archlinux.org/
- 引用版本快照日期:2026-07-30。
10. 常见误区
- 只改
PermitRootLogin不改PasswordAuthentication;密码还在用; AllowUsers没把自己算进去;改完 sshd 重启后失联;MaxAuthTries 3后忘了配LoginGraceTime,慢连接拖死;pam_faillock装上不写到pam.d/common-auth;faillock --reset在攻击路径上:root 不可信时不能盲 reset;- sudoers 不用
visudo;改坏直接失联; - 把私钥 600 改为 644 让 web 进程读,私钥被多用户访问;
- 私钥免密 (passphrase 空) 仍放仓库,等于没设密钥;
- MFA 配完没备份 secret;手机丢了进不去;
Banner当成装饰,写 “Welcome to My Server”;CIS / 法律要求是”未经授权禁止访问”;- 用
PermitUserEnvironment yes给 CI 跑工具,给攻击者也开门; - sshd 没重启只 reload,新配置没生效;
- chroot 目录权限 755 但属主非 root,sshd 拒绝;
Match块放错位置或与全局冲突;UsePAM no后看似更”安全”,实际绕过 PAM 策略。
11. 所有知识点分类(统一规则)
- 编程语言
- 数据结构与算法
- 计算机基础
- 工程技术
- Web 与后端
- 前端与客户端
- 数据与人工智能
- 项目与职业能力
- 安全与可靠性
本计划归属:安全与可靠性 主 + 工程技术 辅。