hadoop 2.4.1配置ssh免密码登陆

</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 271352
dr-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 .bashrc
drwx------.  3 root root      4096 Jul  4 18:29 .config
-rw-r--r--.  1 root root       100 Sep 23  2004 .cshrc
drwx------.  2 root root      4096 Jul  4 18:29 .gconf
drwx------.  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]# ls
Desktop    Downloads  Pictures  Templates
Documents  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 164
drwx------. 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 .bashrc
drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 .cache
drwxr-xr-x.  5 wec  wec  4096 Jul  4 18:43 .config
drwx------.  3 wec  wec  4096 Jul  4 18:43 .dbus
drwxr-xr-x.  2 wec  wec  4096 Jul  4 16:15 Desktop
-rw-r--r--.  1 wec  wec    31 Jul  7 08:34 .dmrc
drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Documents
drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Downloads
-rw-------.  1 wec  wec    16 Jul  4 18:43 .esd_auth
drwx------.  4 wec  wec  4096 Jul  7 08:34 .gconf
drwx------.  2 wec  wec  4096 Jul  7 08:55 .gconfd
drwxr-xr-x.  6 wec  wec  4096 Jul  4 23:32 .gnome2
drwx------.  2 wec  wec  4096 Jul  4 23:32 .gnome2_private
drwxrwxr-x.  3 wec  wec  4096 Jul  4 18:43 .gnote
drwx------.  2 wec  wec  4096 Jul  7 08:34 .gnupg
drwxrwxr-x.  2 wec  wec  4096 Jul  4 18:43 .gstreamer-0.10
-rw-rw-r--.  1 wec  wec   127 Jul  7 08:34 .gtk-bookmarks
drwx------.  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.log
drwxr-xr-x.  3 wec  wec  4096 Jul  4 18:43 .local
drwxr-xr-x.  5 wec  wec  4096 Jul  4 23:32 .mozilla
drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Music
drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 .nautilus
drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Pictures
drwxr-xr-x.  2 wec  wec  4096 Jul  4 18:43 Public
drwx------.  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 Templates
drwxr-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 -la
total 271352
dr-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 .bashrc
drwx------.  3 root root      4096 Jul  4 18:29 .config
-rw-r--r--.  1 root root       100 Sep 23  2004 .cshrc
drwx------.  2 root root      4096 Jul  4 18:29 .gconf
drwx------.  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
drwx------.  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]# ls
known_hosts
[root@itcast01 .ssh]# 



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

[root@itcast01 ~]# cd .ssh/                                           转到隐藏的 .ssh文件夹下
[root@itcast01 .ssh]# ls
known_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@itcast01
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|        .        |
|       . E       |
|    .  +S.       |
| . . +++=o       |
|. . ..o.oo.      |
|. . +   o..      |
| . o =+. o.      |
+-----------------+
[root@itcast01 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts                              完成后,多了两个文件
[root@itcast01 .ssh]# 




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

[root@itcast01 .ssh]# more id_rsa.pub                                                查看公钥内容
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqDTYW9PRPjr5KtnSk35EnPDyFVydtwGCzLsp86V
/qE27kp1mnF6aFicERXBu8Cdoau/f5kNVen+0U6Xl7gYTZm0Nh+S3nWqHGRnFCUJNtDeKQFTMtJ
z/DpZ7v25eKdwQLrzxAU3iBuYUU2lhMT2HG0McWVNesdk+EtCX+f4Z7bcP1YFAiKnlsDiMeNe6b
1AGlNB1dYfuD5TLpzZs74uf1O3ZIVlv1v1RqKG+KTrj5moA8+wdWqP9keLUCW+Os1j8/2vTJRzk
qvCNATUSdNOyasu9IN3DYBU/SgKDHU5f/u/7RRnhpMv27lIJGyakXDbBraI+P9SFvWuKNK8RzKz
oYQ== root@itcast01
[root@itcast01 .ssh]# more id_rsa                                                    查看私钥内容                               
-----BEGIN RSA PRIVATE KEY----- 
MIIEoQIBAAKCAQEAqDTYW9PRPjr5KtnSk35EnPDyFVydtwGCzLsp86V/qE27kp1m
nF6aFicERXBu8Cdoau/f5kNVen+0U6Xl7gYTZm0Nh+S3nWqHGRnFCUJNtDeKQFTM
tJz/DpZ7v25eKdwQLrzxAU3iBuYUU2lhMT2HG0McWVNesdk+EtCX+f4Z7bcP1YFA
iKnlsDiMeNe6b1AGlNB1dYfuD5TLpzZs74uf1O3ZIVlv1v1RqKG+KTrj5moA8+wd
WqP9keLUCW+Os1j8/2vTJRzkqvCNATUSdNOyasu9IN3DYBU/SgKDHU5f/u/7RRnh
pMv27lIJGyakXDbBraI+P9SFvWuKNK8RzKzoYQIBIwKCAQEAmcnqYpXGn0vcfvMJ
q2wheYvWBOb2mLDsnezzJ+fFLCnQEQTwGfAX2b1Fu9SCr68AYcVe/mlyuSRNGUc4
nxt/c5bnzLPbE5SYxn31+dY4akFodVTYap4rBgXte9KfPDbqOVxKD9ItvSohNlG3
8oFllUSsNGl7JkL27KF1AdJg2VVtwVK52+jThh6L2nqf4cSZ4H+Qlhs194YbbIbh
ojkyMVnHLMAQFXUNFMq+31mCYrzYlVk31NL4xaFZmq7usCH3JCWAzPSpR5ZTJsGZ
bCajVSzj56WACYQCfZiAiFM8hSrxXbRQUKsLVeKJ4PqKKxPfhjh1W37g7cTtmYr7
yGixiwKBgQDchH5W6nhgQ9EMOkDGZRJpqTsN66K0AETLnpI0jRlhULzyLJGVITTq
nByBxnIPdbTAUSu6p+n5gZk/5ObtJkhKCxmusyB3gNvV0yEBLmEDf8k8othu3Qrl
Lkm/nruyLe+/pPrbrG/L83O2iPJdHE7ZsnqVJVpC3RucRLUvyG+xFQKBgQDDRZBu
VareEbYXR6DVK06UngnhZbd3CJVSCfCxmQOvnja1G9XJHlBgvcd7nn7t1Lazv2bN
ofND+CEaI1lMfGuMpMkfkdDUA8hMO7BReChsRdlnOKhQeKH3Yln26Xejn1lDoaHO
ICEWlO6LxCJAIMorgFnovmd83ECKEAneoQq1HQKBgDi0WwBoLZUYwLoAWcydpaYk
--More--(67%)



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

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


转到第一台机器:

[root@itcast01 .ssh]# ssh-copy-id 192.168.8.119           输入shell命令,拷贝 192.168.8.118的公钥给192.168.8.119
root@192.168.8.119's password: 
Now try logging into the machine, with "ssh '192.168.8.119'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@itcast01 .ssh]# 


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

[root@itcast02 .ssh]# ls
authorized_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.119
Last 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.out
root@localhost's password:                                          输入二次密码
localhost: starting datanode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-datanode-itcast01.out
Starting 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 SecondaryNameNode
4222 Jps
3957 DataNode
3837 NameNode
[root@itcast01 .ssh]# 
这个验证了,本机的ssh通信的时候,还是需要输入密码的,下面,配置自己本机免密码登录,




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

[root@itcast01 .ssh]# ssh-copy-id itcast01
root@itcast01's password: 
Now try logging into the machine, with "ssh 'itcast01'", and check in:
  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@itcast01 .ssh]# 

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

[root@itcast01 .ssh]# ls
authorized_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 localhost
Last login: Tue Jul  7 11:14:31 2015 from localhost
[root@itcast01 ~]# start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /itcast/hadoop-2.4.1/logs/yarn-root-resourcemanager-itcast01.out
localhost: 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.sh
Starting namenodes on [itcast01]
itcast01: starting namenode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-namenode-itcast01.out
localhost: starting datanode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-datanode-itcast01.out
Starting secondary namenodes [0.0.0.0]
0.0.0.0: starting secondarynamenode, logging to /itcast/hadoop-2.4.1/logs/hadoop-root-secondarynamenode-itcast01.out
starting yarn daemons
starting resourcemanager, logging to /itcast/hadoop-2.4.1/logs/yarn-root-resourcemanager-itcast01.out
localhost: starting nodemanager, logging to /itcast/hadoop-2.4.1/logs/yarn-root-nodemanager-itcast01.out
[root@itcast01 ~]# jps                                       发现这些个进程已经启动了
5423 NameNode
5832 ResourceManager
5515 DataNode
5927 NodeManager
6232 Jps
5683 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值