SSH密钥登录

| |
[不指定 2023/06/03 11:30 | by 刘新修 ]

 ### 本地机器生成密钥

ssh-keygen -t rsa
 
[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:PeMmQH0e2HL+jxZjYJ6/8Hej+ANJ06gm+sUIhzCLNho root@localhost
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|       . o       |
|   o  . + = o    |
|  . +..  Bo= .   |
|E+ . o..SoOoo    |
|o..   ooo+o*+    |
|.     ..+o+ooo   |
|     .  .o o++...|
|      ..   o==+..|
+----[SHA256]-----+
[root@localhost ~]# 
 
进入主机A ~/.ssh/ 目录,会发现生成了一对密钥
Your identification has been saved in /home/test/.ssh/id_rsa.
Your public key has been saved in /home/test/.ssh/id_rsa.pub.
 
### A将公钥文件中的内容复制到 ==> 远程服务器端B
~/.ssh/id_rsa.pub
~/.ssh/authorized_keys
 
## 步骤如下
[root@localhost .ssh]# pwd
/root/.ssh
[root@localhost .ssh]# ll
total 12
-rw------- 1 root root 891 Jun  1 14:52 id_rsa
-rw-r--r-- 1 root root 228 Jun  1 14:52 id_rsa.pub
-rw-r--r-- 1 root root 172 Jun  1 22:28 known_hosts
[root@localhost .ssh]# vi /root/.ssh/authorized_keys
 
### 粘贴id_rsa.pub内的字符串到这个文件,对于OpenSSH来说,我们需要将公钥加入到~/.ssh/authorized_keys中。每个公钥一行,千万注意不要换行,否则可能不能正常工作。
### 修改文件权限
chmod 600 /root/.ssh/authorized_keys
 
 
### 修改/etc/ssh/sshd_config 文件
修改/etc/ssh/sshd_config 文件,将RSAAuthentication 和 PubkeyAuthentication 后面的值都改成yes
将PasswordAuthentication yes 修改成 PasswordAuthentication no
 
### 重启SSHD
/etc/init.d/sshd restart
Linux/Unix | 评论(0) | 引用(0) | 阅读(399)