zoukankan      html  css  js  c++  java
  • 使用公钥进行远程登录主机/远程执行命令

    1. 生成密钥对

    ssh-keygen -t rsa -C "test@mail.com" -b 4096 -N "" -f ./test
    # -t 密钥类型
    # -C 备注
    # -b bits 指定密钥长度。对于RSA密钥,最小要求768位,默认是2048位。DSA密钥必须恰好是1024位(FIPS 186-2 标准的要求)
    # -f 输出文件
    # -N 提供一个新的密码(如果有密码则在连接的时候需要输入密码),这里密码为空
    # 更多说明参考 https://www.cnblogs.com/yanglang/p/9563496.html
    

    运行输出如下

    Generating public/private rsa key pair.
    Your identification has been saved in ./test.
    Your public key has been saved in ./test.pub.
    The key fingerprint is:
    SHA256:S7N4ThFZs21lICbyOUV2EQEUxc61rNw6X+ig0Kzk1fM test@mail.com
    The key's randomart image is:
    +---[RSA 4096]----+
    |      . .o%=B=o  |
    |       o O *.o.  |
    |        * .ooo . |
    |         o .o o  |
    |        S  . o   |
    |       o B .o .. |
    |      . B + +.. .|
    |       * + .o= . |
    |        + .  oE  |
    +----[SHA256]-----+
    

    运行成功后会在当前目录生成test(私钥匙)和test_pub(公钥)文件

    -rw-------  1 Ryou  staff  3434  8 23 13:10 test
    -rw-r--r--  1 Ryou  staff   739  8 23 13:10 test.pub
    

    注:如果使用了-N密码参数,在连接的时候会要求输入密码

    ▶ ssh 127.0.0.1 -i ./test "ls"              
    Enter passphrase for key './test': 
    

    2. 使用密钥对

    首先将上面创建的公钥(test_pub)内容添加到目标机器~/.ssh/authorized_keys文件

    ssh-rsa AAAA*** "这里省略***字节" test@mail.com
    

    方法0 使用默认私钥

    把上面创建的test文件改名为id_rsa,移到~/.ssh/目录,即

    ▶ ls ~/.ssh/
    -rw-------  2 Ryou  staff    400 12 30  2016 id_rsa
    

    这个文件是本用户的默认私钥,在方法1 方法2中没指定私钥的情况下会使用这个作为私钥

    方法1

    ssh 127.0.0.1 -o stricthostkeychecking=no -i ./test
    

    参数说明

    # -i ./test 指定私钥
    # -o stricthostkeychecking=no 不做严格检查,省略该参数会在第一次连接的时候提示以下信息,要求输入yes   
    The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
    ECDSA key fingerprint is SHA256:tQl1ItmIvXn0cszPC8YUNcQmIkqwC1tdoV2BoOR02tI.
    Are you sure you want to continue connecting (yes/no)? 
    

    输入yes确认后,会在~/.ssh/known_hosts中添加一条记录

    # ~/.ssh/known_hosts 的文件内容
    127.0.0.1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKZfOi5mnjuEH72CYj/FQHtWYH91m32Ej9H8h6VlZIrqbg19vIffM8gDPv+EBDNaqnDXiRfMxZ/2gAlr0FqkQa8=
    

    方法2 使用别名

    修改文件~/.ssh/config

    # 分别是 别名 主机地址 用户名 私钥地址
    Host localhost
    HostName 127.0.0.1
    User root
    IdentityFile "/Users/Ryou/.ssh/test"
    

    添加完毕后,直接执行 ssh localhost 就可以连接了

  • 相关阅读:
    WPF DelegateCommand 出现Specified cast is not valid
    WPF DelegateCommand 出现Specified cast is not valid
    WPF DelegateCommand 出现Specified cast is not valid
    win10 sdk 是否向下兼容
    win10 sdk 是否向下兼容
    win10 sdk 是否向下兼容
    PHP extract() 函数
    PHP end() 函数
    PHP each() 函数
    PHP current() 函数
  • 原文地址:https://www.cnblogs.com/arliang/p/11399593.html
Copyright © 2011-2022 走看看