linux的ssh免密码登录

2018-04-10  本文已影响24人  码农梦醒

1. 确认用户

whoami

2. 生成key

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 (/home/froad/.ssh/id_rsa): #私钥存放的位置,默认会存放在用户目录的.ssh文件夹,直接回车
Enter passphrase (empty for no passphrase): #默认,回车
Enter same passphrase again: #默认,回车
Your identification has been saved in /home/froad/.ssh/id_rsa.#私钥路径
Your public key has been saved in /home/froad/.ssh/id_rsa.pub.#公钥路径
The key fingerprint is:
e8:b6:e6:xxxxxxxxxxxxxxxxx:ec:b5:d8 froad@localhost.localdomain

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

3. 配置

将authorized_keys文件拷贝到需要被管理的电脑上。注意:放在用户目录下.ssh文件夹中

4.验证

ssh root@192.168.2.xxx

The authenticity of host '192.168.2.xxx (192.168.2.xxx)' can't be established.
RSA key fingerprint is ff:07:49:4d:xxxxxxxxxxe:2c:38.
Are you sure you want to continue connecting (yes/no)? yes
#同意将指纹添加到本地
Warning: Permanently added '192.168.2.xxx' (RSA) to the list of known hosts.
Last login: Wed Oct 23 13:58:32 2013 from 192.168.1.xxx

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

5. 其它说明

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

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

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

chmod 644 ~/.ssh/authorized_keys

转自: https://blog.csdn.net/hqzxsc2006/article/details/48997167

上一篇下一篇

猜你喜欢

热点阅读