linux下配置SSH免密码连接
1)关闭防火墙(在配置SSH无密码连接前,先关闭防火墙):
service iptables status
# 查看防火墙状态
service iptables stop
# 关闭防火墙
chkconfig --list | grep iptables
# 查看防火墙开机启动状态
chkconfig iptables off
# 关闭防火墙开机启动
2)检查SSH是否已安装
rpm -qa | grep openssh
# 如果出现:openssh-askpass-5.3pl-81.e16x86_64 ...,则说明SSH已安装。
rpm -qa | grep rsync
# 如果出现:rsync-3.0.6-9.e16.x86_64 ,则说明rsync安装。
安装SSH协议:
yum install ssh
yum install rsync
# rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。
启动SSH服务:
service sshd restart
3)生成SSH公钥(以hadoop用户执行)
1>在主节点执行:
ssh-keygen -t rsa
# 输入"ssh-keygen -t rsa"命令并回车后,还需要敲3个回车!
# -t 表示指定生成的密钥类型
结果:生成了~/.ssh文件夹、私钥文件(~/.ssh/id_rsa)、公钥文件(~/.ssh/id_rsa.pub)
eg:
[hadoop@master data]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Created directory '/home/hadoop/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
2e:3f:80:cb:7d:f4:af:e5:ee:6a:2a:d5:a9:0e:64:0c hadoop@master
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| E |
| o |
| .+ S. . |
| .o..o o |
| . oo+.o . |
| o o++ oo |
| +++o*= |
+-----------------+
[hadoop@master data]$
2>生成公钥后,将公钥拷贝到要免密登录的机器上
ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop@master
# 将公钥发至本机的authorized_keys的列表
ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop@slave1
# 将公钥发至从节点的authorized_keys的列表
补充:
# ssh-copy-id命令可以把本地主机的公钥复制到远程主机的authorized_keys文件上。
# ssh-copy-id命令也会给远程主机的用户主目录(home)、~/.ssh以及~/.ssh/authorized_keys设置合适的权限。
# -i 表示指定公钥文件
4)验证:
说明:本机通过ssh连接本机时,如果没有设置免密码连接,则也需要输入密码,故可以使用通过ssh本机连接本机来验证。
[hadoop@master ~]$ ssh hadoop@master
# 如果没有出现输入密码的提示,则配置成功,以后master就可以通过ssh免密码登录master了
# 如果找不到ssh命令,则
yum -y install openssh-clients
# 安装ssh客户端
# 备注:ssh是客户端,sshd是服务器
通过ssh免密码登录slave1来验证:
[hadoop@master ~]$ ssh hadoop@slave1
# 如果没有出现输入密码的提示,则配置成功,以后master就可以通过ssh免密码登录slave1了
如果没有成功,可能是/home/hadoop/.ssh文件夹的权限问题,以hadoop用户执行:
chmod 700 /home/hadoop/.ssh
chmod 644 /home/hadoop/.ssh/authorized_keys
linux下配置SSH免密码连接
最新推荐文章于 2025-05-20 14:46:25 发布