Linux 配置root 用户 ssh 免密登陆

场景:
机器A:192.168.2.111
机器B:192.168.2.200

需要在机器A通过ssh免密登陆到机器B

注意:如果之前生成过公钥私钥就不要再生成了,不然会导致之前所配置的免密登陆失效,直接通过第三步 ssh-copy-id 命令发送给B机器就好了

1,登陆A机器,通过命令 ssh-keygen -t rsa 生成公钥和私钥对,输入命令后直接按三次确认键就能看到下面输出界面

[root@localhost ~]# cd
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
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:
SHA256:OyJ0tdz0H34iXRmfXzVtkYrVPGlQxbhDR/g46X8xc1E root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|             .=B*|
|             .+BE|
|        . . o.oO*|
|       o + o .*oO|
|    . . S . ...==|
|   . .   .   +.*+|
|    . . o   . =.B|
|     . . .   . oo|
|                .|
+----[SHA256]-----+
[root@localhost ~]# 

2,之后进入到.ssh文件夹下,就能看到生成的公钥和私钥文件

[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ll
总用量 8
-rw-------. 1 root root 1679 8月  13 11:03 id_rsa
-rw-r--r--. 1 root root  408 8月  13 11:03 id_rsa.pub

3,接下来通过命令 ssh-copy-id root@192.168.2.200 将公钥文件发送给机器B,弹出的框中输入B机器密码即可
在这里插入图片描述

如果需要双向免密登陆,直接登陆B机器按照步骤生成公钥私钥,然后通过ssh-copy-id命令将公钥发送给A机器就好了

4,可以直接将B机器ip设置到hosts里面,下次直接通过别名登陆就好了
一、编辑 /etc/hosts 文件

vim /etc/hosts

将192.168.2.200 b 添加到文件末尾
在这里插入图片描述
保存退出。
然后直接通过 ssh b 命令就能登陆到b机器了

生成秘钥后需要保证 .ssh目录的权限必须是700 , .ssh/authorized_keys文件权限必须是600,如果不是需要手动通过下面命令更改权限

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值