Linux CentOS6 免密登录

Linux CentOS6 Hadoop集群 免密登录


一对一

  1. 安装ssh客户端(发文件和收文件的机器都得安装)

     yum install -y openssh-clients
    
  2. 生成公钥和私钥

     ssh-keygen -t rsa (一路回车)
    
  3. 为需要进行免密的机器发送公钥(不要忘了给自己发,因为自己登录自己也需要密码)

     ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.2.12
    

    按照提示输入密码以后,就可以进行免密登录或者发文件了


一对多

适用于hadoop搭建集群的时候

  1. 前提
    ① 需要配置好hosts映射文件
    vi /etc/hosts

    192.168.2.11	hadoop01
    192.168.2.12	hadoop02
    192.168.2.13	hadoop03
    

    ② hosts映射文件里这几台机器的密码必须一样

  2. 自己建一个脚本

    autossh.sh 将"PWD_1="后面的值设为/etc/hosts文件里多台机器的登录密码(本人为123456)

     #!/bin/bash
     #yum安装expect
     yum -y install expect
     #PWD_1是登陆密码,可以自己设定
     PWD_1=123456
     ips=$(cat /etc/hosts |grep -v "::" | grep -v "127.0.0.1")
     key_generate() {
     	expect -c "set timeout -1;
     		spawn ssh-keygen -t rsa;
     		expect {
     			{Enter file in which to save the key*} {send -- \r;exp_continue}
     			{Enter passphrase*} {send -- \r;exp_continue}
     			{Enter same passphrase again:} {send -- \r;exp_continue}
     			{Overwrite (y/n)*} {send -- n\r;exp_continue}
     			eof             {exit 0;}
     	};"
     }
     auto_ssh_copy_id () {
     	expect -c "set timeout -1;
     		spawn ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@$1;
     			expect {
     				{Are you sure you want to continue connecting *} {send -- yes\r;exp_continue;}
     				{*password:} {send -- $2\r;exp_continue;}
     				eof {exit 0;}
     			};"
     }
     # rm -rf ~/.ssh
    
     key_generate
    
     for ip in $ips
     do
     	auto_ssh_copy_id $ip  $PWD_1
     done
    

    给脚本添加执行权限

     chmod u+x autossh.sh
    

    然后运行脚本

     ./autossh.sh
    

免密操作

  1. 免密登录

     ssh 192.168.2.12
    
  2. 免密发送文件

     scp /etc/hosts 192.168.2.12:/etc/
    

以上就是实现免密登录的方法步骤( * ^ ▽ ^ * )

猜你喜欢

转载自blog.csdn.net/qq_38671360/article/details/83515380