前言
我的虚拟机网络设置为NAT模式,在终端输入ifconfig只显示127.0.0.1,不能连上外网。
xxx@xxx:~$ ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 226 bytes 16851 (16.8 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 226 bytes 16851 (16.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
<
解决步骤:
1.先切换为root用户:sudo -i
xxx@xxx:~$ sudo -i
[sudo] password for book:
root@xxx:~#
2.输入:dhclient -v
xxx@xxx:~# dhclient -v
Internet Systems Consortium DHCP Client 4.3.5
Copyright 2004-2016 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/ens33/00:0c:29:fc:48:55
Sending on LPF/ens33/00:0c:29:fc:48:55
Sending on Socket/fallback
DHCPREQUEST of 192.168.110.131 on ens33 to 255.255.255.255 port 67 (xid=0x70ddedb7)
DHCPACK of 192.168.110.131 from 192.168.110.254
bound to 192.168.110.131 -- renewal in 759 seconds.
3.再次:ifconfig
xxx@xxx:~# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.110.131 netmask 255.255.255.0 broadcast 192.168.110.255
inet6 fe80::20c:29ff:fefc:4855 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fc:48:55 txqueuelen 1000 (Ethernet)
RX packets 34 bytes 2699 (2.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 62 bytes 8284 (8.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 1772 bytes 127502 (127.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1772 bytes 127502 (127.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
再次ifconfig后可以看到多了一个IP地址
4.输入:exit 返回登陆者用户
root@xxx:~# exit
logout
xxx@xxx:~$
相关问题解答:
该方法在重启虚拟机后,依旧会存在以上问题。
我们可以将该命令加入到~/.bashrc中,保存后执行source ~/.bashrc使其立即生效。
本文详细描述了在虚拟机NAT模式下,通过切换到root用户并执行dhclient-v获取动态IP的过程,解决了ifconfig仅显示127.0.0.1的问题,最后提供了解决方案的永久化方法。
1968





