一、手动配置 SSH 免密登陆
1 、原理
.ssh 文件夹下的文件功能解释 1)known_hosts:记录 ssh 访问过计算机的公钥(public key) 2)id_rsa:生成的私钥 3)id_rsa.pub:生成的公钥 4)authorized_keys :存放授权过得无秘登录服务器公钥 2 、生成公钥 和私钥
[alex@hadoop102 ~]$ ssh-keygen -t rsa
[alex@hadoop103 ~]$ ssh-keygen -t rsa
[alex@hadoop104 ~]$ ssh-keygen -t rsa
注:敲三个回车,生成 id_rsa(私钥)、id_rsa.pub(公钥)两个文件 3 、进入.ssh 目录
[alex@hadoop102 ~]$ pwd
/home/alex
[alex@hadoop102 ~]$ cd ~/.ssh
4 、拷贝公钥 1 )hadoop102
[alex@hadoop102 .ssh]$ ssh-copy-id hadoop102
[alex@hadoop102 .ssh]$ ssh-copy-id hadoop103
[alex@hadoop102 .ssh]$ ssh-copy-id hadoop104
2 )hadoop103
[alex@hadoop103 .ssh]$ ssh-copy-id hadoop102
[alex@hadoop103 .ssh]$ ssh-copy-id hadoop103
[alex@hadoop103 .ssh]$ ssh-copy-id hadoop104
3 )hadoop104
[alex@hadoop104 .ssh]$ ssh-copy-id hadoop102
[alex@hadoop104 .ssh]$ ssh-copy-id hadoop103
[alex@hadoop104 .ssh]$ ssh-copy-id hadoop104
二、脚本配置 SSH 免密登陆
1 、进入 bin 目录
[alex@hadoop102 ~]$ cd /usr/local/bin/
[alex@hadoop102 bin]$
2 、创建脚本文件
[alex@hadoop102 bin]$ sudo touch auto-ssh-sshpass.sh
3 、修改用户和用户组
[alex@hadoop102 bin]$ sudo chown alex:alex auto-ssh-sshpass.sh
4 、赋予脚本执行权限
[alex@hadoop102 bin]$ chmod 755 auto-ssh-sshpass.sh
5 、编写脚本
[alex@hadoop102 bin]$ vim auto-ssh-sshpass.sh
#!/bin/bash
user=`whoami`
passwd=000000
echo "开始配置免密登录......"
for((current=102; current<=104; current++));do
for((host=102; host<=104; host++));do
sshpass -p $passwd ssh -q -o StrictHostKeyChecking=no
$user@hadoop$current "sshpass -p $passwd ssh-copy-id -o
StrictHostKeyChecking=no $user@hadoop$host"
done
done
echo "恭喜, 免密登录配置完成!"
6 、安装依赖
[alex@hadoop102 ~]$ sudo yum install -y sshpass
[alex@hadoop103 ~]$ sudo yum install -y sshpass
[alex@hadoop104 ~]$ sudo yum install -y sshpass
7 、生成公钥和私钥
[alex@hadoop102 ~]$ ssh-keygen -t rsa
[alex@hadoop103 ~]$ ssh-keygen -t rsa
[alex@hadoop104 ~]$ ssh-keygen -t rsa
注:敲三个回车,生成 id_rsa(私钥)、id_rsa.pub(公钥)两个文件 8 、执行脚本
[alex@hadoop102 ~]$ auto-ssh-sshpass.sh
|