SSH双机互信及错误解决大全

简介:

    在linux系统管理中,设置免密码登录,进行机器的批量管理是最常用的一个方法。比如针对几十甚至上百台线上机器,通常我们会设置一台“发布机”作为中央控制机对其它线上机器免密码登录,然后进行软件、配置文件的分发、更新、部署。当然了,针对上面的问题,解决方案并非最优且唯一,比如你也可以用expect模拟自动输入来完成自动登录验证这一过程,或者用现在一些开源的软件自动化配置和部署工具,比如 Puppet,但这都不在本文的讨论范畴,今天咱们要说的就是这种最原始、最有效、最直接的方式:免密码登录。 

一、环境介绍

两台机器

1、个人机 192.168.1.2 主机名 jacken

2、服务器 192.168.1.3 主机名 lamp


二、操作步骤

1、个人机创建公钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@jacken ~] # 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:
64:f3:67:a7:8e:11:d8:b1:fc:7d:34:f4:57:0d:e3:86 root@jacken
The key's randomart image is:
+--[ RSA 2048]----+
|              o  |
|             o o.|
|        + . E o.o|
|       o * o .. o|
|        S * o ..+|
|           = + .o|
|          . o . .|
|           +   . |
|          . .    |
+-----------------+
[root@jacken ~] #

2、复制公钥到服务器并被信任

1
2
3
4
5
6
[root@jacken ~] # ssh-copy-id root@192.168.1.3
root@192.168.1.3's password: 
Now try logging into the machine, with  "ssh 'root@192.168.1.3'" , and check  in :
   . ssh /authorized_keys
to  make  sure we haven 't added extra keys that you weren' t expecting.
[root@jacken ~] #

3、测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@jacken ~] # ssh 192.168.1.3 hostname
lamp
[root@jacken ~] # ping server01
PING server01 (192.168.1.3) 56(84) bytes of data.
64 bytes from server01 (192.168.1.3): icmp_seq=1 ttl=64  time =0.855 ms
^C
--- server01  ping  statistics ---
1 packets transmitted, 1 received, 0% packet loss,  time  509ms
rtt min /avg/max/mdev  = 0.855 /0 .855 /0 .855 /0 .000 ms
[root@jacken ~]
[root@jacken ~] # ssh server01 'ifconfig eth0'
eth0      Link encap:Ethernet  HWaddr 00:0C:29:21:CB:7C  
           inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe21:cb7c /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:2756 errors:0 dropped:0 overruns:0 frame:0
           TX packets:2072 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:266491 (260.2 KiB)  TX bytes:301395 (294.3 KiB)
[root@jacken ~] #


三、常见错误

1、在使用 ssh-copy-id 错误提示

错误提示:

ssh-copy-id:/usr/bin/ssh-copy-id: ERROR: No identities found 

解决方法:

缺少公钥路径,加上IP即可

ssh-copy-id -i ~/.ssh/id_dsa.pub user@remote_ip 

2、ssh ip 可以成功登录,ssh hostname 却失败

错误提示:

ssh: connect to host localhost port 22: Connection refused

解决方法:

看下对方的主机名是不是在 /etc/hosts 文件中和 ip 做了映射,没有就加上即可。

3、ssh 的配置目录权限问题

错误提示:

登录的时候依旧让输入密码

由于 ssh 的权限直接关系到服务器的安全问题,因此 ssh 每次读取配置都会校验相关文件夹和文件的权限,以防止权限过大对外暴露。 

解决方法:

服务端的.ssh目录权限要是700 authorized_keys权限600

4、ssh localhost:publickey 授权失败

解决办法:

#vim /etc/ssh/sshd_config 

开启这3行,重启服务即可

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile     .ssh/authorized_keys

5、ssh localhost:需要密码 

解决方法:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys 

6、Selinux与Iptables阻拦

解决方法:

关闭Selinux:

#sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config (重启生效)

#setenforce 0 (警告模式,相当于临时关闭)

设置Iptables:

#iptables -A INPUT -p tcp --dport 22 -j ACCEPT

#iptables -A INPUT -p tcp --dport 1234 -j ACCEPT

#/etc/rc.d/init.d/iptables save

#/etc/rc.d/init.d/iptables restart


本文转自Jacken_yang 51CTO博客,原文链接:http://blog.51cto.com/linuxnote/1641930,如需转载请自行联系原作者

相关文章
|
3月前
|
Linux 网络安全
Linux命令(124)之ssh
Linux命令(124)之ssh
33 2
|
5月前
|
监控 数据可视化 安全
Linux——怎样使用SSH服务实现远程UI界面本地显示
需求场景 最近几天需要实现软件的远程监控,但是实际场景又不能使用向日葵、VNC、AnyDesk、以及其他的监视软件,并且软件的整体设计也没有这块的数据上行设计。
175 0
|
5月前
|
Linux 网络安全 开发工具
Linux之ssh
Linux之ssh
39 0
|
5月前
|
Linux 网络安全
linux端口连通性测试telnet、wget、ssh、curl
linux端口连通性测试telnet、wget、ssh、curl
|
4月前
|
安全 网络协议 Linux
Linux ssh 命令详解
Linux ssh 命令详解
115 0
|
6月前
|
网络协议 Ubuntu Linux
VSCode使用Remote SSH远程连接Linux服务器【远程开发】
VSCode使用Remote SSH远程连接Linux服务器【远程开发】
|
1月前
|
网络协议 Ubuntu Linux
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
111 0
|
1月前
|
安全 Linux Shell
|
2月前
|
安全 网络协议 Linux
|
2月前
|
存储 Linux 网络安全
如何在 Linux 中删除 SSL 证书和 SSH 密码?
如何在 Linux 中删除 SSL 证书和 SSH 密码?
79 1
如何在 Linux 中删除 SSL 证书和 SSH 密码?