在 Linux 中 ifconfig 本身并不能让你“上网”,它只是一个查看和配置网络接口的工具。如果你发现“用了 ifconfig 还是不能上网”,通常原因不在 ifconfig,而在网络配置本身。下面按常见原因说明:
ifconfig 能做的:
它不会:
ifconfig
如果看到:
eth0: flags=...
inet addr: 没有
说明没 IP。
解决:
dhclient eth0
# 或
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
即使有 IP,没有网关也上不了外网。
查看:
route -n
如果没有:
0.0.0.0 192.168.1.1
设置:
route add default gw 192.168.1.1
测试:
ping 8.8.8.8 # 通
ping www.baidu.com # 不通
解决:
vi /etc/resolv.conf
添加:
nameserver 8.8.8.8
nameserver 114.114.114.114
ifconfig eth0
如果看到 DOWN:
ifconfig eth0 up
很多现代 Linux(CentOS 7+、Ubuntu)不推荐直接用 ifconfig 配网络。
推荐:
# Ubuntu / Debian
nmcli device status
nmcli connection up eth0
# CentOS
systemctl status NetworkManager
✅ 改成 NAT 或桥接
ip addr
ip route
ping 127.0.0.1
ping 网关
ping 8.8.8.8
ping 域名
ifconfig 不能上网,是因为它不负责“完整网络配置”
上网 = IP + 网关 + DNS + 网卡启用
如果你愿意,可以贴一下:
ifconfig 输出ping 结果我可以帮你精确判断是哪一步出问题。