Fedora 的 SSH:服务配置、密钥登录与加固
Fedora 上的 sshd 配置结构和其他发行版不太一样:主文件几乎是空的,真正的配置在 sshd_config.d/*.conf。再加上 crypto-policies 会接管密码套件,照抄网上教程经常改不动。
下面是 F45 实测。
版本与配置在哪
Section titled “版本与配置在哪”rpm -q openssh-serversudo grep -vE "^#|^$" /etc/ssh/sshd_configopenssh-server-10.5p1-1.fc45.x86_64
Include /etc/ssh/sshd_config.d/*.confAuthorizedKeysFile .ssh/authorized_keysSubsystem sftp /usr/libexec/openssh/sftp-server主文件只剩三行有效内容。其余全在 drop-in 目录:
sudo ls /etc/ssh/sshd_config.d/40-redhat-crypto-policies.conf50-cloud-init.conf50-redhat.conf这意味着:你要改的东西写进一个新的 99-*.conf,不要动主文件,也不要动 40- 那个。
crypto-policies 会接管你的加密设置
Section titled “crypto-policies 会接管你的加密设置”看一下 40-redhat-crypto-policies.conf 的内容:
sudo cat /etc/ssh/sshd_config.d/40-redhat-crypto-policies.conf# This system is following system-wide crypto policy. The changes to# crypto properties (Ciphers, MACs, ...) will not have any effect in# this or following included files. ...也就是说:在 sshd_config.d/ 里写 Ciphers、MACs、KexAlgorithms 基本无效,因为系统级 crypto policy 先定了。要用更严格的套件,改策略:
update-crypto-policies --showDEFAULTsudo update-crypto-policies --set FUTURE # 更严格的套件集,重启生效sudo update-crypto-policies --set DEFAULT # 改回来看真正生效的配置
Section titled “看真正生效的配置”别只看文件,看最终值:
sudo sshd -TPort 22AddressFamily anyListenAddress [::]:22sshd -T 会把包含关系、默认值全部展开成最终生效的配置。排障第一步永远是它,而不是逐个文件读。
客户端侧也一样:
ssh -G <主机名> | grep -E "^(user|port|identityfile|proxyjump)"生成一对 ed25519 密钥(比 RSA 短、更快,OpenSSH 10 推荐):
ssh-keygen -t ed25519 -C "alice@laptop"+----[SHA256]-----+ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHiplpcIlutsQhEAyomGV8+ntl2VIaba3xiHIsrxRY2A alice@laptop文件权限:
ls -l ~/.ssh/-rw-------. 1 labuser labuser 97 Oct 25 17:37 authorized_keys规则是死的:私钥 600,authorized_keys 与 config 600,.ssh 目录 700。权限过宽 sshd 会直接忽略这个文件并记录一条日志——表现为”密钥明明传上去了却还要密码”。
放到服务器上:
ssh-copy-id alice@server它做的事就是把公钥追加到 ~/.ssh/authorized_keys 并修正权限。
关键文件与权限一览
Section titled “关键文件与权限一览”| 路径 | 权限 | 说明 |
|---|---|---|
~/.ssh | 700 | 目录过宽 sshd 会忽略 |
~/.ssh/id_ed25519 | 600 | 私钥,永不外传 |
~/.ssh/id_ed25519.pub | 644 | 公钥,可以随便给 |
~/.ssh/authorized_keys | 600 | 服务器侧放公钥 |
/etc/ssh/sshd_config | 600 | 主配置(Fedora 上只剩三行) |
/etc/ssh/sshd_config.d/*.conf | 600 | 真正生效的 drop-in |
/etc/ssh/ssh_host_*_key | 600 | 主机私钥,重装前备份否则客户端会报指纹变化 |
最小加固清单
Section titled “最小加固清单”写进一个新文件,别改主配置:
sudo tee /etc/ssh/sshd_config.d/99-hardening.conf >/dev/null <<'EOF'PermitRootLogin noPasswordAuthentication noKbdInteractiveAuthentication noMaxAuthTries 3ClientAliveInterval 300ClientAliveCountMax 2EOFsudo sshd -t # 语法检查,务必先跑sudo systemctl reload sshdsshd -t 只校验不重启;写错时它会明确指出行号。改完不跑 -t 直接 reload,可能把自己关在门外。
Cloud 镜像上这条已经是默认值:
sudo cat /etc/ssh/sshd_config.d/50-cloud-init.confPasswordAuthentication no端口与防火墙
Section titled “端口与防火墙”改端口要做两件事:
sudo tee /etc/ssh/sshd_config.d/99-port.conf >/dev/null <<'EOF'Port 2222EOFsudo sshd -t && sudo systemctl reload sshd别忘了防火墙和 SELinux:
sudo firewall-cmd --permanent --add-port=2222/tcp && sudo firewall-cmd --reloadsudo semanage port -a -t ssh_port_t -p tcp 2222只改 sshd 不改这两处,结果是”配置好了但连不上”。详见防火墙与SELinux。
ss -tlnp | grep :22 # 1) 端口在听吗sudo sshd -T | head -5 # 2) 生效配置是什么sudo sshd -t # 3) 配置文件语法对了吗journalctl -u sshd -n 30 --no-pager # 4) 认证失败的原因sudo ausearch -m AVC -ts recent # 5) 是不是 SELinux 拦的第 4 步的日志里,Failed publickey for alice 和 Invalid user 是两种完全不同的问题:前者是密钥不对或权限过宽,后者是用户名不存在。