配置Linux系统ssh免密码登陆

作者:Fly的狐狸 浏览(1327) 评论(2)

1、确认当前用户:

[root@localhost ~]# whoami

root

 

2、生成Key

[root@localhost ~]# cd ~ #回到用户目录,不回去也没有关系

 

[root@localhost ~]# ssh-keygen

#可以使用-t选项选择加密方式,包括 RSA 和 DSA 两种密钥#例如:$ssh-keygen -t dsa 或者ssh-keygen -t rsa#加密方式不同,key的名称不同,其他没有区别#如果没有指定密钥,默认RSA

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): #私钥存放的位置,默认会存放在用户目录的.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:

df:b5:44:c6:b2:10:1d:88:37:8a:8c:34:d5:bd:98:b9 root@localhost

The key's randomart image is:

+--[ RSA 2048]----+

|     ... o.o..   |

|    o   o =...   |

|   . + . *.o. +  |

|    . o = .. =   |

|        S.  . o  |

|        E. . o . |

|          . . .  |

|                 |

|                 |

+-----------------+

 

 

[root@localhost .ssh]# cat id_rsa.pub >> authorized_keys   #***代表加密方式,将公钥拷贝到authorized_keys文件中。如果你有很多的电脑需要配置,将所有的id_***.pub公钥拷贝到一个authorized_keys文件中即可

3、配置

将authorized_keys文件拷贝到需要被管理的电脑上。注意:放在用户目录下.ssh文件夹中。Linux用户会限制你的访问权限。


4、验证

[root@localhost .ssh]# ssh root@192.168.169.128

#root是用户名,可以根据你的需求改变

The authenticity of host '192.168.169.128 (192.168.169.128)' can't be established.

RSA key fingerprint is 50:1b:64:2f:53:0e:c1:f8:de:82:e2:02:56:ef:08:43.

Are you sure you want to continue connecting (yes/no)? yes

#同意将指纹添加到本地

Warning: Permanently added '192.168.169.128' (RSA) to the list of known hosts.

Address 192.168.169.128 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

 

 只能在你生成key的电脑上访问authorized_keys的电脑,如果你想两台电脑互相访问均免密码。那么你还需要重复上面的步骤(机器的配置刚好相反)。

 

5、其他

   如果添加指纹的时候提示添加失败,是因为你以前添加过了这个ip的指纹。

   解决办法:将.ssh目录的known_hosts文件删除掉(好粗暴啊( o )啊!),也可以打开这个文件把对于ip的那条记录删除(这个就精细多了O(∩_∩)O哈哈~

   如果操作步骤都正确,但是依然要求输入密码。一般是因为权限的问题。命令如下

chmod 644 ~/.ssh/authorized_keys

 


没有登录不能评论
头像 风..
哈哈 这个好
2018-02-26 11:30:43
头像 风..
mmm
2018-02-26 11:30:18