ubuntu修改dns
目录
[TOC]
一、查看当前DNS
resolvectl status
在ubuntu22.04以下的系统中,应该使用systemd-resolve --status
root@111:~# resolvectl
Global
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
Current DNS Server: 192.168.2.1
DNS Servers 192.168.2.1 fe80::5
Link 2 (enp6s0)
Current Scopes: DNS
Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 192.168.2.1
DNS Servers: 192.168.2.1 fe80::5
注意到需要修改两个地方的DNS,一个是Global
,一个是Link 2 (enp6s0)
二、修改Global DNS
1.打开配置文件
vim /etc/systemd/resolved.conf
2.在 [Resolve]
部分添加或修改以下行:
DNS=223.6.6.6 1.1.1.1 119.29.29.29 2400:3200:baba::1 2606:4700:4700::1111 2402:4e00::
3.重启服务
sudo systemctl restart systemd-resolved
4.再次查看当前DNS
resolvectl status
root@111:~# resolvectl
Global
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
Current DNS Server: 223.6.6.6
DNS Servers 223.6.6.6 1.1.1.1 119.29.29.29 2400:3200:baba::1 2606:4700:4700::1111 2402:4e00::
Link 2 (enp6s0)
Current Scopes: DNS
Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 192.168.2.1
DNS Servers: 192.168.2.1 fe80::5
root@111:~#
三、修改特定网卡的的DNS(这里以enp6s0为例)
1.进入netplan目录并查看文件
cd /etc/netplan
ls -lah
# root @ ubuntu in /etc/netplan [11:41:20]
$ cd /etc/netplan
ls -lah
总计 32K
drwxr-xr-x 2 root root 4.0K 2月 14 01:59 .
drwxr-xr-x 154 root root 12K 2月 14 02:29 ..
-rw------- 1 root root 49 7月 10 2024 01-network-manager-all.yaml.dpkg-backup
-rw------- 1 root root 287 2月 14 01:59 50-cloud-init.yaml
-rw------- 1 root root 771 2月 14 02:01 90-NM-065414f7-ecf2-42cc-be4e-a2a963949b3a.yaml
-rw------- 1 root root 540 2月 14 01:59 90-NM-6eb17bc8-a642-3fb0-8edf-601ed5a4c021.yaml
# root @ ubuntu in /etc/netplan [11:41:47]
2.根据网卡名找到对应的文件(比如enp6s0)
# root @ ubuntu in /etc/netplan [11:45:38] C:130
$ grep -rn 'enp6s0'
90-NM-6eb17bc8-a642-3fb0-8edf-601ed5a4c021.yaml:7: name: "enp6s0"
3.vim编辑找到的文件
vim 90-NM-6eb17bc8-a642-3fb0-8edf-601ed5a4c021.yaml
可以看到文件内容
network:
version: 2
ethernets:
NM-6eb17bc8-a642-3fb0-8edf-601ed5a4c021:
renderer: NetworkManager
match:
name: "enp6s0"
dhcp4: true
dhcp6: true
macaddress: "preserve"
wakeonlan: true
networkmanager:
uuid: "6eb17bc8-a642-3fb0-8edf-601ed5a4c021"
name: "ubu"
passthrough:
connection.autoconnect-priority: "-999"
connection.timestamp: "1720592091"
ipv6.addr-gen-mode: "default"
ipv6.ip6-privacy: "-1"
proxy._: ""
缺少nameservers
需要手动添加,nameservers
和networkmanager
同级
network:
version: 2
ethernets:
NM-6eb17bc8-a642-3fb0-8edf-601ed5a4c021:
renderer: NetworkManager
match:
name: "enp6s0"
dhcp4: true
dhcp6: true
macaddress: "preserve"
wakeonlan: true
networkmanager:
uuid: "6eb17bc8-a642-3fb0-8edf-601ed5a4c021"
name: "ubu"
passthrough:
connection.autoconnect-priority: "-999"
connection.timestamp: "1720592091"
ipv6.addr-gen-mode: "default"
ipv6.ip6-privacy: "-1"
proxy._: ""
nameservers:
addresses:
- 223.6.6.6
- 1.1.1.1
- 119.29.29.29
- 2400:3200:baba:0:0:0:0:1
- 2606:4700:4700:0:0:0:0:1111
- 2402:4e00:0:0:0:0:0:0
4.应用配置
netplan apply
此时ssh连接会断开,等30秒后重新连接ssh查看dns
5.查看修改后的dns
root@111:~# resolvectl
Global
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
Current DNS Server: 223.6.6.6
DNS Servers 223.6.6.6 1.1.1.1 119.29.29.29 2400:3200:baba::1 2606:4700:4700::1111 2402:4e00::
Link 2 (enp6s0)
Current Scopes: DNS
Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 223.6.6.6
DNS Servers: 223.6.6.6 1.1.1.1 119.29.29.29 192.168.2.1 2400:3200:baba::1 2606:4700:4700::1111 2402:4e00::
fe80::5
root@111:~#
此时系统优先使用的DNS已经是手动设置的了,但有时候还可以看到后面还是有几个DNS(有可能是路由器ip
或者光猫ip
或者运营商默认dns ip
),这是DHCP自动配置的DNS,完全禁用需要修改其他配置