在 Linux 里,ifconfig 本身不能配置 DNS。ifconfig 只负责网络接口(IP、掩码、广播等),DNS 是通过 /etc/resolv.conf 或其他网络管理工具来配置的。
下面分几种常见情况说明:
ifconfig + 手动配 DNS(最原始方式)ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
vi /etc/resolv.conf
添加:
nameserver 8.8.8.8
nameserver 114.114.114.114
✅ 适合:临时测试、无 NetworkManager 的服务器
⚠️ 注意:
resolv.conf 会被网络服务覆盖resolvconf(Debian/Ubuntu 常见)编辑:
/etc/resolvconf/resolv.conf.d/head
写入:
nameserver 8.8.8.8
然后执行:
resolvconf -u
nmcli device
nmcli con mod eth0 ipv4.dns "8.8.8.8 114.114.114.114"
nmcli con up eth0
✅ 推荐方式(现代 Linux)
/etc/network/interfaces(Debian 系)vi /etc/network/interfaces
示例:
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 114.114.114.114
重启网络:
systemctl restart networking
vi /etc/sysconfig/network-scripts/ifcfg-eth0
添加:
DNS1=8.8.8.8
DNS2=114.114.114.114
重启:
systemctl restart network
ifconfig 不配 DNS,DNS 在
/etc/resolv.conf或网络管理工具里配
如果你告诉我:
我可以给你最准确的一条命令。