linux远程登录ssh免密码配置方法

作者:好大的刀 时间:2023-08-01 09:23:27 

一、情景

公司刚上几台Linux,现在要把主机之间都能远程ssh免密码登陆。

二、原理
很简单,使用ssh-keygen 在主机A上生成private和public密钥,将生成的public密钥拷贝到远程机器主机B上后,就可以使用ssh命令无需密码登录到另外一台机器主机B上。

三、步骤

主机A:

1、 生成公钥和私钥文件id_rsa和id_rsa.pub (敲3下回车即可)
[root@bogon ~]# 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:
67:da:0d:79:e0:d6:2b:cd:7d:22:af:51:7e:9c:75:fe root@bogon
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| . + |
| S B o . o|
| * * = o+|
| . o B +.=|
| . + +.|
| ... E|
+-----------------+

2、ssh-cop-id命令会将指定的公钥文件复制到远程计算机

[root@bogon ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.1.250.166
The authenticity of host '10.1.250.166 (10.1.250.166)' can't be established.
RSA key fingerprint is c8:9d:6d:92:55:77:3d:3e:af:f5:cb:18:80:5a:47:93.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.250.166' (RSA) to the list of known hosts.
reverse mapping checking getaddrinfo for bogon [10.1.250.166] failed - POSSIBLE BREAK-IN ATTEMPT!
root@10.1.250.166's password: <主机B的登陆密码>
Now try logging into the machine, with "ssh 'root@10.1.250.166'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

3、然后ssh登陆主机B验证是否需要密码

[root@localhost ~]# ssh 10.1.250.166
reverse mapping checking getaddrinfo for bogon [10.1.250.166] failed - POSSIBLE BREAK-IN ATTEMPT!
Last login: Wed Oct 21 10:05:39 2015 from 10.1.250.141
[root@bogon ~]#

4、登陆成功后,我们需要在主机B也做下以上3步,这样就可以相互免密码ssh登陆。(如果有多台主机,每台主机都做下相同操作,以方便以后管理)。

5、权限问题
建议用其他用户做ssh登陆的话,调整目录文件权限
设置authorized_keys权限

chmod 644 authorized_keys

设置.ssh目录权限

chmod 700 -R .ssh

6、要保证.ssh和authorized_keys都只有用户自己有写权限。否则验证无效。

本文出自 “好大的刀” 博客

标签:ssh免密码,linux远程登录
0
投稿

猜你喜欢

  • VMware15/16解锁VMware安装MacOS的步骤详解

    2022-02-23 01:55:49
  • 如何提高IT团队的执行力?

    2008-09-01 12:31:00
  • DEDECMS鼠标 hover 显示全部标题的简便方法

    2009-02-15 10:09:00
  • 使用GDB调试多线程实例详解

    2021-02-15 09:16:36
  • 谷歌推出搜索定制功能 或是众多网站失职

    2008-12-19 13:32:00
  • 妙手回春,化解服务器登录难题(2)

    2007-03-27 10:22:00
  • 互联网营销 怎么从网站的seo迷茫中走出来

    2009-02-20 09:46:00
  • 关于robots.txt无法禁止收录的小看法

    2011-10-14 20:34:22
  • 就看做得多精准 网络定向广告与精准营销

    2009-03-04 12:48:00
  • 详解ubuntu安装opencv的正确方法

    2023-07-11 14:19:43
  • Linux虚拟机修改ip地址,查看网关,网络环境配置的教程

    2023-09-02 02:29:56
  • 百度地图推手机客户端 Windows Mobile版本上线

    2010-05-27 12:33:00
  • 银行与支付宝合作将带来新市场机会

    2009-10-27 15:30:00
  • 虚拟主机利用ISAPI_Rewrite为HTML文档设置301永久性转向

    2011-06-08 18:56:00
  • Web服务器日志工具点评

    2009-07-23 12:34:00
  • 让网站流量爆涨的十条秘决

    2008-10-13 12:16:00
  • 百度快照倒退原因分析及补救措施

    2009-02-13 09:34:00
  • 与大家分享我的四个网络赚钱方式

    2009-03-22 15:07:00
  • Linux使用 iftop 实时监控网卡的流量

    2023-10-17 11:32:58
  • Linux文件编辑命令vi详细整理(总结)

    2021-10-19 02:04:06
  • asp之家 网站运营 m.aspxhome.com