第 23 章 安全网络


23.1. 使用 OpenSSH 的两个系统间使用安全通讯

SSH(Secure Shell)是一种协议,它使用客户端-服务器架构在两个系统之间提供安全通信,并允许用户远程登录到服务器主机系统。与其他远程通信协议(如 FTP 或 Telnet )不同,SSH 会加密登录会话,这可防止入侵者从连接中收集未加密的密码。

23.1.1. 生成 SSH 密钥对

您可以使用在本地系统上生成的 SSH 密钥对,并将生成的公钥复制到 OpenSSH 服务器来在不输入密码的情况下登录到 OpenSSH 服务器。每个要创建密钥的用户都必须运行此流程。

要在重新安装系统后保留之前生成的密钥对,请在创建新密钥前备份 ~/.ssh/ 目录。重新安装后,将其复制到主目录中。您可以为系统中的所有用户(包括 root 用户)进行此操作。

先决条件

  • 您已经以希望使用密钥连接到 OpenSSH 服务器的用户的身份登录了。
  • OpenSSH 服务器被配置为允许基于密钥的身份验证。

流程

  1. 生成一个 ECDSA 密钥对:

    $ ssh-keygen -t ecdsa
    Generating public/private ecdsa key pair.
    Enter file in which to save the key (/home/<username>/.ssh/id_ecdsa):
    Enter passphrase (empty for no passphrase): <password>
    Enter same passphrase again: <password>
    Your identification has been saved in /home/<username>/.ssh/id_ecdsa.
    Your public key has been saved in /home/<username>/.ssh/id_ecdsa.pub.
    The key fingerprint is:
    SHA256:Q/x+qms4j7PCQ0qFd09iZEFHA+SqwBKRNaU72oZfaCI <username>@<localhost.example.com>
    The key's randomart image is:
    +---[ECDSA 256]---+
    |.oo..o=++        |
    |.. o .oo .       |
    |. .. o. o        |
    |....o.+...       |
    |o.oo.o +S .      |
    |.=.+.   .o       |
    |E.*+.  .  . .    |
    |.=..+ +..  o     |
    |  .  oo*+o.      |
    +----[SHA256]-----+
    Copy to Clipboard Toggle word wrap

    您还可以使用没有任何参数的 ssh-keygen 命令生成一个 RSA 密钥对,或通过输入 ssh-keygen -t ed25519 命令生成一个 Ed25519 密钥对。请注意,Ed25519 算法不符合 FIPS-140,OpenSSH 在 FIPS 模式下无法使用 Ed25519 密钥。

  2. 将公钥复制到远程机器上:

    $ ssh-copy-id <username>@<ssh-server-example.com>
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    <username>@<ssh-server-example.com>'s password:
    …
    Number of key(s) added: 1
    
    Now try logging into the machine, with: "ssh '<username>@<ssh-server-example.com>'" and check to make sure that only the key(s) you wanted were added.
    Copy to Clipboard Toggle word wrap

    <username>@<ssh-server-example.com> 替换为您的凭证。

    如果您没有在会话中使用 ssh-agent 程序,上一个命令会复制最新修改的 ~/.ssh/id*.pub 公钥。要指定另一个公钥文件,或在 ssh-agent 内存中缓存的密钥优先选择文件中的密钥,使用带有 -i 选项的 ssh-copy-id 命令。

验证

  • 使用密钥文件登录到 OpenSSH 服务器:

    $ ssh -o PreferredAuthentications=publickey <username>@<ssh-server-example.com>
    Copy to Clipboard Toggle word wrap

要提高系统安全性,通过在 OpenSSH 服务器上禁用密码身份验证来强制进行基于密钥的身份验证。

先决条件

  • 已安装 openssh-server 软件包。
  • sshd 守护进程正在服务器中运行。
  • 您已使用密钥连接到 OpenSSH 服务器。

    详情请参阅 生成 SSH 密钥对 部分。

流程

  1. 在文本编辑器中打开 /etc/ssh/sshd_config 配置,例如:

    # vi /etc/ssh/sshd_config
    Copy to Clipboard Toggle word wrap
  2. PasswordAuthentication 选项改为 no:

    PasswordAuthentication no
    Copy to Clipboard Toggle word wrap
  3. 在不是新默认安装的系统上,检查是否设置了 PubkeyAuthentication 参数,或是否设置为 yes
  4. ChallengeResponseAuthentication 指令设置为 no

    请注意,相应的条目在配置文件中已被注释掉,默认值为 yes

  5. 要在 NFS 挂载的主目录中使用基于密钥的验证,启用 use_nfs_home_dirs SELinux 布尔值:

    # setsebool -P use_nfs_home_dirs 1
    Copy to Clipboard Toggle word wrap
  6. 如果您要进行远程连接,而不使用控制台或带外访问,在禁用密码验证前测试基于密钥的登录过程。
  7. 重新载入 sshd 守护进程以应用更改:

    # systemctl reload sshd
    Copy to Clipboard Toggle word wrap

23.1.3. 使用 ssh-agent 缓存 SSH 凭证

为了避免在每次发起 SSH 连接时输入密码短语,您可以使用 ssh-agent 工具为登录会话缓存 SSH 私钥。如果代理正在运行,并且您的密钥已解锁,您可以使用这些密钥登录到 SSH 服务器,但不必再次输入密钥的密码。确保私钥和密语安全。

先决条件

  • 您有一个运行了 SSH 守护进程的远程主机,并可通过网络访问。
  • 您知道登录到远程主机的 IP 地址或者主机名以及凭证。
  • 您已用密码生成了 SSH 密钥对,并将公钥传送到远程机器。

    详情请参阅 生成 SSH 密钥对 部分。

流程

  1. 将在您的会话中自动启动 ssh-agent 的命令添加到 ~/.bashrc 文件中:

    1. 在您选择的文本编辑器中打开 ~/.bashrc,例如:

      $ vi ~/.bashrc
      Copy to Clipboard Toggle word wrap
    2. 在文件中添加以下行:

      eval $(ssh-agent)
      Copy to Clipboard Toggle word wrap
    3. 保存更改,退出编辑器。
  2. ~/.ssh/config 文件中添加以下行:

    AddKeysToAgent yes
    Copy to Clipboard Toggle word wrap

    使用此选项并在会话中启动了 ssh-agent ,代理仅在您第一次连接到主机时提示输入密码。

验证

  • 登录到使用代理中缓存的私钥的对应的公钥的主机,例如:

    $ ssh <example.user>@<ssh-server@example.com>
    Copy to Clipboard Toggle word wrap

    请注意您不必输入密码短语。

您可以在智能卡上创建并存储 ECDSA 和 RSA 密钥,并通过 OpenSSH 客户端上的智能卡进行身份验证。智能卡验证替换了默认密码验证。

先决条件

  • 在客户端中安装了 opensc 软件包,pcscd 服务正在运行。

流程

  1. 列出所有由 OpenSC PKCS #11 模块提供的密钥,包括其 PKCS #11 URIs,并将输出保存到 key.pub 文件:

    $ ssh-keygen -D pkcs11: > keys.pub
    Copy to Clipboard Toggle word wrap
  2. 将公钥传送到远程服务器。使用ssh-copy-id 命令和上一步中创建的 keys.pub 文件:

    $ ssh-copy-id -f -i keys.pub <username@ssh-server-example.com>
    Copy to Clipboard Toggle word wrap
  3. 使用 ECDSA 密钥连接到 <ssh-server-example.com>。您可以只使用 URI 的子集,它唯一引用您的密钥,例如:

    $ ssh -i "pkcs11:id=%01?module-path=/usr/lib64/pkcs11/opensc-pkcs11.so" <ssh-server-example.com>
    Enter PIN for 'SSH key':
    [ssh-server-example.com] $
    Copy to Clipboard Toggle word wrap

    因为 OpenSSH 使用 p11-kit-proxy 包装器,且 OpenSC PKCS #11 模块已注册到 p11-kit 工具,因此您可以简化上一个命令:

    $ ssh -i "pkcs11:id=%01" <ssh-server-example.com>
    Enter PIN for 'SSH key':
    [ssh-server-example.com] $
    Copy to Clipboard Toggle word wrap

    如果您跳过 PKCS #11 URI 的 id= 部分,则 OpenSSH 会加载代理模块中可用的所有密钥。这可减少输入所需的数量:

    $ ssh -i pkcs11: <ssh-server-example.com>
    Enter PIN for 'SSH key':
    [ssh-server-example.com] $
    Copy to Clipboard Toggle word wrap
  4. 可选:您可以使用 ~/.ssh/config 文件中的同一 URI 字符串使配置持久:

    $ cat ~/.ssh/config
    IdentityFile "pkcs11:id=%01?module-path=/usr/lib64/pkcs11/opensc-pkcs11.so"
    $ ssh <ssh-server-example.com>
    Enter PIN for 'SSH key':
    [ssh-server-example.com] $
    Copy to Clipboard Toggle word wrap

    ssh 客户端工具现在自动使用此 URI 和智能卡中的密钥。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat