简单使用ssh密钥认证

来源:互联网 发布:招聘新媒体美工的要求 编辑:程序博客网 时间:2024/05/17 21:43

转自:http://phoenix007.iteye.com/blog/709455


A机器(ssh连接发起端,即客户端):

 

添加一个测试用户aaa

 

[root@A ~]# useradd aaa

 

suaaa

 

[root@A ~]# su - aaa

[aaa@A ~]$

 

ssh-keygen创建rsa密钥对

 

[aaa@A ~]$ ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/home/aaa/.ssh/id_rsa):      密钥文件的保存位置

Created directory '/home/aaa/.ssh'.             默认的密钥文件存放目录

Enter passphrase (empty for no passphrase):             密钥文件的保护密码

Enter same passphrase again:

Your identification has been saved in /home/aaa/.ssh/id_rsa.             生成的私钥

Your public key has been saved in /home/aaa/.ssh/id_rsa.pub.   生成的公钥

The key fingerprint is:

81:d7:de:e7:cb:7c:4e:16:d6:76:da:9d:30:25:76:09 aaa@A.test.com

 

[aaa@A ~]$ ll

total 32K

drwx------ 3 aaa aaa 4.0K Sep 17 16:09 .

drwxr-xr-x 4 root root 4.0K Sep 17 16:09 ..

-rw-r--r-- 1 aaa aaa   304 Sep 17 16:09 .bash_logout

-rw-r--r-- 1 aaa aaa   191 Sep 17 16:09 .bash_profile

-rw-r--r-- 1 aaa aaa   124 Sep 17 16:09 .bashrc

-rw-r--r-- 1 aaa aaa   383 Sep 17 16:09 .emacs

drwx------ 2 aaa aaa 4.0K Sep 17 16:09 .ssh             创建出的.ssh目录默认权限为700

 

注意生成的私钥文件的默认权限是rw-------,即600(确保他人不能查看)

 

[aaa@A ~]$ ll .ssh/

total 16K

drwx------ 2 aaa aaa 4.0K Sep 17 16:09 .

drwx------ 3 aaa aaa 4.0K Sep 17 16:09 ..

-rw------- 1 aaa aaa 951 Sep 17 16:09 id_rsa        私钥文件

-rw-r--r-- 1 aaa aaa 231 Sep 17 16:09 id_rsa.pub  公钥文件


B机器(ssh的被连接端,即服务端):

 

添加bbb用户

 

[root@B ~]# useradd bbb

 

subbb

 

[root@B ~]# su - bbb

 

手工在家目录下建立.ssh目录(如果不使用ssh-keygen工具的话)

 

[bbb@B ~]$ mkdir .ssh

 

[bbb@B ~]$ ls -al

total 28

drwx------ 3 bbb bbb 4096 Sep 17 16:52 .

drwxr-xr-x 4 root root 4096 Sep 17 16:52 ..

-rw-r--r-- 1 bbb bbb   304 Sep 17 16:52 .bash_logout

-rw-r--r-- 1 bbb bbb   191 Sep 17 16:52 .bash_profile

-rw-r--r-- 1 bbb bbb   124 Sep 17 16:52 .bashrc

drwxrwxr-x 2 bbb bbb 4096 Sep 17 16:52 .ssh

 

修改.ssh目录的权限为700(非常重要!)

 

[bbb@B ~]$ chmod 700 .ssh

 

[bbb@B ~]$ ls -al

total 28

drwx------ 3 bbb bbb 4096 Sep 17 16:52 .

drwxr-xr-x 4 root root 4096 Sep 17 16:52 ..

-rw-r--r-- 1 bbb bbb   304 Sep 17 16:52 .bash_logout

-rw-r--r-- 1 bbb bbb   191 Sep 17 16:52 .bash_profile

-rw-r--r-- 1 bbb bbb   124 Sep 17 16:52 .bashrc

drwx------ 2 bbb bbb 4096 Sep 17 16:52 .ssh    确认权限为700

 

.ssh目录里创建authorized_keys文件,并将A机器上aaa用户的公钥(id_rsa.pub)内容写入此文件(关键操作!)

 

[bbb@B ~]$ cd .ssh/

 

[bbb@B .ssh]$ vi authorized_keys

 

粘贴aaaid_rsa.pub内容进来

 

修改authorized_keys的权限为400(非常重要!)

 

[bbb@B .ssh]$ chmod 400 authorized_keys

 

[bbb@B .ssh]$ ls -l authorized_keys

-r-------- 1 bbb bbb 231 Sep 17 16:53 authorized_keys   确认权限为400

 

 

 

配置结束。


测试:从A机器上,在aaa用户下,使用bbb帐号登录B机器。

 

 

[aaa@A ~]$ ssh -l bbb xxx.xxx.xxx.xxx

Enter passphrase for key '/home/aaa/.ssh/id_rsa':      会提示输入私钥保护密码(在创建密钥对时输的密码,输入正确后进入系统)

[bbb@B ~]$

 

  

安全的关键点在于如何安全地将id_rsa.pub的内容传递到服务端

 

 

测试完毕,清理系统(删除测试用户帐号及其家目录)

 

 

A机器上,exitroot

 

[root@A ~]# userdel -r aaa

 

B机器上,exitroot

 

[root@B ~]# userdel -r bbb


原创粉丝点击