跳转到内容
搜索 Fedora 文档

    浏览当前文档目录

    Fedora 的 SSH:服务配置、密钥登录与加固

    Fedora 上的 sshd 配置结构和其他发行版不太一样:主文件几乎是空的,真正的配置在 sshd_config.d/*.conf。再加上 crypto-policies 会接管密码套件,照抄网上教程经常改不动。

    下面是 F45 实测。

    Terminal window
    rpm -q openssh-server
    sudo grep -vE "^#|^$" /etc/ssh/sshd_config
    openssh-server-10.5p1-1.fc45.x86_64
    Include /etc/ssh/sshd_config.d/*.conf
    AuthorizedKeysFile .ssh/authorized_keys
    Subsystem sftp /usr/libexec/openssh/sftp-server

    主文件只剩三行有效内容。其余全在 drop-in 目录:

    Terminal window
    sudo ls /etc/ssh/sshd_config.d/
    40-redhat-crypto-policies.conf
    50-cloud-init.conf
    50-redhat.conf

    这意味着:你要改的东西写进一个新的 99-*.conf,不要动主文件,也不要动 40- 那个。

    crypto-policies 会接管你的加密设置

    Section titled “crypto-policies 会接管你的加密设置”

    看一下 40-redhat-crypto-policies.conf 的内容:

    Terminal window
    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 先定了。要用更严格的套件,改策略:

    Terminal window
    update-crypto-policies --show
    DEFAULT
    Terminal window
    sudo update-crypto-policies --set FUTURE # 更严格的套件集,重启生效
    sudo update-crypto-policies --set DEFAULT # 改回来

    别只看文件,看最终值:

    Terminal window
    sudo sshd -T
    Port 22
    AddressFamily any
    ListenAddress [::]:22

    sshd -T 会把包含关系、默认值全部展开成最终生效的配置。排障第一步永远是它,而不是逐个文件读。

    客户端侧也一样:

    Terminal window
    ssh -G <主机名> | grep -E "^(user|port|identityfile|proxyjump)"

    生成一对 ed25519 密钥(比 RSA 短、更快,OpenSSH 10 推荐):

    Terminal window
    ssh-keygen -t ed25519 -C "alice@laptop"
    +----[SHA256]-----+
    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHiplpcIlutsQhEAyomGV8+ntl2VIaba3xiHIsrxRY2A alice@laptop

    文件权限:

    Terminal window
    ls -l ~/.ssh/
    -rw-------. 1 labuser labuser 97 Oct 25 17:37 authorized_keys

    规则是死的:私钥 600,authorized_keys 与 config 600,.ssh 目录 700。权限过宽 sshd 会直接忽略这个文件并记录一条日志——表现为”密钥明明传上去了却还要密码”。

    放到服务器上:

    Terminal window
    ssh-copy-id alice@server

    它做的事就是把公钥追加到 ~/.ssh/authorized_keys 并修正权限。

    路径权限说明
    ~/.ssh700目录过宽 sshd 会忽略
    ~/.ssh/id_ed25519600私钥,永不外传
    ~/.ssh/id_ed25519.pub644公钥,可以随便给
    ~/.ssh/authorized_keys600服务器侧放公钥
    /etc/ssh/sshd_config600主配置(Fedora 上只剩三行)
    /etc/ssh/sshd_config.d/*.conf600真正生效的 drop-in
    /etc/ssh/ssh_host_*_key600主机私钥,重装前备份否则客户端会报指纹变化

    写进一个新文件,别改主配置:

    Terminal window
    sudo tee /etc/ssh/sshd_config.d/99-hardening.conf >/dev/null <<'EOF'
    PermitRootLogin no
    PasswordAuthentication no
    KbdInteractiveAuthentication no
    MaxAuthTries 3
    ClientAliveInterval 300
    ClientAliveCountMax 2
    EOF
    sudo sshd -t # 语法检查,务必先跑
    sudo systemctl reload sshd

    sshd -t 只校验不重启;写错时它会明确指出行号。改完不跑 -t 直接 reload,可能把自己关在门外。

    Cloud 镜像上这条已经是默认值:

    Terminal window
    sudo cat /etc/ssh/sshd_config.d/50-cloud-init.conf
    PasswordAuthentication no

    改端口要做两件事:

    Terminal window
    sudo tee /etc/ssh/sshd_config.d/99-port.conf >/dev/null <<'EOF'
    Port 2222
    EOF
    sudo sshd -t && sudo systemctl reload sshd

    别忘了防火墙和 SELinux:

    Terminal window
    sudo firewall-cmd --permanent --add-port=2222/tcp && sudo firewall-cmd --reload
    sudo semanage port -a -t ssh_port_t -p tcp 2222

    只改 sshd 不改这两处,结果是”配置好了但连不上”。详见防火墙与SELinux。

    Terminal window
    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 是两种完全不同的问题:前者是密钥不对或权限过宽,后者是用户名不存在。