linux下多节点之间,免密钥访问实现

说明:

  • 目前安装了4个Redhat linux操作系统,主机名分别为hadoop01,hadoop02,hadoop03,hadoop04
  • 其中hadoop01为主节点,hadoop02,hadoop03,hadoop04为从节点
  • 四个节点ip地址为192.168.10.61~64.
  • 如果不修改hosts文件,从第二部开始可将hadoop01~04改为所对应的ip地址,如第4步中的scp ~/.ssh/id_rsa.pub [email protected]:~命令可改为scp ~/.ssh/id_rsa.pub [email protected]:~

第1步:hosts文件修改(可选)

如果将来想采用ssh 主机名的方式来访问其余节点,请确保各个节点上的hosts文件下有对各个ip以及主机名的映射,可通过如下命令进行查看

<code class="language-shell hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">cat /etc/hosts</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

如果没有将ip地址与主机名相对应,请按照如上所示,修改hosts文件

<code class="language-shell hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">vim /etc/hosts</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

第2步:生成密钥对

在每个节点上执行如下命令来生成密钥对,一直点击回车即可

<code class="language-shell hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ssh<span class="hljs-attribute" style="box-sizing: border-box;">-keygen</span> <span class="hljs-attribute" style="box-sizing: border-box;">-t</span> rsa <span class="hljs-attribute" style="box-sizing: border-box;">-P</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">''</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

第3步:本机ssh访问

在每个节点上执行如下命令将公钥文件写入授权文件中,并赋值权限

<code class="language-shell hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">cat ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ssh</span>/id_rsa<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.pub</span> >> ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ssh</span>/authorized_keys
chmod <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">600</span> ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ssh</span>/authorized_keys</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

在每个节点上可通过输入两次ssh 本节点主机名命令来验证(注意,第一次输入ssh 本节点主机名会询问,输入yes,以后就不需要输入密码了)

第4步:主节点到从节点无密码ssh访问

在主节点分别执行如下命令将主节点的公钥文件拷贝到从节点上(注意,当进行远程拷贝的时候会要求输入节点的密码)

<code class="language-shell hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">scp ~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/.ssh/id</span>_rsa.pub  root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@hadoop02</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~</span>
scp ~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/.ssh/id</span>_rsa.pub  root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@hadoop03</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~</span>
scp ~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/.ssh/id</span>_rsa.pub  root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@hadoop04</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

在从节点中执行如下命令,将主节点的公钥追加到授权文件中

<code class="language-shell hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">cat  ~/id_rsa<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.pub</span> >>  ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ssh</span>/authorized_keys
chmod <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">600</span> ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ssh</span>/authorized_keys
rm ~/id_rsa<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.pub</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

在主节点中执行如下命令,实现主节点到从节点无密码访问

<code class="language-shell hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ssh hadoop02
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span>
ssh hadoop03
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span>
ssh hadoop04
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

第5步:从节点到主节点无密码ssh访问

在每个从节点上执行如下命令,实现从节点到主节点的无密码访问

<code class="language-shell hljs dos has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">cd</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">cd</span>  .ssh
ssh-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">copy</span>-id -i id_rsa.pub [email protected]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

在每个从节点上执行如下命令,验证从节点到主节点无密码ssh访问。

<code class="language-shell hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ssh hadoop01</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

时间: 2024-10-12 20:32:00

linux下多节点之间,免密钥访问实现的相关文章

linux下多节点之间免密钥访问

说明: 目前安装了4个Redhat linux操作系统,主机名分别为hadoop01,hadoop02,hadoop03,hadoop04 其中hadoop01为主节点,hadoop02,hadoop03,hadoop04为从节点 四个节点ip地址为192.168.10.61~64. 如果不修改hosts文件,从第二步开始可将hadoop01~04改为所对应的ip地址,如第4步中的scp ~/.ssh/id_rsa.pub [email protected]:~命令可改为scp ~/.ssh/i

linux下主从节点互相免密匙登陆

安装了三个节点,master,slave1,slave2 1.主节点免密匙登陆从节点主机点窗口输入 ssh-keygen -t rsa 一路回车下去2.然后输入cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys 3.然后将authorized_keys发送到从节点slave1,slave2scp authorized_keys [email protected]:~ scp autho

linux下mysql的用户管理及访问问题

1.mysql的安装配置 linux下有一个很神奇的东西叫yum,只要有源,用yum来安装是一件非常容易的事,什么都不用管,它会为你解决好一些软件依赖的问题.一键安装mysql: [[email protected] ~]# yum install mysql-server mysql-devel 安装完成后我们就可以使用mysql了: [[email protected] ~]# /etc/init.d/mysqld start Starting mysqld: [ OK ] [[email 

Linux下多节点SSH无密码互联实现

需求:有3个主机192.168.0.191.192.168.0.192.192.168.0.193,需要实现无密码ssh互联访问 我使用的是root用户进行操作的: 1.每个节点分别检查是否安装openssh (每个节点都做):如果没有安装ssh,则需要先安装,执行命令: sudo apt-get install ssh 2.每个节点分别生成公钥和私钥: ssh-keygen -t rsa 以上命令执行后,进行3次回车就行,其中-t后面的参数是指使用的加密协议类型,可以是rsa或者dsa,生成的

Linux下不同机器之间的文件拷贝

通过 scp 命令实现不同机器之间的文件拷贝. (1)本机考到目标机器:scp 本机文件 目的地: 如:scp /home/odp-web.war   [email protected]:/usr/local/Tomcat/wepapps/ (2)其他机器考到本机:scp 其他机器上的文件 本机路径 如:scp [email protected]:/usr/local/Tomcat/wepapps/   /home/odp-web.war

最简单有效的关于linux下配置Git操作免登录ssh公钥

ssh-keygen -t rsa -b 4096 -C "[email protected]" 然后回车三连击- 进入目录(执行cd ~/.ssh),可以看到当前目录下多出两个文件 id_rsa.pub 和 id_rsa 带后缀是公钥,不带是私钥 用vi 编辑器将 id_rsa.pub 打开公钥,将全部内容复制(私钥别动) 然后进入github 登录>点头像>点settings>点SSH and GPG keys>点New SSH key,如图 Title:只

Linux下支持mysql支持远程ip访问

示例代码: 1 use mysql; 2 SELECT `Host`,`User` FROM user; 3 UPDATE user SET `Host` = '%' WHERE `User` = 'use**' LIMIT 1; 4 flush privileges; //强制刷新权限 注:%表示所有ip都将支持 实操图如下:

linux下tomcat无法访问问题(换一种说法:无法访问8080端口)

有时候linux下的tomcat其他机器无法访问,比如主机无法访问linux虚拟机的tomcat,这是因为tocat的端口,linux没有对外开放,所以只能localhost访问,但是别的机器访问不了:解决方法如下 编辑linux的iptables文件:vi /etc/sysconfig/iptables 复制一句:已经开放22端口的命令改为8080就好: -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT 允许某个端

【Hadoop系列】linux下 root用户免密码登录远程主机 ssh

操作环境: CentOS 6.5 操作对象: 用户A主机和远程主机B 正文部分:斜体加粗代表linux指令. linux下 非root用户免密码登录远程主机 ssh请转至此链接:XXXXXXXXXXXXXXXX(暂未写好) 一.远程主机B和用户A都安装openssh 首先我们检查远程主机是否安装openshh和rsync rpm -qa|grep opensshrpm -qa|grep rsync 如果没有返回相关信息代表并未安装,我们用yum指令安装. yum install openssh