配置 SSH 免密登录

BBigSun 评论443阅读模式

1、工作原理

使用 ssh-keygen 工具在管理主机生成“公钥”与“私钥”,上传公钥到托管主机,即可实现管理主机免密登录到托管主机。

2、管理主机上生成“公钥”与“私钥”

$ ssh-keygen -t rsa

# 一直回车即可
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:vwf+Zr9AVIugWqv270wuawCGyIpR02cOyI/XVRO9vFc root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| . o     .=o  .  |
|  = o o .. o.o . |
|.o = * .o  .o..  |
|o.o = oo . .o   E|
|o. o .. S   .. . |
|o     .. .... .  |
|      o. .o...   |
|     . .o+..+.   |
|       .o=*=..o. |
+----[SHA256]-----+

密钥的位于 /root/.ssh 下:文章源自十年又十年-https://www.bbigsun.com/73.html

$ ls /root/.ssh/ | grep rsa

id_rsa          # 私钥
id_rsa.pub      # 公钥

3、上传“私钥”到托管主机

方法一:使用 ssh-copy-id(推荐)文章源自十年又十年-https://www.bbigsun.com/73.html

命令格式:ssh-copy-id -i <your_public_key> <username>@<ip>文章源自十年又十年-https://www.bbigsun.com/73.html

$ ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.60.129

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.60.129 (192.168.60.129)' can't be established.
ECDSA key fingerprint is SHA256:dtUbuyuVxdu/j5B4jEbLTc578YMRGXOEKh3Ht3kFUp8.
ECDSA key fingerprint is MD5:5f:b1:5c:11:9f:0d:14:2a:d8:ae:2f:13:32:f2:aa:5f.
Are you sure you want to continue connecting (yes/no)?
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.60.129's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.60.129'"
and check to make sure that only the key(s) you wanted were added.

方法二:手动上传文章源自十年又十年-https://www.bbigsun.com/73.html

scp /root/.ssh/id_rsa.pub root@192.168.60.129:/root/.ssh/
文章源自十年又十年-https://www.bbigsun.com/73.html文章源自十年又十年-https://www.bbigsun.com/73.html

纸上得来终觉浅,绝知此事要躬行。

weinxin
17688689121
我的微信
微信扫一扫
BBigSun
  • 本文由 BBigSun 发表于 2022年 11月 24日 08:59:57
  • 转载请务必保留本文链接:https://www.bbigsun.com/73.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定