Linux-SSH无密钥登陆

               这里所用虚拟机是Centos6.5

安装三个机器,    机器名(主机)            IP地址
                            master     192.168.29.130
                            slave1      192.168.29.133
                            slave2      192.168.29.134
检查主机名称:hostname     运行结果:admin
       命令:      hostname  master:可以将主机名admin改成slave1,只能是暂时的,重启了之后失效。

1.修改/etc/hosts文件

Linux-SSH无密钥登陆_第1张图片

2.给三个主机生成私钥(id_rsa),公钥(id_rsa.pub)文件,(我是在当前root下执行命令)

  1) ssh-keygen -t rsa -P   ''
    所以密钥保存到了/root/.ssh/目录下,可以使用命令查看:
         ls /root/.ssh/
    
      就会出现这两个文件:id_rsa id_rsa.pub
        每个主机上执行上述命令,就会出现这两个密钥文件。

 2)或者通过下面这个方法:也可以达到目的

         ①在主机master下,用户名统一是:Mcwang
            ssh-keygens -t rsa  :自动在Mcwang目录下,即Mcwang/.ssh/创建两个
                                                文件(id_rsa 、id_rsa.pub),
            ls -la  :查询是否有.ssh(隐藏文件)
            chmod 700  .ssh   :将权限改一改
            cd   .ssh     :
            ll   :查询在.ssh文件下,各个文件的详情。
            ssh localhost :登陆自己的主机,需要输入密码
            cp -id_rsa.pub authorized_keys :将公钥id_rsa.pub复制到authorized_keys
            ssh localhost  :登陆自己的主机,不需要使用密码了,它已经默认使用authorized_keys,
                    读取里面的公钥。
             cat kown_hosts :查看这个文件,里面是登陆的记录,没有什么用,删除也没啥

           想从主机master登陆主机slave1,我们要不输入密码就能登陆
            方法:slave1
                        刚刚master已经有authorized_keys文件了,我们需要将authorized_keys复制
                到slave1,slave1是还没有密钥的,我们需要创建一个.ssh目录:
                                su Mcwang
                                mkdir .ssh  :/home/Mcwang/.ssh创建文件夹.ssh
                                ls -la .ssh:查看权限
                                chmod 700 .ssh:修改权限
                   回到master:   
                     Mcwang @master .ssh:    scp authorized_keys [email protected]:
                                                                /home/Mcwang/.ssh/authorized_keys
                    注解:将Mcwang下authorized_keys复制到slave1主机的.ssh/authorized_keys文件夹,
                    自动创建authorized_keys(slave1);
                   回到slave1:
                    Mcwang @slave1 .ssh:  ls
                                            有刚刚复制的文件
                    从master登陆slave1: ssh  [email protected]
                            此时已不需要密码就可以登陆slave1了,

                    master登陆slave2:

                    方法:
                                同上:
                                              mkdir .ssh
                                               chmod 700 .ssh
                                               ls -la
                             回到master:cd .ssh
                                                    scp authorized_keys [email protected]:/home/
                                                    Mcwang/.ssh/authorized_keys  :将文件复制到slave2
                                                    ssh [email protected] : 连接slave2
                                                    (只有连接之后,会提示yes/no,一律yes,可能要输入密码,只需要输入一次
                                                        下次就不用输了,密钥文件的创建,一路回车就好)

                        slave1登陆master:

                                              cd .ssh
                                               ll  :查看详情
                                               ssh-keygens -t rsa :slave2生成自己的公钥和私钥
                                               cat id_rsa.pub >>authorized_keys :将公钥追加到autho..文件(这样就可以自                                                      己连接自己了 ,看下一个命令)
                                                ssh localhost :自己连接自己,第一次需要输入密码,第二次直接登陆;
                                                scp authorize_keys [email protected]:/home/Mcwang/.ssh
                                                        /authorized_keys     //slave1登陆master,将文件覆盖,前者有                                                            slave1和 master的公钥,后者只有自己master的公钥,所以为了方便就复制。
                                            ssh [email protected]

                   slave2登陆master:

                                          cd .ssh
                                          ll  :查看详情
                                          ssh-keygens -t rsa   :slave2生成自己的公钥和私钥
                                          scp authorize_keys [email protected]:/home/Mcwang/.ssh
                                                        /authorized_keys 
                                        也可以将slave2的id_rsa.pub 公钥 追加到master的.ssh/authorized_keys
                                总结:A想要登陆B,就要将自己A的公钥复制到对方B的authorized_keys.                      





你可能感兴趣的:(Hadoop,linux系统)