解决Ubuntu无法上网但能Ping通网络问题

1 问题概述

在Linux系统中,尤其是Ubuntu,网络问题是一个常见但又令人头疼的问题。你可能会遇到这样的情况:系统可以ping通网络,但浏览器却无法打开网页,或者其他的网络应用无法正常工作。本文将详细探讨这一问题,并提供多种解决方案,帮助你彻底解决Ubuntu无法上网的问题。

当你发现Ubuntu系统可以ping通外部网络(如ping 8.8.8.8成功),但无法通过浏览器访问网页时,通常意味着网络连接的某些部分出现了问题。这可能是由于DNS配置不当、防火墙设置、网络服务故障等原因引起的。


2 常见故障排除命令

在开始解决问题之前,了解一些常用的网络故障排除命令是非常有帮助的:

  1. ping:检查远程服务器的可达性。
  2. curl:用于数据传输,帮助排查网络问题。
  3. wget:下载文件,检查网络连通性。
  4. ip:替代ifconfig,配置和检索网络接口信息。
  5. dig:检索与域名相关的DNS记录。
  6. nslookup:类似于dig,用于DNS查询。
  7. traceroute:查找到目标服务器的路径,显示跃点之间的时间。
  8. mtr:结合ping和traceroute,提供更多中间主机信息。

3 解决方案

3.1 重启网络服务

有时候,简单的重启网络服务就能解决问题。

1
2
sudo systemctl start NetworkManager
sudo systemctl restart NetworkManager

3.2 修改NetworkManager配置

如果重启服务无效,可以尝试修改NetworkManager的配置。

1
2
3
su root
apt-get install gedit
gedit /etc/NetworkManager/NetworkManager.conf

将文件中的managed=False改为managed=True,保存并退出编辑器。


3.3 检查DNS配置

DNS配置不当是导致无法上网的常见原因。

方案1:

修改 /etc/resolv.conf配置文件,但很快就会被覆盖,文件第二行已经声明Do not edit.

1
sudo vi /etc/resolv.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 显示如下
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

# nameserver 127.0.0.53
nameserver 8.8.8.8
nameserver 114.114.114.114
options edns0 trust-ad
search .

确保文件中有可用的DNS服务器,如:

1
2
nameserver 8.8.8.8
nameserver 4.4.4.4

nameserver改为8.8.8.8后保存,再次ping www.baidu.com恢复正常,不出意外的话很快就会发现/etc/resolve.conf又回去了,只好再次寻找查找解决办法。

方案二:

修改/etc/systemd/resolved.conf,去除DNS前的#,修改值如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#  This file is part of systemd.
2 #
3 # systemd is free software; you can redistribute it and/or modify it under the
4 # terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2.1 of the License, or (at your option)
6 # any later version.
7 #
8 # Entries in this file show the compile time defaults. Local configuration
9 # should be created by either modifying this file, or by creating "drop-ins" in
10 # the resolved.conf.d/ subdirectory. The latter is generally recommended.
11 # Defaults can be restored by simply deleting this file and all drop-ins.
12 #
13 # Use 'systemd-analyze cat-config systemd/resolved.conf' to display the full config.
14 #
15 # See resolved.conf(5) for details.
16
17 [Resolve]
18 # Some examples of DNS servers which may be used for DNS= and FallbackDNS=:
19 # Cloudflare: 1.1.1.1#cloudflare-dns.com 1.0.0.1#cloudflare-dns.com 2606:4700:4700:: 1111#cloudflare-dns.com 2606:4700:4700::1001#cloudflare-dns.com
20 # Google: 8.8.8.8#dns.google 8.8.4.4#dns.google 2001:4860:4860::8888#dns.google 2001:4860:4860::8844#dns.google
21 # Quad9: 9.9.9.9#dns.quad9.net 149.112.112.112#dns.quad9.net 2620:fe::fe#dns.qu ad9.net 2620:fe::9#dns.quad9.net
22 DNS=8.8.8.8 114.114.114.114
23 #FallbackDNS=
24 #Domains=
25 #DNSSEC=no
26 #DNSOverTLS=no
27 #MulticastDNS=no
28 #LLMNR=no
29 #Cache=no-negative
30 #CacheFromLocalhost=no
31 #DNSStubListener=yes
32 #DNSStubListenerExtra=
33 #ReadEtcHosts=yes
34 #ResolveUnicastSingleLabel=no

重启域名解析服务

1
2
systemctl restart systemd-resolved
systemctl enable systemd-resolved

备份当前的/etc/resolve.conf,并重新设置/run/systemd/resolve/resolv.conf 到/etc/resolve.conf的软链接

1
2
mv /etc/resolv.conf /etc/resolv.conf.bak
ln -s /run/systemd/resolve/resolv.conf /etc/

大功告成


3.4 配置静态IP和网关

有时候,自动获取的IP地址可能有问题,可以尝试手动配置静态IP。

1
sudo vi /etc/network/interfaces

添加以下内容:

1
2
3
4
5
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1

然后重启网络服务:

1
sudo service networking restart

3.5 关闭防火墙

防火墙设置可能导致网络访问问题。

1
sudo ufw disable

3.6 检查桥接模式配置

如果你在使用虚拟机,桥接模式配置不当也可能导致问题。

确保虚拟机的网络配置正确,并且宿主机的防火墙允许虚拟机发起的网络请求。


3.7 检查IP地址冲突

IP地址冲突也可能导致网络问题。确保没有其他设备使用相同的IP地址。

1
ifconfig

如果有冲突,尝试设置一个不同的静态IP地址。


4 案例分析

案例1:

用户在VMware虚拟机上安装了Ubuntu 20.04,发现虚拟机无法上网,但本机可以ping通虚拟机。经过排查,改为桥接模式并重启网络后问题解决。

案例2:

用户在Ubuntu系统中无法访问网页,但可以ping通8.8.8.8。检查发现DNS配置不当,修改/etc/resolv.conf后问题解决。


5 总结

Ubuntu无法上网但能ping通网络的问题可能由多种原因引起,包括DNS配置不当、网络服务故障、防火墙设置等。通过逐步排查和尝试上述解决方案,大多数问题都可以得到有效解决。希望本文能帮助你顺利解决网络问题,享受顺畅的网络体验。

如果你在尝试上述方法后仍无法解决问题,建议查阅更多相关资料或寻求专业帮助。网络故障排除是一个复杂的过程,耐心和细致是成功的关键。