云服务器免密码登陆

  1. 创建密钥对
    1.1 命令用法
    ssh-keygen [-t 密钥种类] [-b 加密位数]
    1.2 举例说明
    ssh-keygen -t rsa -b 4096    #生成一个4096位的RSA密钥对
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):  #保存密钥对的位置,直接回车即可
    Enter passphrase (empty for no passphrase):    #设置密钥对的密码,这里不设置,直接敲两下回车即可
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx root@CentOS
    The key's randomart image is:
    +--[ RSA 4096]----+
    |    Eo+.         |
    |     *.o         |
    |  . o O +        |
    | . o B + o       |
    |  . * o S        |
    | . o o           |
    |  . o            |
    |   . .           |
    |  ...            |
    +-----------------+
    #创建密钥对完成
    
  2. 使用 ssh-copy-id 把公钥上传到远程主机上
    2.1 命令用法
    ssh-copy-id <用户名@主机>
    2.2 举例说明
    ssh-copy-id root@10.16.1.2
    注意:默认服务器上的 .ssh/authorized_keys 文件存储着合法客户机的公钥,如果客户机的私钥泄漏,应该在该文件里删除私钥泄漏的客户机公钥。
  3. 设置服务器
    3.1 确保 ~/.ssh 目录中的所有内容没有对其他用户赋予写、执行权限
    如果别的用户有了对这个目录的写权限,那么他们就可以自由地往里面添加自己的公钥,然后自由地登录您的账户了!显然不能这样。 sshd 有个自我保护的功能,当这个目录对其他用户赋予写权限时,公钥认证将不能进行。
    chmod -R 600 ~/.ssh
    3.2 允许公钥登录,禁止密码登录
    编辑 /etc/ssh/sshd_config,检查并设置以下选项
      PubkeyAuthentication yes
      AuthorizedKeysFile .ssh/authorized_keys
      PasswordAuthentication no
    
  4. 配置成功

ssh配置杂集

解决 ssh 登录到ubuntu server 慢的问题

  • 修改服务器的 /etc/ssh/sshd_config配置文件
  1. 添加如下配置
UseDNS no
GSSAPIAuthentication no
  1. 重启服务
sudo service ssh restart
posted @ 2022-02-13 22:13  迦洛德影歌  阅读(76)  评论(0)    收藏  举报