STEP1
在任意一个Linux机器上利用ssh-keygen
命令选择一种加密算法,生成一个密钥对。输入保存密钥对的位置和密码,输入完毕会在指定的目录,默认为/root/.ssh/
下生成密钥对
-
语法
ssh-keygen [选项]
-
选项
-
-t TYPE
加密算法类型,TYPE可选值dsa | ecdsa | ed25519 | rsa | rsa1
默认采用rsa
算法
-
[root@God ~]# ssh-keygen
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:gI3wr9NX6MWwshGZeihUjRX9LPNKpKp206LcyHO/FcE root@God
The key‘s randomart image is:
+---[RSA 2048]----+
| . .+oo |
| +.+.+. |
| . + * Eo |
| . + o+*o |
| . o =oS++ |
| . +.=.+. |
| ooo.+. |
| ooo*..o. |
| .**.+o. |
+----[SHA256]-----+
[root@God ~]# cd .ssh/
[root@God .ssh]# ll
-rw-------. 1 root root 1679 Jun 24 17:00 id_rsa
-rw-r--r--. 1 root root 390 Jun 24 17:00 id_rsa.pub
STEP2
将公钥文件id_rsa.pub
中的文本内容添加到需要免密登录的远程机器上的authorized_keys
文件中
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- 需要将
id_rsa.pub
文件上传过去或直接用文本编辑器打开复制粘贴也行 - 没有
authorized_keys
文件新建一个就行
然后设置文件的权限(这个权限必须是这样,貌似权限比这大了都不行,这是个坑 = =,至于为啥暂时还不理解 )
chmod -R 700 ~/.ssh
chmod 600 authorized_keys
SETP3
拿着私钥id_rsa
就可以实现免密登陆了
-
如果使用其他Linux机器登录,只需要将
id_rsa
文件拷贝到该机器的~/.ssh/
目录下即可 -
使用xshell进行登录,将
id_rsa
下载到本地计算机,点击浏览选择该文件即可