Linux 能 ping 通,但是 ssh 登陆不上

1,114 阅读1分钟

问题描述

刚刚在本地装了ubuntu虚拟机,发现本地本机能ping通虚拟机网络

$ ping 172.16.0.4
PING 172.16.0.4 (172.16.0.4): 56 data bytes
64 bytes from 172.16.0.4: icmp_seq=0 ttl=64 time=0.385 ms
64 bytes from 172.16.0.4: icmp_seq=1 ttl=64 time=0.775 ms
64 bytes from 172.16.0.4: icmp_seq=2 ttl=64 time=0.697 ms

172.16.0.4ubuntu虚拟机ip,接着使用ssh命令尝试登录服务器发现登陆不上

$ ssh root@172.16.0.4
ssh: connect to host 172.16.0.4 port 22: Connection refused

问题分析

本地能ping通虚拟机,证明虚拟机与本机之间的网络连接没问题。那么就有可能是ssh服务的问题,在虚拟机中使用telnet尝试连接22端口

$ telnet 127.0.0.1 22
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

发现22端口连接不上,此时有可能是ssh服务未启动,查看ssh服务状态

$ systemctl status sshd
● sshd.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

No such file or directory 表示没安装该服务

问题解决

安装ssh服务

$ sudo apt-get install openssh-server

安装完openssh-server,再次查看sshd服务状态

$ systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-02-20 04:07:24 PST; 17min ago
 Main PID: 6437 (sshd)
   CGroup: /system.slice/ssh.service
           └─6437 /usr/sbin/sshd -D

Feb 20 04:07:24 172 systemd[1]: Starting OpenBSD Secure Shell server...
Feb 20 04:07:24 172 sshd[6437]: Server listening on 0.0.0.0 port 22.
Feb 20 04:07:24 172 sshd[6437]: Server listening on :: port 22.
Feb 20 04:07:24 172 systemd[1]: Started OpenBSD Secure Shell server.

此时sshd服务正常启动,本机再次使用ssh登陆到虚拟机

$ ssh root@172.16.0.4
root@172.16.0.4's password:

要求输入密码,证明ssh服务正常,问题解决

公众号

关注公众号: huangxy,一起学习一起进步
关注公众号: huangxy