ssh认证的完整描述:https://www.ibm.com/developerworks/cn/linux/security/openssh/part1/
今天我们只说生成ssh的key,从而达到免密码登陆的目的。
不知道ssh是什么的自己看说明。好了,不废话了。
系统:CentOS 5.8
- 确认用户
确认当前用户是你需要的用户!
1
2
3 1$ whoami
2froad #froad用户,根据你自己的需求选择用户。我这儿用froad用户演示。
3
- 生成key
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1$ cd ~ #回到用户目录,不回去也没有关系
2$ ssh-keygen
3#可以使用-t选项选择加密方式,包括 RSA 和 DSA 两种密钥
4#例如:$ssh-keygen -t dsa 或者ssh-keygen -t rsa
5#加密方式不同,key的名称不同,其他没有区别
6#如果没有指定密钥,默认RSA
7Generating public/private rsa key pair.
8Enter file in which to save the key (/home/froad/.ssh/id_rsa): #私钥存放的位置,默认会存放在用户目录的.ssh文件夹,直接回车
9Enter passphrase (empty for no passphrase): #默认,回车
10Enter same passphrase again: #默认,回车
11Your identification has been saved in /home/froad/.ssh/id_rsa.#私钥路径
12Your public key has been saved in /home/froad/.ssh/id_rsa.pub.#公钥路径
13The key fingerprint is:
14e8:b6:e6:xxxxxxxxxxxxxxxxx:ec:b5:d8 froad@localhost.localdomain
15
16$ cat id_***.pub >> authorized_keys #***代表加密方式,将公钥拷贝到authorized_keys文件中。如果你有很多的电脑需要配置,将所有的id_***.pub公钥拷贝到一个authorized_keys文件中即可
17
- 配置
将authorized_keys文件拷贝到需要被管理的电脑上。注意:放在用户目录下.ssh文件夹中。Linux用户会限制你的访问权限
验证
1
2
3
4
5
6
7
8
9 1$ ssh root@192.168.2.xxx
2#root是用户名,可以根据你的需求改变
3The authenticity of host '192.168.2.xxx (192.168.2.xxx)' can't be established.
4RSA key fingerprint is ff:07:49:4d:xxxxxxxxxxe:2c:38.
5Are you sure you want to continue connecting (yes/no)? yes
6#同意将指纹添加到本地
7Warning: Permanently added '192.168.2.xxx' (RSA) to the list of known hosts.
8Last login: Wed Oct 23 13:58:32 2013 from 192.168.1.xxx
9
只能在你生成key的电脑上访问authorized_keys的电脑,如果你想两台电脑互相访问均免密码。那么你还需要重复上面的步骤(机器的配置刚好相反)。
- 其他
如果添加指纹的时候提示添加失败,是因为你以前添加过了这个ip的指纹。
解决办法:将.ssh目录的known_hosts文件删除掉(好粗暴啊( ⊙ o ⊙ )啊!),也可以打开这个文件把对于ip的那条记录删除(这个就精细多了O(∩_∩)O哈哈~)
如果操作步骤都正确,但是依然要求输入密码。一般是因为权限的问题。命令如下
1
2 1chmod 644 ~/.ssh/authorized_keys
2