虚拟机重启后无法联网ifconfig后只出现127.0.0.1

本文详细描述了在虚拟机NAT模式下,通过切换到root用户并执行dhclient-v获取动态IP的过程,解决了ifconfig仅显示127.0.0.1的问题,最后提供了解决方案的永久化方法。

前言

我的虚拟机网络设置为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使其立即生效。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香蕉牛奶糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值