如何实现Linux主机远程登录ssh免密码

简介:

一、情景

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

二、原理

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

三、步骤

主机A:

1.生成公钥和私钥文件id_rsa和id_rsa.pub (敲三下回车即可)。

 
 
  1. [root@bogon ~]# ssh-keygen -t rsa  
  2. Generating public/private rsa key pair.  
  3. Enter file in which to save the key (/root/.ssh/id_rsa):  
  4. Enter passphrase (empty for no passphrase):  
  5. Enter same passphrase again:  
  6. Your identification has been saved in /root/.ssh/id_rsa.  
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.  
  8. The key fingerprint is 
  9. 67:da:0d:79:e0:d6:2b:cd:7d:22:af:51:7e:9c:75:fe root@bogon  
  10. The key's randomart image is 
  11. +--[ RSA 2048]----+  
  12. | |  
  13. | |  
  14. | . |  
  15. | . + |  
  16. | S B o . o|  
  17. | * * = o+|  
  18. | . o B +.=|  
  19. | . + +.|  
  20. | ... E|  
  21. +-----------------+ 

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

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

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

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

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

5.权限问题

建议用其他用户做ssh登陆的话,调整目录文件权限。

设置authorized_keys权限

 
 
  1. chmod 644 authorized_keys 

设置.ssh目录权限

 
 
  1. chmod 700 -R .ssh 

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






作者:好大的刀
来源:51CTO
目录
相关文章
|
5天前
|
Ubuntu 网络安全 数据安全/隐私保护
如何在 Ubuntu 上创建一个 SSH CA 以验证主机和客户端
如何在 Ubuntu 上创建一个 SSH CA 以验证主机和客户端
13 0
|
4天前
|
存储 安全 测试技术
【超实用却暗藏杀机】sshpass:一键免密SSH登录的神器,为何生产环境却要敬而远之?探秘背后的安全隐患与替代方案!
【8月更文挑战第16天】sshpass 是一款便捷工具,可实现自动化SSH登录,简化脚本中的远程连接流程。通过后台自动处理密码输入,便于执行远程操作,如 `sshpass -p &#39;yourpassword&#39; ssh user@remotehost`。也可结合更多SSH选项使用,例如指定私钥文件。然而,因需明文传递密码,存在较大安全隐患,不适于生产环境;推荐使用公钥认证以增强安全性。
15 4
|
5天前
|
Linux 网络安全 Python
Linux离线安装Python时ssh和hashlib死活安装不上的解决方案
本文提供了Linux环境下离线安装Python时遇到的"ImportError: No module named _ssl"和"ERROR:root:code for hash md5|sha1|sha224|sha256|sha384|sha512 was not found"两个问题的解决方案,通过设置OpenSSL环境变量和编辑Python源码配置文件来解决。
9 1
|
18天前
|
安全 Linux 网络安全
紧急恢复指南:当Linux系统的启动或远程登录受阻
在Linux系统管理中,没有什么比发现系统因为配置错误而无法启动或登录更让人头疼的了。这次操作记录将介绍几种有效的恢复方法,减少潜在的业务中断影响。
|
3天前
|
存储 安全 Linux
说到Linux安全,SSH限制IP登录绕不开这3种方法!
说到Linux安全,SSH限制IP登录绕不开这3种方法!
|
28天前
|
安全 Linux 网络安全
|
5天前
|
安全 Linux Shell
如何在 Linux 服务器上配置基于 SSH 密钥的身份验证
如何在 Linux 服务器上配置基于 SSH 密钥的身份验证
12 0
|
29天前
|
Shell 网络安全 数据安全/隐私保护
MacOS Sonoma14.2.1系统SSH免密登录
【7月更文挑战第9天】在MacOS Sonoma 14.2.1中设置SSH免密登录,包括:1) 使用`ssh-keygen`生成RSA密钥对;2) 使用`ssh-copy-id`将公钥传到远程主机;3) 用`ssh-add --apple-use-keychain`添加私钥到ssh-agent,并为重启后自动添加配置自动化脚本;4) 可选地,编辑`~/.ssh/config`设置别名简化登录。确保远程主机的`.ssh/authorized_keys`文件权限为600。
|
7天前
|
Linux 网络安全 数据安全/隐私保护
Linux——配置SSH免密登录
Linux——配置SSH免密登录
18 0
|
8天前
|
运维 Linux Shell
Linux运维:批量处理远程主机
Linux运维:批量处理远程主机
19 0