无法ssh到另一台计算机,但是可以ping它吗?


20

无法SSH到另一台计算机,但可以ping通吗?不确定我缺少什么?
使用Netgear路由器

bash-3.2$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
        inet6 ::1 prefixlen 128 
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
        inet 127.0.0.1 netmask 0xff000000 
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        ether xx:xx:xx:xx:xx:xx 
        media: autoselect (none)
        status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        ether xx:xx:xx:xx:xx:xx 
        inet6 xxxx::xxxx:xxxx:xxxx:xxxxxx prefixlen 64 scopeid 0x5 
        inet 10.0.0.3 netmask 0xffffff00 broadcast 10.0.0.255
        media: autoselect
        status: active
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
        lladdr xx:xx:xx:xx:xx:xx:xx:xx 
        media: autoselect <full-duplex>
        status: inactive
bash-3.2$ ssh jeremy@10.0.0.4
ssh: connect to host 10.0.0.4 port 22: Connection refused
bash-3.2$ ssh -p 5900 jeremy@10.0.0.4
ssh: connect to host 10.0.0.4 port 5900: Connection refused
bash-3.2$ ping 10.0.0.3
PING 10.0.0.3 (10.0.0.3): 56 data bytes
64 bytes from 10.0.0.3: icmp_seq=0 ttl=64 time=0.046 ms
64 bytes from 10.0.0.3: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 10.0.0.3: icmp_seq=2 ttl=64 time=0.078 ms
64 bytes from 10.0.0.3: icmp_seq=3 ttl=64 time=0.077 ms
64 bytes from 10.0.0.3: icmp_seq=4 ttl=64 time=0.079 ms
64 bytes from 10.0.0.3: icmp_seq=5 ttl=64 time=0.081 ms
64 bytes from 10.0.0.3: icmp_seq=6 ttl=64 time=0.078 ms
^C
--- 10.0.0.3 ping statistics ---
7 packets transmitted, 7 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.046/0.074/0.081/0.011 ms
bash-3.2$ ping 10.0.0.4
PING 10.0.0.4 (10.0.0.4): 56 data bytes
64 bytes from 10.0.0.4: icmp_seq=0 ttl=64 time=2.667 ms
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=2.675 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=2.969 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=2.663 ms
64 bytes from 10.0.0.4: icmp_seq=4 ttl=64 time=2.723 ms
^C
--- 10.0.0.4 ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 2.663/2.739/2.969/0.117 ms
bash-3.2$ 

Answers:


17

服务器不是在运行sshd(因此没有在端口22上监听),或者在防火墙上阻止了端口22(默认的ssh端口),或者在极少数情况下在其他端口上运行ssh(几乎肯定不是这种情况) 。

首先检查以确保已安装sshd(使用debian示例)

sudo apt-get install openssh-server

如果是这样,它是否正在运行:

ps -ef | grep sshd

然后检查它是否正在监听端口22

sudo netstat -nlp | grep :22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      946/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      946/sshd

然后检查您的防火墙规则(这有很大的不同,因此我将显示一个debian / ubuntu / etc示例):

sudo ufw status

sudo ufw show listening
tcp:
  22 * (sshd)
  24224 * (ruby)
tcp6:
  22 * (sshd)
  8080 * (java)
udp:
  123 10.X.Y.Z (ntpd)
  123 * (ntpd)
  18649 * (dhclient)
  24224 * (ruby)
  34131 * (ruby)
  60001 10.87.43.24 (mosh-server)
  68 * (dhclient)
udp6:
  123 fe80::1031:AAAA:BBBB:CCCC (ntpd)
  123 * (ntpd)
  48573 * (dhclient)

如果ufw显示为已关闭,则运行(同样是debian / ubuntu示例)

sudo ufw allow 22

1
FWIW,它有点常见的有面向外部的机器不同的端口,以减轻攻击表面上运行SSH
SG

3

有点怪异的,但请确保您的IP不变。我曾经遇到过这个问题-我将.bashrc别名设置alias sshdev='ssh me@123.2.3.4'为我的典型登录方式,有一天,我开始出现以下错误:

ME-M-216C:~ me$ sshdev 
ssh: connect to host 123.2.3.4 port 22: Connection refused

我们只是在工作中停电了,所以重置了IP,所以我成功ping通了IP,但它不是正确的机器。您可以使用nslookup <IP>它来确保您尝试输入的机器名称正确ssh


1

当您收到消息“连接被拒绝”时,这意味着守护程序没有在该端口上侦听,或者防火墙正在拒绝该连接。要解决此问题,请确保该服务器ssh正在运行,并且本地防火墙规则未拒绝该端口上的传入连接。


1

我在Linux Lite中也遇到了同样的问题。为了解决此问题,我必须访问“设置”>“防火墙配置”。登录到root用户后,我将传入的设置更改为Allow,并且可以使用。


0

两个想法。

  1. 防火墙是否允许通过端口22连接到计算机?
  2. ssh守护程序(sshd)是否正在运行?


0

通常遵循的步骤:1)ping到目标主机,然后验证并交叉检查输入的IP地址。2)检查两台主机上的sudo service sshd状态。如果停止,请启动sshd服务。如果找不到错误的sshd.service,请安装openssh-server-> sudo apt install -y openssh-server并重新启动sshd.service 3)禁用防火墙或更改配置文件应被视为最后一个选择。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.