hadoop 2.4.1配置ssh免密码登陆

来源:互联网 发布:辛普森悖论 知乎 编辑:程序博客网 时间:2024/05/16 08:37

</pre>问题起源:在vmware环境下,配置了两台centos6.4的虚拟机,在配置好IP地址,主机名以后,不同的虚拟机之间可以通过SSH协议进行通信,但是,每次通信都得需要输入密码,这是一件很麻烦的事情,为了减少麻烦,特写此博客,记录,如何 配置  ssh免密码登录。<p></p><p><span style="color:#ff0000">需        求</span>:在IP地址为 192.168.8.118的主机给192.168.8.119的主机发送命令的时候,不需要输入密码。</p><p><span style="color:#ff0000">步        骤</span>:在secureCRT的shell命令窗口,连接192.168.8.118,登录之后,输入 ls -la,看到如下结果</p><p>                 </p><pre name="code" class="plain">[root@itcast01 ~]# \ls -la                                                total 271352dr-xr-x---.  6 root root      4096 Jul  6 19:29 .dr-xr-xr-x. 28 root root      4096 Jul  7 09:17 ..-rw-------.  1 root root      1430 Jul  4 17:34 anaconda-ks.cfg-rw-------.  1 root root      1339 Jul  7 08:21 .bash_history-rw-r--r--.  1 root root        18 May 20  2009 .bash_logout-rw-r--r--.  1 root root       176 May 20  2009 .bash_profile-rw-r--r--.  1 root root       176 Sep 23  2004 .bashrcdrwx------.  3 root root      4096 Jul  4 18:29 .config-rw-r--r--.  1 root root       100 Sep 23  2004 .cshrcdrwx------.  2 root root      4096 Jul  4 18:29 .gconfdrwx------.  2 root root      4096 Jul  4 18:41 .gconfd-rw-r--r--.  1 root root 124191203 Jul  5 17:04 hadoop-2.4.1-x64.tar.gz-rw-r--r--.  1 root root     49448 Jul  4 17:33 install.log-rw-r--r--.  1 root root     10033 Jul  4 17:30 install.log.syslog-rw-r--r--.  1 root root 153530841 Jul  4 19:50 jdk-7u80-linux-x64.tar.gz<span style="color:#ff0000;">drwx------.  2 root root      4096 Jul  6 19:36 .ssh                          注意这里,有一个.ssh的隐藏文件夹</span>-rw-r--r--.  1 root root       129 Dec  4  2004 .tcshrc-rw-------.  1 root root      5970 Jul  6 19:29 .viminfo-rw-------.  1 root root        53 Jul  6 16:01 .xauthaaVDEJ


[root@itcast01 ~]# pwd                                                      <span style="color:#ff0000;">  pwd命令,查看当前root用户的“家”在哪里</span>/root                                                                        <span style="color:#ff0000;"> 发现root用户的家在 “/root” 文件夹下</span>


[root@itcast01 /]# cd /home                                                  <span style="color:#ff6666;"> 转到home文件夹写下</span>[root@itcast01 home]# ls                                                      <span style="color:#ff0000;">列出所有文件夹</span>lost+found  wec                                                               <span style="color:#ff0000;">发现wec用户在home文件夹下有个wec文件夹</span>
[root@itcast01 home]# cd wec[root@itcast01 wec]# lsDesktop    Downloads  Pictures  TemplatesDocuments  Music      Public    Videos[root@itcast01 wec]# pwd                                                      <span style="color:#ff0000;">查看wec用户的家,在"home/wec"下面</span>/home/wec

        超级用户住别墅,普通用户主公寓, 超级用户在 /root下面,而普通用户在/home下面

[root@itcast01 wec]# ls -la                                                   查看wec用户下的所有的文件夹total 164drwx------. 27 wec  wec  4096 Jul  7 08:34 .drwxr-xr-x.  4 root root 4096 Jul  4 18:39 ..drwxrwxr-x.  2 wec  wec  4096 Jul  4 18:43 .abrt-rw-------.  1 wec  wec   236 Jul  4 16:10 .bash_history-rw-r--r--.  1 wec  wec    18 Feb 22  2013 .bash_logout-rw-r--r--.  1 wec  wec   176 Feb 22  2013 .bash_profile-rw-r--r--.  1 wec  wec   124 Feb 22  2013 .bashrcdrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 .cachedrwxr-xr-x.  5 wec  wec  4096 Jul  4 18:43 .configdrwx------.  3 wec  wec  4096 Jul  4 18:43 .dbusdrwxr-xr-x.  2 wec  wec  4096 Jul  4 16:15 Desktop-rw-r--r--.  1 wec  wec    31 Jul  7 08:34 .dmrcdrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Documentsdrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Downloads-rw-------.  1 wec  wec    16 Jul  4 18:43 .esd_authdrwx------.  4 wec  wec  4096 Jul  7 08:34 .gconfdrwx------.  2 wec  wec  4096 Jul  7 08:55 .gconfddrwxr-xr-x.  6 wec  wec  4096 Jul  4 23:32 .gnome2drwx------.  2 wec  wec  4096 Jul  4 23:32 .gnome2_privatedrwxrwxr-x.  3 wec  wec  4096 Jul  4 18:43 .gnotedrwx------.  2 wec  wec  4096 Jul  7 08:34 .gnupgdrwxrwxr-x.  2 wec  wec  4096 Jul  4 18:43 .gstreamer-0.10-rw-rw-r--.  1 wec  wec   127 Jul  7 08:34 .gtk-bookmarksdrwx------.  2 wec  wec  4096 Jul  4 18:43 .gvfs-rw-------.  1 wec  wec  2170 Jul  7 08:34 .ICEauthority-rw-r--r--.  1 wec  wec   631 Jul  7 08:34 .imsettings.logdrwxr-xr-x.  3 wec  wec  4096 Jul  4 18:43 .localdrwxr-xr-x.  5 wec  wec  4096 Jul  4 23:32 .mozilladrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Musicdrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 .nautilusdrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Picturesdrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Publicdrwx------.  2 wec  wec  4096 Jul  4 18:43 .pulse-rw-------.  1 wec  wec   256 Jul  4 18:43 .pulse-cookie-rw-------.  1 wec  wec   218 Jul  4 18:45 .recently-used.xbel<span style="color:#ff0000;">drwx------.  2 wec  wec  4096 Jul  4 18:43 .ssh                               普通用户也有自己的公钥私钥文件</span>drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Templatesdrwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Videos-rw-------.  1 wec  wec   850 Jul  4 22:40 .viminfo-rw-------.  1 wec  wec   631 Jul  7 08:35 .xsession-errors-rw-------.  1 wec  wec  1759 Jul  7 08:21 .xsession-errors.old[root@itcast01 wec]# 


回到root用户下:


[root@itcast01 ~]# pwd/root[root@itcast01 ~]# ls -latotal 271352dr-xr-x---.  6 root root      4096 Jul  6 19:29 .dr-xr-xr-x. 28 root root      4096 Jul  7 09:17 ..-rw-------.  1 root root      1430 Jul  4 17:34 anaconda-ks.cfg-rw-------.  1 root root      1339 Jul  7 08:21 .bash_history-rw-r--r--.  1 root root        18 May 20  2009 .bash_logout-rw-r--r--.  1 root root       176 May 20  2009 .bash_profile-rw-r--r--.  1 root root       176 Sep 23  2004 .bashrcdrwx------.  3 root root      4096 Jul  4 18:29 .config-rw-r--r--.  1 root root       100 Sep 23  2004 .cshrcdrwx------.  2 root root      4096 Jul  4 18:29 .gconfdrwx------.  2 root root      4096 Jul  4 18:41 .gconfd-rw-r--r--.  1 root root 124191203 Jul  5 17:04 hadoop-2.4.1-x64.tar.gz-rw-r--r--.  1 root root     49448 Jul  4 17:33 install.log-rw-r--r--.  1 root root     10033 Jul  4 17:30 install.log.syslog-rw-r--r--.  1 root root 153530841 Jul  4 19:50 jdk-7u80-linux-x64.tar.gzdrwx------.  2 root root      4096 Jul  6 19:36 .ssh-rw-r--r--.  1 root root       129 Dec  4  2004 .tcshrc-rw-------.  1 root root      5970 Jul  6 19:29 .viminfo-rw-------.  1 root root        53 Jul  6 16:01 .xauthaaVDEJ[root@itcast01 ~]# cd .ssh/[root@itcast01 .ssh]# lsknown_hosts[root@itcast01 .ssh]# 



看下生成密钥前后文件夹的区别,以及如何生成密钥

[root@itcast01 ~]# cd .ssh/                                           转到隐藏的 .ssh文件夹下[root@itcast01 .ssh]# lsknown_hosts                                                           发现只有一个文件夹[root@itcast01 .ssh]# ssh-keygen -t rsa                               输入生成密钥命令     回车Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):              回车Enter passphrase (empty for no passphrase):                           回车Enter same passphrase again:                                          回车Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:b9:88:0d:8a:de:bd:d8:fb:1d:a0:e5:71:ee:13:92:d1 root@itcast01The key's randomart image is:+--[ RSA 2048]----+|                 ||                 ||        .        ||       . E       ||    .  +S.       || . . +++=o       ||. . ..o.oo.      ||. . +   o..      || . o =+. o.      |+-----------------+[root@itcast01 .ssh]# lsid_rsa  id_rsa.pub  known_hosts                              完成后,多了两个文件[root@itcast01 .ssh]# 




看一下公钥和私钥的内容:

[root@itcast01 .ssh]# more id_rsa.pub                                                查看公钥内容ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqDTYW9PRPjr5KtnSk35EnPDyFVydtwGCzLsp86V/qE27kp1mnF6aFicERXBu8Cdoau/f5kNVen+0U6Xl7gYTZm0Nh+S3nWqHGRnFCUJNtDeKQFTMtJz/DpZ7v25eKdwQLrzxAU3iBuYUU2lhMT2HG0McWVNesdk+EtCX+f4Z7bcP1YFAiKnlsDiMeNe6b1AGlNB1dYfuD5TLpzZs74uf1O3ZIVlv1v1RqKG+KTrj5moA8+wdWqP9keLUCW+Os1j8/2vTJRzkqvCNATUSdNOyasu9IN3DYBU/SgKDHU5f/u/7RRnhpMv27lIJGyakXDbBraI+P9SFvWuKNK8RzKzoYQ== root@itcast01[root@itcast01 .ssh]# more id_rsa                                                    查看私钥内容                               -----BEGIN RSA PRIVATE KEY----- MIIEoQIBAAKCAQEAqDTYW9PRPjr5KtnSk35EnPDyFVydtwGCzLsp86V/qE27kp1mnF6aFicERXBu8Cdoau/f5kNVen+0U6Xl7gYTZm0Nh+S3nWqHGRnFCUJNtDeKQFTMtJz/DpZ7v25eKdwQLrzxAU3iBuYUU2lhMT2HG0McWVNesdk+EtCX+f4Z7bcP1YFAiKnlsDiMeNe6b1AGlNB1dYfuD5TLpzZs74uf1O3ZIVlv1v1RqKG+KTrj5moA8+wdWqP9keLUCW+Os1j8/2vTJRzkqvCNATUSdNOyasu9IN3DYBU/SgKDHU5f/u/7RRnhpMv27lIJGyakXDbBraI+P9SFvWuKNK8RzKzoYQIBIwKCAQEAmcnqYpXGn0vcfvMJq2wheYvWBOb2mLDsnezzJ+fFLCnQEQTwGfAX2b1Fu9SCr68AYcVe/mlyuSRNGUc4nxt/c5bnzLPbE5SYxn31+dY4akFodVTYap4rBgXte9KfPDbqOVxKD9ItvSohNlG38oFllUSsNGl7JkL27KF1AdJg2VVtwVK52+jThh6L2nqf4cSZ4H+Qlhs194YbbIbhojkyMVnHLMAQFXUNFMq+31mCYrzYlVk31NL4xaFZmq7usCH3JCWAzPSpR5ZTJsGZbCajVSzj56WACYQCfZiAiFM8hSrxXbRQUKsLVeKJ4PqKKxPfhjh1W37g7cTtmYr7yGixiwKBgQDchH5W6nhgQ9EMOkDGZRJpqTsN66K0AETLnpI0jRlhULzyLJGVITTqnByBxnIPdbTAUSu6p+n5gZk/5ObtJkhKCxmusyB3gNvV0yEBLmEDf8k8othu3QrlLkm/nruyLe+/pPrbrG/L83O2iPJdHE7ZsnqVJVpC3RucRLUvyG+xFQKBgQDDRZBuVareEbYXR6DVK06UngnhZbd3CJVSCfCxmQOvnja1G9XJHlBgvcd7nn7t1Lazv2bNofND+CEaI1lMfGuMpMkfkdDUA8hMO7BReChsRdlnOKhQeKH3Yln26Xejn1lDoaHOICEWlO6LxCJAIMorgFnovmd83ECKEAneoQq1HQKBgDi0WwBoLZUYwLoAWcydpaYk--More--(67%)



注意注意注意::: 转到第二台机器192.168.8.119看看:::

[root@itcast02 bin]# cd ~/.ssh/[root@itcast02 .ssh]# lsknown_hosts                                       看到有公钥文件[root@itcast02 .ssh]#    <em>                    </em>


转到第一台机器:

[root@itcast01 .ssh]# ssh-copy-id 192.168.8.119           输入shell命令,拷贝 192.168.8.118的公钥给192.168.8.119root@192.168.8.119's password: Now try logging into the machine, with "ssh '192.168.8.119'", and check in:  .ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.[root@itcast01 .ssh]# 


再次查看第二台机器中的.ssh中的文件夹

[root@itcast02 .ssh]# lsauthorized_keys  known_hosts                        会看到多了<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">authorized_keys这个授权文件</span>


细心的你会发现,在192.168.8.119这台机器的.ssh文件中的authorized_keys的内容与192.168.8.118这台机器的.ssh文件中的id_rsa id_rsa.pub的内容是一样的,

可以通过 moreid_rsa id_rsa.pub 这个shell命令来查看。


这时候在192.168.8.118上可以尝试

[root@itcast01 .ssh]# ssh 192.168.8.119Last login: Tue Jul  7 14:49:26 2015 from 192.168.8.118             此时,不在输入密码,直接成功[root@itcast02 ~]# 
同时用户可以测试,在192.168.8.118上使用 命令在启动 192.168.8.119上的tomcat的时候,也是不再需要密码了。



但是有一个问题: 192.168.8.119对192.168.8.118进行了授权,但是,自己并没有对自己的SSH写并没有授权,因此,在本机调用通过ssh启动start-dfs.sh的时候,必须还需要输入密码。如下

[root@itcast01 .ssh]# start-dfs.sh                                  启动hadoop的命令Starting namenodes on [itcast01]root@itcast01's password:                                           输入一次密码itcast01: starting namenode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-namenode-itcast01.outroot@localhost's password:                                          输入二次密码localhost: starting datanode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-datanode-itcast01.outStarting secondary namenodes [0.0.0.0]root@0.0.0.0's password:                                            输入三次密码0.0.0.0: starting secondarynamenode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-secondarynamenode-itcast01.out[root@itcast01 .ssh]# 
[root@itcast01 .ssh]# jps                                           输入JPS发现又启动了三个进程4113 SecondaryNameNode4222 Jps3957 DataNode3837 NameNode[root@itcast01 .ssh]# 
这个验证了,本机的ssh通信的时候,还是需要输入密码的,下面,配置自己本机免密码登录,




将 公钥 拷贝给自己   命令如下   执行拷贝命令的时候,还是需要输入密码的  

[root@itcast01 .ssh]# ssh-copy-id itcast01root@itcast01's password: Now try logging into the machine, with "ssh 'itcast01'", and check in:  .ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.[root@itcast01 .ssh]# 

发现上述过程中授权文件 已经生成

[root@itcast01 .ssh]# lsauthorized_keys  id_rsa  id_rsa.pub  known_hosts        发现多了一个 <span style="font-family: Arial, Helvetica, sans-serif;">authorized_keys 文件</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>

验证本机通过ssh协议启动hadoop文件不再需要  输入密码

[root@itcast01 .ssh]# ssh localhostLast login: Tue Jul  7 11:14:31 2015 from localhost[root@itcast01 ~]# start-yarn.shstarting yarn daemonsstarting resourcemanager, logging to /itcast/hadoop-2.4.1/logs/yarn-root-resourcemanager-itcast01.outlocalhost: starting nodemanager, logging to /itcast/hadoop-2.4.1/logs/yarn-root-nodemanager-itcast01.out[root@itcast01 ~]# 



本机启动hadoop的进程检测

[root@itcast01 ~]# start-all.sh 这是一个过时的命令,可以用,但是推荐使用下两行的两个命令

This script is Deprecated. Instead use start-dfs.sh and start-yarn.shStarting namenodes on [itcast01]itcast01: starting namenode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-namenode-itcast01.outlocalhost: starting datanode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-datanode-itcast01.outStarting secondary namenodes [0.0.0.0]0.0.0.0: starting secondarynamenode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-secondarynamenode-itcast01.outstarting yarn daemonsstarting resourcemanager, logging to /itcast/hadoop-2.4.1/logs/yarn-root-resourcemanager-itcast01.outlocalhost: starting nodemanager, logging to /itcast/hadoop-2.4.1/logs/yarn-root-nodemanager-itcast01.out[root@itcast01 ~]# jps                                       发现这些个进程已经启动了5423 NameNode5832 ResourceManager5515 DataNode5927 NodeManager6232 Jps5683 SecondaryNameNode




总        结:如果需要  192.168.8.118 机器可以免登录 直接调用 192.168.8.119  需要的步骤

1)在192.168.8.118上生成两个密钥   命令   ssh-keygen -t rsa  过程中需要4个回车

2)将192.168.8.118生成的.pub的共有的密钥拷贝到192.168.8.119上去,命令  ssh-copy-id 192.168.8.119  会生成一个authorized_keys文件

3)在本机启动hadoop的时候,还是不能免密码登录的,因为没有为自己授权 需要 ssh-copy-id itcast01  将授权公钥文件分配给本机一份,

4)需要注意的是:现在192.168.8.118可以免登录192.168.8.119,但是反过来是不行的,需要192.168.8.119生成公钥授权文件,然后用命令拷给192.168.8.118才可以,这时      候就可以相互的免密码登录了。

0 0
原创粉丝点击