大数据入门之 ssh 免密码登录

目录

1、ssh-keygen生成公钥和私钥

2、查看生成的公钥和私钥

3、将公钥发到其他主机上

4、给~/.ssh和authorized_keys赋予权限

5、ssh 远程连接到各个节点

6、配置错误如何重新配置


1、ssh-keygen生成公钥和私钥

cd /root/.ssh
ssh-keygen

直接三次回车,Linux会生成两个密钥:公钥和私钥

公钥文件:/root/.ssh/id_rsa.pub

私钥文件:/root/.ssh/id_rsa

大数据入门之 ssh 免密码登录_第1张图片

2、查看生成的公钥和私钥

cd ~/.ssh/
ll
pwd
cat known_hosts

大数据入门之 ssh 免密码登录_第2张图片

# 查看公钥
cat ~/.ssh/id_rsa.pub
# 查看私钥
cat ~/.ssh/id_rsa

大数据入门之 ssh 免密码登录_第3张图片

3、将公钥发到其他主机上

语法:ssh-copy-id -i ~/.ssh/id_rsa.pub root@remote(远程ip)

注意:建议逐个主机执行,多台机器同时执行容易出错

ssh-copy-id -i ~/.ssh/id_rsa.pub root@cdh01
ssh-copy-id -i ~/.ssh/id_rsa.pub root@cdh02
ssh-copy-id -i ~/.ssh/id_rsa.pub root@cdh03
ssh-copy-id -i ~/.ssh/id_rsa.pub root@cdh04
ssh-copy-id -i ~/.ssh/id_rsa.pub root@cdh05
ssh-copy-id -i ~/.ssh/id_rsa.pub root@cdh06

每台机器发送自己的公钥给其他机器,按回车键输入root用户密码即可 

大数据入门之 ssh 免密码登录_第4张图片

4、给~/.ssh和authorized_keys赋予权限

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
cat authorized_keys

 

5、ssh 远程连接到各个节点

ssh cdh01
ssh cdh02
ssh cdh03
ssh cdh04
ssh cdh05
ssh cdh06

6、配置错误如何重新配置

(1)进入 ~/.ssh 目录,查看公钥配置

cd ~/.ssh/
ll
cat authorized_keys

 (2)直接删除~/.ssh目录下的所有文件,重新生成authorized_keys

rm -rf ~/.ssh/*
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
cd ~/.ssh/

(3)如果还不行就直接把公钥文件的内容追加到authorized_keys中吧

# 本机执行
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
# 其他机器执行
cat ~/.ssh/id_rsa.pub
# 本机再拷贝上一条命令的公钥填到echo "" 
echo "" >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@cdh02:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@cdh03:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@cdh04:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@cdh05:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@cdh06:~/.ssh/authorized_keys

你可能感兴趣的:(Shell,CentOS)