zoukankan      html  css  js  c++  java
  • 【熟能生巧】配置多个SSH免密码登录实战

    需求

    本地有一台机器使用Linux系统,需要SSH(免密码登录)到云端的两台Server上。

    按照网上的教程试了一下,发现配置第一台可以,配置第二台的时候就有些问题了。于是花了些时间研究了下,下面是详细的过程。

    至于SSH的原理,可以参考搜索这里:[Public-key cryptography] https://en.wikipedia.org/wiki/Public-key_cryptography

    流程

    大致流程如下,特别要注意括号里面的内容,是关键!

    • 首先,生成一对密钥 Public Key - Private Key
    • 然后,把Private Key在本地自己藏好。(且告诉系统去哪找这个Key)
    • 然后,把Public Key上传到目标服务器上。(且告诉系统这个Key是可信的,可以用来登录)

    前置条件

    生成的key都是放在.ssh下面的,.ssh的路径一般是/root/.ssh或者~/.ssh

    生成

    运行命令ssh-keygen,下面是出来的结果:

    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_keya
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa_keya.
    Your public key has been saved in /root/.ssh/id_rsa_keya.pub.
    

    特别要注意的是,有一步,让你选择文件的名称和位置,这里最好自己输入一个文件名,从而如果有多个key的时候可以区分开来。

    Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_keya -> 比如这里我们就命名为id_rsa_keya

    总结,这一步生成了两个key,且放在了本地的/root/.ssh目录下

    • id_rsa_keya - Private Key
    • id_rsa_keya.pub - Public Key

    配置 Public Key

    将Public Key上传并添加到目标服务器上

    运行命令ssh-copy-id -i ~/.ssh/id_rsa_keya.pub user@host,下面是出来的结果:

    (user和host自己根据情况改掉)

    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa_keya.pub"
    The authenticity of host '139.999.999.999 (139.999.999.999)' can't be established.
    ECDSA key fingerprint is SHA256:G6ejPxxxxxxxxxxxRKexxxxxxxxxJcVaFPKx0.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    

    这里它会问是不是要把这个host加入到信任列表中,点yes。这一步操作,会更改~/.ssh/known_hosts文件的内容。下一次,如果从同一个IP登录,就不会再询问了。

    这次上传要输入服务器的密码,完成了之后,会显示如下:

    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    user@139.999.999.999's password: 
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh 'user@139.999.999.999'"
    and check to make sure that only the key(s) you wanted were added.
    

    总结,这一步做了两个操作:

    • id_rsa_keya.pub放到了server上的~/.ssh/目录下。
    • id_rsa_keya.pub的值append到了~/.ssh/authorized_keys这个文件里。这一步授权很关键,否则你用自己的私钥连是没用的。

    配置 Private Key

    这里,我们需要告诉系统,当我登录服务器A时,你去拿id_rsa_keya这个key做校验。(不设置的话,默认会拿id_rsa

    vim ~/.ssh/config

    加入如下内容:

    Host aliyun
        HostName aliyun
        User user
        Port 22
        IdentityFile ~/.ssh/id_rsa_keya
    

    vim /etc/hosts

    加入如下内容:

    139.999.999.999 aliyun aliyun
    

    当然,这两步也可以合为一步。这里卖个关子,各位同学可以自行研究下。

    连接

    最后,我们可以直接使用如下命令进行连接

    ssh aliyun

    ->

    Welcome to Alibaba Cloud Elastic Compute Service !
    

    完成!(第二,第三台,甚至更多server的配置也是一样的,这里就不重复了)

  • 相关阅读:
    写程序一定要养成良好习惯程序编码规范
    今天用GRID感觉它严重缺少灵活性
    REPEATER 嵌套
    DATAGRID的困惑。。。
    VB常用函数。。。。
    子父表,就是这么简单。。。。。
    今天解决了DataGrid无刷新全选删除问题。
    看来我还没完全懂DATAGRID。。。
    indexOf 和 lastIndexOf 使用
    javascript 要注意的事项
  • 原文地址:https://www.cnblogs.com/maxstack/p/13609285.html
Copyright © 2011-2022 走看看