阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

SSH免密码登陆

131次阅读
没有评论

共计 3417 个字符,预计需要花费 9 分钟才能阅读完成。

SSH 免密码登录,折腾了我一天,如果用 root 权限的话挺简单的,但是这样着实有些不正规,毕竟我是专业的好伐~~

所以带来了很多奇怪的问题,当解决了的时候发现,其实也没那么麻烦。

以下是我最开始用 root 用户做的 117 单向无密码连接到 118,119,120。这个是构建 hdfs-HA-Federation+yarn 之前必须要做的。

在设置免密码登录之前,要把自己的 hosts 中配置好每一个 ip 对应的名字,方便将来切换和使用,将自己的 hostname 顺便也改了吧,不改也是可以的。

1. 首先要使用下面的命令来生成 ssh 免密码的密钥:

[root@localhost ~]# ssh-keygen -t rsa -P ''

一路回车,如果之前有过,可以选择 yes 覆盖之前的密钥~~

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ca:14:3d:fa:96:ba:41:17:c6:60:7d:dc:31:67:9e:b8 root@localhost.localdomain
The key's randomart image is:
+--[RSA 2048]----+
|      o. . .o.o  |
|     . +. o .* . |
|      . *.  . o  |
|       + o   .   |
|      + S   E    |
|     + + .       |
|      + +        |
|       +         |
|      o.         |
+-----------------+

 2. 进入该用户的根目录,会有隐藏的.ssh 文件夹,进入后会看到有个 id_rsa.pub,这个就是免密码的密钥。将这个密钥追加到 authorized_keys 中。

[root@localhost .ssh]# cat id_rsa.pub >> authorized_keys

必须将 authorized_keys 修改权限为 600,说实话我也不知道为啥。

[root@localhost .ssh]# chmod 600 authorized_keys

 

3. 不管用什么方式,将生成的密钥传给要免密码的服务器中(将密钥分别给到 118,119,120)。 把自己密钥给了哪台机器,自己就可以登录哪台机器

[root@localhost .ssh]# scp id_rsa.pub root@192.168.75.118:/home/id_rsa.pub

 复制过去之后重复上面的操作,将传递过来的 id_rsa.pub 中的值追加到生成的 authorized_keys(其实直接复制到 authorized_keys 中就可以, 同时也要将权限设置为 600)

4. 在 root 权限下,编辑 sshd_config 文件:

[root@localhost .ssh]# vim /etc/ssh/sshd_config

将 RSAAuthentication 和 PubkeyAuthentication 的注释取消,并修改 AuthorizedKeyFile 的位置(这个位置是相对于进入用户的位置,而不是 root 下的位置)

RSAAuthentication yes #启用 RSA 认证
PubkeyAuthentication yes #启用公钥私钥配对认证方式 
# The
default is to check both .ssh/authorized_keys and .ssh/authorized_keys2 # but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile
.ssh/authorized_keys# 公钥文件路径(和上面生成的文件同)

配置好之后重启 ssh 服务:

[root@localhost .ssh]# service sshd restart
Redirecting to /bin/systemctl restart  sshd.service

5. 测试一下吧~~

[root@linuxidc ~]# ssh node117
Last login: Tue Aug 18 07:48:58 2015
[root@linuxidc ~]# exit
logout
Connection to node117 closed.
[root@linuxidc ~]# ssh node118
Last login: Tue Aug 18 07:46:59 2015 from node117
[root@qiang118 ~]# exit
logout
Connection to node118 closed.
[root@linuxidc ~]# ssh node119
Last login: Tue Aug 18 07:47:17 2015 from node117
[root@localhost ~]# exit
logout
Connection to node119 closed.
[root@linuxidc ~]# ssh node120
Last login: Tue Aug 18 07:47:39 2015 from node117
[root@localhost ~]# exit
logout
Connection to node120 closed.
[root@linuxidc ~]#  

这个是后来用非 root 用户做的免密码登录(需要注意的是,每个服务器的用户名字应该是一样的, 事实证明不然连不上 …)

[qiang@linuxidc ~]$ ssh node117
Last login: Tue Aug 18 08:59:20 2015
[qiang@linuxidc ~]$ ssh node118
Last login: Tue Aug 18 08:59:27 2015 from node117
[qiang@qiang118 ~]$ exit
logout
Connection to node118 closed.
[qiang@linuxidc ~]$ ssh node119
Last login: Tue Aug 18 08:59:40 2015 from node117
[qiang@qiang119 ~]$ exit
logout
Connection to node119 closed.
[qiang@linuxidc ~]$ ssh node120
Last login: Tue Aug 18 09:00:39 2015 from node120
[qiang@qiang120 ~]$ exit
logout
Connection to node120 closed.
[qiang@linuxidc ~]$

创建好新的用户之后,在该用户根目录下重新获取密钥,这与 root 用户下生成的密钥是不一样的~

下面关于 SSH 相关的文章您也可能喜欢,不妨参考下:

Ubuntu 下配置 SSH 服务全过程及问题解决 http://www.linuxidc.com/Linux/2011-09/42775.htm

Ubuntu 14.04 下安装 Samba 及 SSH 服务端的方法 http://www.linuxidc.com/Linux/2015-01/111971.htm

SSH 服务远程访问 Linux 服务器登陆慢 http://www.linuxidc.com/Linux/2011-08/39742.htm

提高 Ubuntu 的 SSH 登陆认证速度的办法 http://www.linuxidc.com/Linux/2014-09/106810.htm

开启 SSH 服务让 Android 手机远程访问 Ubuntu 14.04  http://www.linuxidc.com/Linux/2014-09/106809.htm

如何为 Linux 系统中的 SSH 添加双重认证 http://www.linuxidc.com/Linux/2014-08/105998.htm

在 Linux 中为非 SSH 用户配置 SFTP 环境 http://www.linuxidc.com/Linux/2014-08/105865.htm

Linux 上 SSH 服务的配置和管理 http://www.linuxidc.com/Linux/2014-06/103627.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-08/122259.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3417字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中