SSH 免密登录 Windows 与 Ubuntu 服务器

本文主要记录了客户端与 Windows 服务器和 Ubuntu 服务器的 SSH 免密登录配置方法。

SSH 免密登录 Windows 服务器

  1. 在客户端和 Windows 服务器上安装 OpenSSH 服务 Ref

  2. 在客户端生成 SSH 密钥对 Ref

  3. 在 Windows 服务器上配置 OpenSSH 服务

  • 开启 OpenSSH 服务

    1
    Start-Service sshd # PowerShell
  • 添加客户端的公钥到 Windows 服务器的 ~\.ssh\authorized_keys 文件中

  • 修改 OpenSSH 服务的配置文件 C:\ProgramData\ssh\sshd_config,需要使用管理员权限打开

    1
    2
    3
    4
    5
    6
    7
    8
    # 确保以下3条没有被注释
    PubkeyAuthentication yes # enable public key authentication
    AuthorizedKeysFile .ssh/authorized_keys # path of authorized_keys
    PasswordAuthentication no # disable password login

    # 确保以下2条有注释掉
    #Match Group administrators
    # AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
  1. 重启 OpenSSH 服务
    1
    Restart-Service sshd # PowerShell
  2. 在客户端测试免密登录
    1
    ssh username@hostname

SSH 免密登录 Ubuntu 服务器

  1. 在 Ubuntu 服务器上安装 OpenSSH 服务

    1
    2
    3
    4
    5
    ssh # check if OpenSSH is installed
    sudo apt-get install openssh-server # install OpenSSH
    sudo ufw allow ssh # allow OpenSSH through the firewall
    sudo systemctl start ssh # start OpenSSH
    sudo systemctl status ssh # check the status of OpenSSH
  2. 在客户端生成 SSH 密钥对 Ref

  3. 将客户端的公钥添加到 Ubuntu 服务器的 ~/.ssh/authorized_keys 文件中

  4. 修改 Ubuntu 服务器的 SSH 配置文件 sudo nvim /etc/ssh/sshd_config

    1
    2
    PubkeyAuthentication yes # enable public key authentication
    PasswordAuthentication no # disable password login
  5. 重启 OpenSSH 服务

    1
    sudo systemctl restart ssh
  6. 在客户端测试免密登录

    1
    ssh username@hostname

SSH 免密登录 Windows 与 Ubuntu 服务器

https://latexalpha.github.io/8bc428db9e24/

作者

Shangyu ZHAO

发布于

2024-10-13

更新于

2024-10-13

许可协议