ssh 公钥免密码登陆

使用ssh公钥实现免密码登录

(2011-04-22 01:24:10)
标签:

杂谈

分类: Linux系统

ssh 无密码登录要使用公钥与私钥。linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例。

有机器A(192.168.1.155),B(192.168.1.181)。现想A通过ssh免密码登录到B。
首先以root账户登陆为例。


1.在A机下生成公钥/私钥对。

[root@A ~]# ssh-keygen -t rsa -''


-P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。
该命令将在/root/.ssh目录下面产生一对密钥id_rsa和id_rsa.pub。

一般采用的ssh的rsa密钥:
id_rsa     私钥
id_rsa.pub 公钥
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1

2.把A机下的/root/.ssh/id_rsa.pub 复制到B机的 /root/.ssh/authorized_keys文件里,先要在B机上创建好 /root/.ssh 这个目录,用scp复制。

[root@A ~]# scp /root/.ssh/id_rsa.pub root@192.168.1.181:/root/.ssh/authorized_keys
root@192.168.1.181's password:
id_rsa.pub                                    100%  223     0.2KB/s   00:00


由于还没有免密码登录的,所以要输入一次B机的root密码。

3.authorized_keys的权限要是600!!!

[root@B ~]# chmod 600 /root/.ssh/authorized_keys




4.A机登录B机。

[root@A ~]# ssh -l root 192.168.1.181
The authenticity of host '192.168.1.181 (192.168.1.181)' can't be established.
RSA key fingerprint is 00:a6:a8:87:eb:c7:40:10:39:cc:a0:eb:50:d9:6a:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.181' (RSA) to the list of known hosts.
Last login: Thu Jul  3 09:53:18 2008 from root
[root@B ~]#
原文地址:https://www.cnblogs.com/yuyezhulan/p/4011151.html