SSH 免密登录

目录结构

免密登录相关文件放在~/.ssh目录下,主要有4个文件。

[root@linux1 .ssh]# pwd
/root/.ssh
[root@linux1 .ssh]# ll
total 16
-rw-r--r--. 1 root root  393 May 21 04:48 authorized_keys
-rw-r--r--. 1 root root    0 May 21 05:05 config
-rw-------. 1 root root 1675 May 21 04:17 id_rsa
-rw-r--r--. 1 root root  393 May 21 04:17 id_rsa.pub
-rw-r--r--. 1 root root  396 May 21 04:21 known_hosts
[root@linux1 .ssh]# 

authorized_keys:存想免密登录机器的公钥
id_rsa:私钥   ##rsa、dsa:加密类型
id_rsa.pub:公钥
known_hosts:记录访问过本机的ip和公钥
config:可以配置忽略known_hosts记录等
 

生成步骤

1、生成秘钥
输入ssh-keygen,中间没有空格,输入命令之后,连续按3个回车。

[root@linux1 .ssh]# 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:
9f:93:6b:7d:52:a8:12:5d:2a:75:92:74:ef:95:29:8b root@linux3
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|          . .    |
|         . o .  o|
|          + o..o.|
|        So =.oo. |
|        o.oE..o  |
|         o=o .   |
|        . ooo .  |
|         o.  o   |
+-----------------+
[root@linux3 .ssh]# 

2、有些人会这样输入命令

[root@linux1 .ssh]# [root@linux4 .ssh]# ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa 

 -t dsa:表示使用密钥的加密类型,可以为'rsa'和'dsa'
  -P '':表示不需要密码登录

  -f ~/.ssh/id_dsa:表示密钥存放的路径为${USER}/.ssh/id_dsa

 

3、发送到别的机器

[root@linux1 .ssh]# ssh-copy-id -i id_rsa.pub linux1

本机也发一份。
 

你可能感兴趣的:(Linux,DevOps)