zoukankan      html  css  js  c++  java
  • 使用ssh公钥实现免密码登录

    使用ssh公钥实现免密码登录

    ssh 无密码登录要使用公钥与私钥。linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例。

    有机器A(10.207.160.34),B(10.221.32.234)。现想A通过ssh免密码登录到B。
    首先以root账户登陆为例。


    1.在A机下生成公钥/私钥对。

    ssh-keygen -t rsa -P ''

    -P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。

    该命令将在~/.ssh目录下面产生一对密钥id_rsa和id_rsa.pub。

    一般采用的ssh的rsa密钥:
    id_rsa     私钥
    id_rsa.pub 公钥
    下述命令产生不同类型的密钥
    ssh-keygen -t dsa
    ssh-keygen -t rsa
    ssh-keygen -t rsa1

    执行结果:

    [hadoop@VM_32_234_centos ~]$ ssh-keygen -t rsa -P ''
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/hadoop/.ssh/id_rsa): 
    Created directory '/home/hadoop/.ssh'.
    Your identification has been saved in /home/hadoop/.ssh/id_rsa.
    Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
    The key fingerprint is:
    c6:ae:f2:7b:d1:54:eb:30:c3:ee:a1:ea:89:14:da:97 hadoop@VM_32_234_centos
    The key's randomart image is:
    +--[ RSA 2048]----+
    |                 |
    |            .    |
    |         . . .   |
    |       .  * .    |
    |    .   S+ =     |
    |   o . +. + .    |
    |  . o E .+ .     |
    |   ..o oo .      |
    |    .+B+         |
    +-----------------+

    2.把A机下的~/.ssh/id_rsa.pub 复制到B机的 ~/.ssh/authorized_keys文件里,先要在B机上创建好 ~/.ssh 这个目录,用scp复制。

    scp ~/.ssh/id_rsa.pub hadoop@Node1:~/authorized_keys
    cat ~/id_rsa.pub >> ~/.ssh/authorized_keys 

    或者

    scp ~/.ssh/id_rsa.pub hadoop@Node1:~/.ssh/authorized_keys

    执行结果:

    hadoop@VM_160_34_centos:~> scp ~/.ssh/id_rsa.pub hadoop@Node1:~/.ssh/authorized_keys
    The authenticity of host 'node1 (10.221.32.234)' can't be established.
    RSA key fingerprint is 4a:bc:1e:ca:18:87:39:af:e4:dd:c4:ce:c1:7b:c7:66.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'node1,10.221.32.234' (RSA) to the list of known hosts.
    hadoop@node1's password: 
    id_rsa.pub                                    100%  405     0.4KB/s   00:00    

    由于还没有免密码登录的,所以要输入一次B机当前用户的密码。

    hadoop@VM_160_34_centos:~> ssh Node1
    hadoop@node1's password: 
    Last login: Tue Aug 19 21:29:46 2014 from master

    3.authorized_keys的权限要是600!!!

    chmod 600 ~/.ssh/authorized_keys

    4.A机登录B机。

    [hadoop@VM_32_234_centos ~]$ ssh node1
    Last login: Tue Aug 19 22:05:43 2014 from master

    SSH-KeyGen 的用法

    假设 A 为客户机器,B为目标机;

    要达到的目的:
    A机器ssh登录B机器无需输入密码;
    加密方式选 rsa|dsa均可以,默认dsa

    做法:
    1、登录A机器
    2、ssh-keygen -t [rsa|dsa],将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub
    3、将 .pub 文件复制到B机器的 .ssh 目录, 并 cat id_dsa.pub >> ~/.ssh/authorized_keys
    4、大功告成,从A机器登录B机器的目标账户,不再需要密码了;

    ssh-keygen做密码验证可以使在向对方机器上ssh ,scp不用使用密码.
    具体方法如下:
    ssh-keygen -t rsa
    然后全部回车,采用默认值.

    这样生成了一对密钥,存放在用户目录的~/.ssh下。
    将公钥考到对方机器的用户目录下,并拷到~/.ssh/authorized_keys中。

    要保证.ssh和authorized_keys都只有用户自己有写权限。否则验证无效。(今天就是遇到这个问题,找了好久问题所在),其实仔细想想,这样做是为了不会出现系统漏洞。

  • 相关阅读:
    生成类似于MongoDB产生的ObjectId
    链式编程:遇到多个构造器参数(Constructor Parameters)时要考虑用构建器(Builder)
    mysql时间字符串按年/月/天/时分组查询 -- date_format
    根据模板导出excel
    九度 1188 约瑟夫环问题
    快速排序
    Linux 命令
    volatile小记
    线程池ThreadPoolExecutor
    CyclicBarrier、CountDownLatch与Semaphore的小记
  • 原文地址:https://www.cnblogs.com/mjorcen/p/3923209.html
Copyright © 2011-2022 走看看