5

How do I set the DNS server to be 8.8.8.8 and 8.8.4.4 through the CLI with 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:                            --

The Wired Connection 1, configured with the GUI, is using those DNS settings:

$ 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  --      

in detail:

$ 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

but I'd like to apply that configuration using nmcli to eno1.

Somewhat tangentially, there looks to be a problem with DNS configuration for Ubuntu, perhaps a bug. (I'm effected by this bug, insofar as it shows in resolve.conf)

perhaps:

sudo nmcli dev set enp8s0 managed yes

or similar...

muru
  • 197,895
  • 55
  • 485
  • 740
Thufir
  • 4,551

2 Answers2

7

To add custom dns servers use

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

to check if the config is ok use

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

I stumbled upon same issue if I remove the 192.168.1.1 then due to some internal vmware networking bridging it wasn't working, but to set the additional dns server, you can use the above method.

pomsky
  • 68,507
user806882
  • 71
  • 1
  • 2
  • I can't quite recall what I was doing, and won't be able to check the answer for some time. But sounds right. – Thufir Mar 16 '18 at 23:30
0
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

to automate everything you can use the following aliases (put in .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 "