启用 OpenSSH 服务(用于远程连接到本机)


  1. 安装 openssh

    sudo apt update
    sudo apt install openssh-server
    
  2. 查看运行状态

    安装后 openssh 会自动启动并开机自启,你可以输入以下命令查看状态:

    systemctl status ssh
       
    # 或者
    systemctl status sshd
    
  3. 设置防火墙以允许 SSH 连接

    sudo ufw allow ssh
    


远程连接到本机(没有公网地址的话需要在同一个局域网内)


  1. 通过以下命令查看本机 IP 地址:

    ip a
    
  2. 连接 SSH 服务器:

    ssh <username>@<ip_address>
    
  3. 根据提示输入密码


禁用 or 启用 OpenSSH 服务


  1. 禁用:

    sudo systemctl disable --now ssh
    
  2. 启用:

    sudo systemctl enable --now ssh
    


配置 SSH 的密钥

以下内容大部分翻译自 How to Setup Passwordless SSH Login

Secure Shell(SSH,安全外壳) 是一种用于连接服务器和客户端的加密网络协议,并且支持好几种验证机制。最常用的两种验证方式分别是基于密码和基于密钥的验证机制。

使用密钥验证不需要每次连接都输入密码,而且安全性更高(只要确保没人可以查看你的密钥),所以推荐开启 SSH 服务后就关闭密码验证,只使用密钥验证。

具体步骤:

  1. 检查已有的 SSH 密钥对,如果有,则可以使用这些密钥,或者备份并删除它们。

    ls -al ~/.ssh/id_*.pub
    
  2. 生成新的 SSH 密钥对:

    # 推荐
    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    或者:

    # 如果 OpenSSH 版本较老不支持上面那条就用这一条
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  3. 确保真的生成了新的密钥对:

    ls ~/.ssh/id_*
    
  4. 复制公钥到服务器上:

    ssh-copy-id remote_username@server_ip_address
    

    如果上述命令用不了,可以使用以下命令:

    cat ~/.ssh/id_rsa.pub | ssh remote_username@server_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
    
  5. 使用 SSH 密钥登录服务器:

    ssh remote_username@server_ip_address
    

    如果配置正确,你现在不用输密码就可以登录到服务器上了。

关闭 SSH 的密码验证

  1. 使用超级用户(sudo)或 root 登录到服务器:

    ssh sudo_user@server_ip_address
    

或者先用普通用户登录到服务器上再切换到超级用户。

  1. 打开 SSH 的配置文件 /etc/ssh/sshd_config,找到下面的配置项并修改得和下面一样:

    /etc/ssh/sshd_config
    PasswordAuthentication no
    ChallengeResponseAuthentication no
    UsePAM no
    
  2. 保存文件后重启 SSH 服务

    • Ubuntu 或 Debian 服务器:

      sudo systemctl restart ssh
      
    • CentOS、Fedora 或 Arch Linux 服务器:

      sudo systemctl restart sshd
      

如果你想要了解密钥对算法和它的安全性


这里有几个不错的科普:

参考