ssh 无密码登录

   也知道这个东西,大家都会,就当是谁忘了的时候,做个提醒的吧。。。

          ssh 是一个专为远程登录会话和其他网络服务提供安全性的协议。默认状态下ssh链接是需要密码认证的,可以通过添加系统认证(即公钥-私钥)的修改,修改后系统间切换可以避免密码输入和ssh认证。以下将创建过程简单介绍下。

 

一、用ssh-keygen创建公钥

haifeng@haifeng-EX38-DS4:/$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/haifeng/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/haifeng/.ssh/id_rsa.
Your public key has been saved in /home/haifeng/.ssh/id_rsa.pub.
The key fingerprint is:
7b:75:98:eb:fd:13:ce:0f:c4:cf:2c:65:cc:73:70:53 haifeng@haifeng-EX38-DS4
The key's randomart image is:
+--[ RSA 2048]----+
|                E|
|                .|
|              ...|
|             + =.|
|        S   + +.*|
|         . . + Bo|
|        . . . = =|
|         . . . * |
|            . ..=|
+-----------------+

 

##输入后,会提示创建.ssh/id_rsa、id_rsa.pub的文件,其中第一个为密钥,第二个为公钥。过程中会要求输入密码,为了ssh访问过程无须密码,可以直接回车 。

2.查看钥匙。

[root@localhost .ssh]# ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts

###可以发现 ssh目录下的两枚钥匙。

3.将公钥复制到被管理机器上面

[root@localhost .ssh]# scp id_rsa.pub root@192.168.36.194:~/.ssh/authorized_keys
root@192.168.36.194's password: 
id_rsa.pub                                    100%  408     0.4KB/s   00:00

4.访问

# ssh 192.168.36.194
The authenticity of host '<Game2> (<192.168.36.194>)' can't be established. 
RSA key fingerprint is 34:b9:92:06:53:e6:91:4d:47:92:73:57:78:6a:5d:09. 
Are you sure you want to continue connecting (yes/no)?yes 
Warning: Permanently added '
<Game2> (<192.168.36.194>' (RSA) to the list of known hosts. 

这是因为首次访问后,ssh会在.ssh/known_hosts中保存各个认证过的主机信息:

192.168.36.194 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAppStzIRxeFn0e737z7KO1tdm6CJUoLapaaoBDZqHy0Z11cUAmpg02dbrqwU7TBY9lDFwWQcry+W8X8qk1CoPdzu8YcMCpw5425mai0/RxkB/RPZ1putL2DQrRBMRTU1m5meLOYRXYlnU5E+YElCgH+ZJ8EXiurOzDvw6vi7pASi9wMQuJosFyNmv5E9/8ULgaKg3LtvP+0O1wPxrHOBDwVq2u9Oi7T2pX8deBEnOI4uG4CGXn/p0ml+uuS4DO3Up2VjqoRtqtuzWExnTyAGS/wQNnN3mera1ERya3FomEVHJRV5K2zJRkgSF8WfETXzQ2rAliOsW/YLTGF8vVvjo5w==

5.再次访问,ssh登录发现可以不用密码登录。

[root@localhost .ssh]# ssh 192.168.36.194
Last login: Fri Apr 22 00:56:45 2011 from 192.168.18.44



可能遇到的问题:

1.进行ssh登录时,出现:”Agent admitted failure to sign using the key“ .

   执行: $ssh-add

   强行将私钥 加进来。

2.如果无任何错误提示,可以输密码登录,但就是不能无密码登录,在被连接的主机上(如A向B发起ssh连接,则在B上)执行以下几步:

  $chmod o-w ~/

   $chmod 700 ~/.ssh

   $chmod 600 ~/.ssh/authorized_keys

必须是以上两种权限,700,600.

3.如果执行了第2步,还是不能无密码登录,再试试下面几个

  $ps -Af | grep agent 

        检查ssh代理是否开启,如果有开启的话,kill掉该代理,然后执行下面,重新打开一个ssh代理,如果没有开启,直接执行下面:

       $ssh-agent

  还是不行的话,执行下面,重启一下ssh服务

       $sudo service sshd restart

4. 执行ssh-add时提示“Could not open a connection to your authenticationh agent”而失败

执行: ssh-agent bash 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值