普通账号 ssh:

http://bbs.chinaunix.net/thread-3554821-1-1.html



应用场景:有时候我们要给远在北京或者国外的开发人员服务器的权限,为了保证服务器的安全性我们不想让他们知道服务器的root登陆密码,所以我们可以给他们用秘钥的登陆模式。

客户端ip:192.168.0.163

服务器ip:192.168.0.167

1.密钥认证的生成

[root@vm1 .ssh]# ifconfig eth0 | awk  '/inet addr/{print }'

          inet addr:192.168.0.163  Bcast:192.168.0.255  Mask:255.255.255.0


[root@vm1 .ssh]# ssh-keygen -t rsa

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:

8a:85:d9:37:a8:95:e2:06:0e:d3:18:91:11:60:11:68 root@vm1

The key's randomart p_w_picpath is:

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

|BBo              |

|+E               |

|o                |

| +   + o         |

|+ o + * S        |

| + o * o .       |

|  . = .          |

|   .             |

|                 |

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


公钥已经生成

现在把公钥上传到另一台服务器上去

[root@vm1 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.167:/root/.ssh/

或者使用:

[root@vm2 .ssh]# scp id_rsa.pub root@192.168.0.167:/root/.ssh/

登陆到服务器上:

服务器把客户端公钥重命名

[root@localhost .ssh]# mv id_rsa.pub authorized_keys

-rw------- 1 root root 391 Aug  4 18:31 .ssh/authorized_keys

说明公钥上传成功了。


修改ssh配置文件,设置公钥认证登录

vi /etc/ssh/sshd_config

将下边2行的注释去掉,重启ssh服务

RSAAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

/etc/init.d/sshd restart

配置完毕,现在开始登录一下


[root@www.linuxidc.com ~]# ssh 192.168.0.167
Address 192.168.72.129 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Last login: Thu Aug  4 18:08:05 2011 from 192.168.0.163
[root@localhost ~]#

现在不用输入密码(,就可以登录了。退出登录

[root@localhost ~]# exit
logout
Connection to 192.168.0.167 closed.

linux ssh 密钥认证无需输入密码即可登录,在设置密钥的同时,也可以输入密码,即可密码+密钥认证就可以完成!