ssh实现免密登录服务器

【原文链接】ssh实现免密登录服务器

需求:实现从服务器A免密登录到服务器B上

以下以两台服务器为例

服务器A:192.168.1.11

服务器B:192.168.1.100

需求:实现从服务器A(192.168.1.11)通过ssh 192.168.1.100 免密的方式登录到服务器B(192.168.1.100)

解决方案

(1)在服务器A上查看是否有公钥和私钥

ls /root/.ssh/

如下,没有id_rsa文件和id_rsa.pub文件,说明当前服务器上尚未生成公钥和私钥

[root@centos7-1 ~]# ls /root/.ssh
known_hosts
[root@centos7-1 ~]#

(2)若服务器已经存在公钥和私钥了,则跳过当前步骤,若没有,则通过如下命令生成公钥和私钥,即执行 ssh-keygen 命令,然后全部默认回车

[root@centos7-1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:I+IRvm6TalpTJoYUYNi7fxMnGufeqkp3ZBMWC7E9J9E root@centos7-1
The key's randomart image is:
+---[RSA 2048]----+
|++  o.o.         |
|o o  + +E        |
| . .o B .        |
|.... o =         |
|. o.* = S        |
| ..=.*+o..       |
|  +.+*.+         |
| o.+*.+.         |
|.oo+o=oo.        |
+----[SHA256]-----+
[root@centos7-1 ~]#

(3)其中 /root/.ssh/id_rsa 为私钥,/root/.ssh/id_rsa.pub为公钥,然后将生成的公钥通过如下命令上传至服务器B

ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

(4)然后就可以通过ssh直接免密登录了

ssh [email protected]

你可能感兴趣的:(Linux,服务器,ssh,linux)