两台Linux密钥连接(SSH远程)

180 阅读2分钟
远程连接方式
[root@yang-1 ~]# ssh 192.168.29.129     //IP地址连接
Last login: Mon Sep 23 22:36:48 2019 from 192.168.29.1
[root@yang-2 ~]# 登出
Connection to 192.168.29.129 closed.
[root@yang-1 ~]# ssh root@192.168.29.129     //用户名连接
Last login: Mon Sep 23 22:37:21 2019 from 192.168.29.128
[root@yang-2 ~]# 登出
Connection to 192.168.29.129 closed.
[root@yang-1 ~]# ssh -p 22 root@192.168.29.129     //端口号连接
Last login: Mon Sep 23 22:37:47 2019 from 192.168.29.128
[root@yang-2 ~]# 登出
Connection to 192.168.29.129 closed.
[root@yang-1 ~]# 
生成密钥对
[root@yang-1 ~]# ssh-keygen     //生成密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):      //回车
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? yes     //输入:yes
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:HfjV2dLRdczZs4Y3jh9QMQMiI7nS7Y4OSDltXAwwSpc root@yang-1
The key's randomart image is:
+---[RSA 2048]----+
| . +o. ..o . ..BO|
|. oE. o...o .. BX|
| .    .o+ . . * =|
|    +..o + o o * |
|   + +. S o   * .|
|  . +    .   . o |
|   . .  o     . .|
|      .. .     . |
|      ..         |
+----[SHA256]-----+
[root@yang-1 ~]# 
查看生成的密钥对
[root@yang-1 ~]# ls /root/.ssh     //查看是否生成密钥对
authorized_keys  id_rsa  id_rsa.pub  known_hosts     //id_rsa为公钥,id_rsa.pub为私钥
查看私钥文件内容
[root@yang-1 ~]# cat /root/.ssh/id_rsa.pub     //查看私钥文件内容
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCy17w4gqi/JOLvYkFobfhVsopAAenXfvXbK3X57Lxg2PE0RmP4030822HI+8oPiNecDelAmQmkAnb7gcG68pkt8k0RrGscyNdCOVHdIjxIymB59Jc1Es3bCUIYs7+YRLC6tW5tKhLg2nhPamiHsgXKVBrvm/N2XcmQAqep1MhCJGX6kDbLg6lPvnkVgd1F0kNYup7LK1LrTWdOESH7ixwqmtFDt0WOhzRKL1Sp4wXsXlswPk29kS0wo93+Pb9lxCPHLi9Zy8c9D+PQ4DpDYas/F89EFTtZ3Zl3RfRqFndsyZM8XTFiiLZ32nTD0SFwcD6buW7E68ZeiFUFvY1BTr4D root@yang-1

把生成密钥对中的私钥复制到另一台Linux系统中

编辑另一台Linux私钥配置文件
[root@yang-2 ~]# vi /root/.ssh/authorized_keys     //创建一个私钥文件,把生成的私钥内容复制过来
编辑配置文件
#Linux key     //标注说明一下内容
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCy17w4gqi/JOLvYkFobfhVsopAAenXfvXbK3X57Lxg2PE0RmP4030822HI+8oPiNecDelAmQmkAnb7gcG68pkt8k0RrGscyNdCOVHdIjxIymB59Jc1Es3bCUIYs7+YRLC6tW5tKhLg2nhPamiHsgXKVBrvm/N2XcmQAqep1MhCJGX6kDbLg6lPvnkVgd1F0kNYup7LK1LrTWdOESH7ixwqmtFDt0WOhzRKL1Sp4wXsXlswPk29kS0wo93+Pb9lxCPHLi9Zy8c9D+PQ4DpDYas/F89EFTtZ3Zl3RfRqFndsyZM8XTFiiLZ32nTD0SFwcD6buW7E68ZeiFUFvY1BTr4D root@yang-1

:wq     //输入i开始输入内容,按esc停止输入,:wq保存输入内容
关闭防火墙
[root@yang-2 ~]# setenforce 0     //关闭防火墙
验证是否成功
[root@yang-1 ~]# ssh -p 22 root@192.168.29.129     //ssh连接另一台Linux
Last failed login: Mon Sep 23 23:45:27 CST 2019 from 192.168.29.128 on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Mon Sep 23 23:44:29 2019 from 192.168.29.128
[root@yang-2 ~]#     //不需要输入密码,直接可以登录
[root@yang-2 ~]# 登出
Connection to 192.168.29.129 closed.
[root@yang-1 ~]# 

验证成功