• ssh秘钥登录


    1.设置 SSH 通过密钥登录

    密钥形式登录的原理是:利用密钥生成器制作一对密钥——一只公钥和一只私钥。

    将公钥添加到服务器的某个账户上,然后在客户端利用私钥即可完成认证并登录。这样一来,没有私钥,任何人都无法通过 SSH 暴力破解你的密码来远程登录到系统。

    此外,如果将公钥复制到其他账户甚至主机,利用私钥也可以登录。

    2.制作密钥对

    通过服务器来制作密钥对,一般这个服务器账户是你打算通过密钥登录的账号,执行

    [~]# ssh-keygen     #创建密钥对
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/xx/.ssh/id_rsa):      #默认就行,按enter 
    Created directory 'home/xx/.ssh'.
    Enter passphrase (empty for no passphrase):     #输入密钥锁码,或留空按enter
    Enter same passphrase again:   #再输一遍密钥锁码
    
    Your identification has been saved in home/xx/.ssh/id_rsa.    #私钥
    Your public key has been saved in home/xx/.ssh/id_rsa.pub.    #公钥
    
    The key fingerprint is:
    xxx
    The key's randomart image is:
    +--[ RSA 2048]----+
    |  o*.            |
    | o..+            |
    |o  .             |
    | +  .            |
    |   S             |
    |                 |
    |                 |
    |                 |
    |                 |
    +-----------------+
    [~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。当然,也可以留空,实现无密码登录。

    现在,在 家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥。

    通常使用root用户登录, 就是在**/root**目录下

    3.服务器配置
    3.1在服务器上安装公钥

    键入以下命令,在服务器上安装公钥:

    cd $HOME/.ssh
    cat id_rsa.pub >> authorized_keys
    
    • 1
    • 2

    如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:

    chmod 600 authorized_keys ; chmod 700 ~/.ssh
    
    • 1
    3.2配置SSH密钥登录功能

    编辑 /etc/ssh/sshd_config 文件,进行如下设置:

    vim /etc/ssh/sshd_config 
    # 启动ssh密钥登录
    RSAAuthentication yes
    PubkeyAuthentication yes
    
    • 1
    • 2
    • 3
    • 4
    # 启动ssh密钥登录的快速脚本:
    sudo sed -r -i '/RSAAuthentication/d' /etc/ssh/sshd_config
    sudo sed -r -i '/PubkeyAuthentication/d' /etc/ssh/sshd_config
    sudo sh -c "echo 'RSAAuthentication yes' >> /etc/ssh/sshd_config"
    sudo sh -c "echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_config"
    
    • 1
    • 2
    • 3
    • 4
    • 5

    另外,请留意 root 用户能否通过 SSH 登录:

    # root用户可以通过ssh登录(禁用改为no)
    PermitRootLogin yes
    
    • 1
    • 2
    # 阻止 root 用户通过 SSH 登录:
    sudo sed -r -i '/PermitRootLogin/d' /etc/ssh/sshd_config
    sudo sh -c "echo 'PermitRootLogin no' >> /etc/ssh/sshd_config"
    
    • 1
    • 2
    • 3

    当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:

    # 允许密码登录(禁用改为no)
    PasswordAuthentication yes
    
    • 1
    • 2
    # 禁用密码登录的快速脚本:
    sudo sed -r -i '/PasswordAuthentication/d' /etc/ssh/sshd_config
    sudo sh -c "echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config"
    
    • 1
    • 2
    • 3

    最后,重启 SSH 服务:

    sudo service sshd restart
    
    • 1
    4. ssh客户端配置私钥
    4.1 xshell

    在新建/现有的会话中,依次点击:

    1、连接-用户身份验证

    2、方法(M)选为Pubilc Key

    3、输入用户名。

    4、在用户密钥栏,点击浏览(B)选择你下载下来的密钥(id_rsa)。

    确定连接即可。

  • 相关阅读:
    通过源码来理解Cglib与JDK动态代理
    CentOS7安装mysql8.0.12
    一种优雅的Git分支实践
    Mybatis-Plus常用注解
    医院管理系统(Java+SSM+MySQL开发的医院科室管理系统)
    C# CAD交互界面-自定义面板集-查找定位(六)
    助力汽修企业突破转型瓶颈,S2B2B商城价格管理模块实现采购交易数字化升级
    什么是容器
    文本截取内容易语言代码
    Linux程序地址空间
  • 原文地址:https://blog.csdn.net/weimeibuqieryu/article/details/134306487