出现问题

当使用Xshell连接虚拟机时出现:Could not connect to '192.168.xxx.xxx' (port 22): Connection failed.

出现原因

  • IP地址错误
  • 防火墙未开放22端口
  • ssh未安装/ssh服务未开启

解决问题

  1. IP地址错误
  • 查看IP地址是否可连接
#ping 命令查看ip地址是否可连接
ping 192.168.130

#若出现连接超时
#正在 Ping 192.168.127.130 具有 32 字节的数据:
#请求超时。
#请求超时。
#请求超时。
#请求超时。
#192.168.127.130 的 Ping 统计信息:
#数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),

image.png

  • 查看IP地址
#进入虚拟机 ifconfig 查看IP地址
ifconfig

image.png

  • shell填入新的IP,并重新连接

image.png

  1. 防火墙未开启22端口
#使用ping命令可以连接的话

#查询所有端口
netstat -anp

#查询22端口情况
netstat -nltp|grep 22

#防火墙开启22端口
firewalld -cmd --zone=public --add-port=22/tcp --permanent

#重新加载防火墙规则
firewalld -cmd --reload

#重启防火墙
systemctl restart firewalld

#查询22端口情况
netstat -nltp|grep 22

#Xshell尝试重新连接

image.png

  1. ssh未安装
  • 查看是否安装ssh
yum -q list openssh-server

image.png

  • 若已安装shh,查看是否处于运行状态
systemctl status sshd

image.png

  • 若未处于运行状态,开启shh服务
systemctl start sshd

image.png

  • 安装ssh
#通过yum命令安装ssh
yum -y install openssh.server

#设置开机自启
systemctl enable sshd

#开启ssh服务
systemctl start sshd

#查看ssh状态
systemctl start sshd

#查询22端口情况
netstat -nltp|grep 22

#防火墙开启22端口
firewalld -cmd --zone=public --add-port=22/tcp --permanent

#重新加载防火墙规则
firewalld -cmd --reload

#重启防火墙
systemctl restart firewalld

#查询22端口情况
netstat -nltp|grep 22

#Xshell尝试重新连接

image.png

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐