虚拟机Ubuntu没有IP地址上不了网

环境

  • Ubuntu 22.04
  • VMware® Workstation 15 Pro

问题

之前一直可以正常上网,今天忽然上不了网了。查看IP地址,发现没有IP了。

➜  ~ ip addr
1: lo: ,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: ,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:0c:29:2b:b5:88 brd ff:ff:ff:ff:ff:ff
    altname enp2s1

分析

试试另外一台虚拟机,发现仍然可以正常上网,如下:

➜  dist ip addr 
1: lo: ,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: ,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:8d:4c:eb brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.1.80/24 brd 192.168.1.255 scope global dynamic noprefixroute ens33
       valid_lft 86359sec preferred_lft 86359sec
    inet6 2408:8207:18bc:8780:824c:c50c:537a:3c06/64 scope global temporary dynamic 
       valid_lft 259161sec preferred_lft 86071sec
    inet6 2408:8207:18bc:8780:1535:2c8c:bf9b:d057/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 259161sec preferred_lft 172761sec
    inet6 fe80::c3a4:e120:99f9:1043/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: ,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:d3:03:6e:06 brd ff:ff:ff:ff:ff:ff

对比二者,可见后者有IP地址 192.168.1.80

这两个虚拟机,使用的都是桥接模式:

虚拟机Ubuntu没有IP地址上不了网_第1张图片
网上查了一下,真是众说纷纭,因为这个问题确实影响因素有很多。不过对于我来说,既然另一个虚拟机能正常上网,配置也一样都是桥接模式,那问题还是出在虚拟机自身了。

查看 NetworkManager

➜  ~ service NetworkManager status
● NetworkManager.service - Network Manager
     Loaded: loaded (/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-07-03 11:03:49 CST; 1min 9s ago
       Docs: man:NetworkManager(8)
   Main PID: 920 (NetworkManager)
      Tasks: 3 (limit: 4588)
     Memory: 10.3M
        CPU: 282ms
     CGroup: /system.slice/NetworkManager.service
             └─920 /usr/sbin/NetworkManager --no-daemon

7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5278] ifupdown: interfaces file /etc/network/interfaces doesn't exist
7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5298] dhcp-init: Using DHCP client 'internal'
7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5299] device (lo): carrier: link connected
7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5302] manager: (lo): new Generic device (/org/freedesktop/NetworkManager/Devices/1)
7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5312] manager: (ens33): new Ethernet device (/org/freedesktop/NetworkManager/Devices/2)
7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5327] failed to open /run/network/ifstate
7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5343] manager: startup complete
7月 03 11:03:52 ding-ubuntu NetworkManager[920]:   [1656817432.5555] modem-manager: ModemManager available
7月 03 11:03:54 ding-ubuntu NetworkManager[920]:   [1656817434.1480] agent-manager: agent[542d08cd03b54fa2,:1.41/org.gnome.Shell.NetworkAgent/127]: agent registered
7月 03 11:04:27 ding-ubuntu NetworkManager[920]:   [1656817467.5163] agent-manager: agent[9cb6da91b91ecc95,:1.84/org.gnome.Shell.NetworkAgent/1000]: agent registered

在网上搜了半天,试了半天,最后这个办法解决了我的问题:

dhclient ens33

注:这里的 ens33 ,是因为 ip addr 结果里是 ens33

运行命令后没有任何输出,但是IP地址有了。

➜  ~ ip addr show  
1: lo: ,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: ,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:2b:b5:88 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.1.88/24 brd 192.168.1.255 scope global dynamic ens33
       valid_lft 86397sec preferred_lft 86397sec
    inet6 2408:8207:18bc:8780:20c:29ff:fe2b:b588/64 scope global dynamic mngtmpaddr 
       valid_lft 259197sec preferred_lft 172797sec
    inet6 fe80::20c:29ff:fe2b:b588/64 scope link 
       valid_lft forever preferred_lft forever

现在就能正常上网了。

参考

  • https://blog.csdn.net/yunfeiw0/article/details/122624599

你可能感兴趣的:(网络,Linux,ubuntu,tcp/ip,vm)