ubuntu如何通过命令修改默认dns
系统与软件
1
评论
1
发布者
1
浏览
-
在 Ubuntu 系统中,你可以通过命令行修改默认 DNS 服务器。具体方法取决于你的 Ubuntu 版本和所使用的网络管理工具。
方法 1:通过
resolvconf
工具(适用于大多数系统)-
首先安装 resolvconf(如果未安装):
sudo apt update && sudo apt install resolvconf
-
启动并启用服务:
sudo systemctl start resolvconf sudo systemctl enable resolvconf
-
编辑配置文件:
sudo nano /etc/resolvconf/resolv.conf.d/head
-
添加 DNS 服务器地址:
nameserver 8.8.8.8 # Google DNS nameserver 8.8.4.4 # Google DNS 备用
你也可以使用其他 DNS,如 Cloudflare 的
1.1.1.1
或 Quad9 的9.9.9.9
-
保存文件并重启服务:
sudo systemctl restart resolvconf
方法 2:直接编辑 resolv.conf(临时生效)
如果只是临时修改 DNS,可以直接编辑
/etc/resolv.conf
:sudo nano /etc/resolv.conf
添加 DNS 服务器:
nameserver 8.8.8.8 nameserver 8.8.4.4
注意:这种方法在系统重启或网络服务重启后可能会失效,因为该文件可能被网络管理工具自动覆盖。
方法 3:使用 netplan(适用于 Ubuntu 18.04 及以上版本)
-
找到 netplan 配置文件(通常在
/etc/netplan/
目录下):ls /etc/netplan/
-
编辑配置文件(文件名可能不同):
sudo nano /etc/netplan/01-network-manager-all.yaml
-
在配置中添加 DNS 服务器:
network: version: 2 renderer: NetworkManager ethernets: eth0: # 替换为你的网卡名称 dhcp4: yes nameservers: addresses: [8.8.8.8, 8.8.4.4]
-
应用配置:
sudo netplan apply
修改完成后,可以通过以下命令验证 DNS 设置:
cat /etc/resolv.conf
-