如何使用 nmcli 为设备设置 DNS 服务器?

如何使用 nmcli 为设备设置 DNS 服务器?

如何设置 DNS服务器8.8.8.88.8.4.4通过 CLI使用nmcli

$ nmcli device show eno1
GENERAL.DEVICE:                         eno1
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         C8:9C:DC:28:86:CA
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     Wired connection 1
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.1.6/24
IP4.GATEWAY:                            192.168.1.1
IP4.ROUTE[1]:                           dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000
IP4.DNS[1]:                             192.168.1.1
IP6.ADDRESS[1]:                         fe80::49c7:61b4:886f:27e9/64
IP6.GATEWAY:                            --

使用Wired Connection 1GUI 配置的 使用以下 DNS 设置:

$ nmcli con show
NAME                UUID                                  TYPE             DEVICE  
Wired connection 1  f5687342-d1bd-3e5e-83a1-a1e2ec627d3c  802-3-ethernet   eno1    
docker0             84b0d93b-f4f7-4b52-a6cb-800d161f0954  bridge           docker0 
virbr0              024c6c7a-12f4-49ad-92b3-434106caad25  bridge           virbr0  
vici                c19aafd8-fa10-4577-a748-dee4cb684675  802-11-wireless  --      

详细地:

$ nmcli con show Wired_connection_1 | grep ipv4.dns
ipv4.dns:                               8.8.8.8,8.8.4.4
ipv4.dns-search:                        --
ipv4.dns-options:                       (default)
ipv4.dns-priority:                      0

但我想使用该配置nmcli应用eno1

有点离题的是,看起来问题使用 Ubuntu 的 DNS 配置,也许漏洞. (我受到了这个错误的影响,因为它显示在resolve.conf

也许

sudo nmcli dev set enp8s0 managed yes

或类似...

答案1

要添加自定义 DNS 服务器,请使用

nmcli connection modify Wired_connection_1 ipv4.dns "192.168.1.1,8.8.8.8,8.8.4.4" 

检查配置是否正确使用

nmcli connection show Wired_connection_1 | grep "dns"

ipv4.dns:                               192.168.1.1,8.8.8.8,8.8.4.4
ipv4.dns-search:                        --
ipv4.dns-options:                       (default)
ipv4.dns-priority:                      0
ipv4.ignore-auto-dns:                   no
ipv6.dns:                               --
ipv6.dns-search:                        --
ipv6.dns-options:                       (default)
ipv6.dns-priority:                      0
ipv6.ignore-auto-dns:                   no

我偶然发现了同样的问题,如果我删除它,192.168.1.1那么由于某些内部 vmware 网络桥接它就无法工作,但要设置附加 dns 服务器,您可以使用上述方法。

答案2

nmcli connection show

# network_uuid    : 2nd element
# net_device_uuid : 4th element

# modify the dns
# > the dns strings
onedotipv4="1.1.1.3 1.0.0.3"
onedotipv6="2606:4700:4700::1113 2606:4700:4700::1003"
# > modify parameters
nmcli con mod ${network_uuid} ipv4.dns '$onedotipv4' ipv6.dns '$onedotipv6' ipv4.ignore-auto-dns yes ipv6.ignore-auto-dns yes connection.dns-over-tls 1

# apply changes
nmcli dev reapply ${net_device_uuid}

# verify it works
nmcli con show ${network_uuid} | grep ipv | grep .dns  && nslookup google.com

要自动化所有操作,您可以使用以下别名(放入 .bashrc / .zshrc )

NETWORK_NAME="<REPLACE ME>"
network_uuid="$(nmcli connection show | grep $NETWORK_NAME | awk '{print $2}')"
net_device_uuid="$(nmcli connection show | grep $NETWORK_NAME | awk '{print $4}')"
onedotipv4="1.1.1.3 1.0.0.3"
onedotipv6="2606:4700:4700::1113 2606:4700:4700::1003"

alias onedotdns="nmcli con show ${network_uuid} | grep ipv | grep .dns  && nmcli con mod ${network_uuid} ipv4.dns '$onedotipv4' ipv6.dns '$onedotipv6' ipv4.ignore-auto-dns yes ipv6.ignore-auto-dns yes && nmcli connection modify ${network_uuid} connection.dns-over-tls 1 && nmcli general reload dns-full && nmcli dev reapply ${net_device_uuid} && nmcli con show ${network_uuid} | grep ipv | grep .dns  && nslookup google.com "

alias defaultdns="nmcli con show ${network_uuid} | grep ipv | grep .dns  && nmcli con mod ${network_uuid} ipv4.dns '' ipv4.ignore-auto-dns no ipv6.dns '' ipv6.ignore-auto-dns no connection.dns-over-tls 0 && nmcli general reload dns-full && nmcli dev reapply ${net_device_uuid} && nmcli con show ${network_uuid} | grep ipv | grep .dns && nslookup google.com "
alias dnstest="nmcli con show ${network_uuid} | grep ipv | grep .dns && nslookup google.com "

相关内容