问题描述
刚刚在本地装了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.4
为ubuntu
虚拟机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
服务正常,问题解决