How to Change DNS of network from Terminal?

23,537

nmcli connection show --active to obtain active connection name
nmcli connection edit double tab to list available connections and chose appropriate

   nmcli> remove ipv4.dns  
   nmcli> set ipv4.ignore-auto-dns yes
   nmcli> set ipv4.dns 8.8.8.8 8.8.4.4 (or other dns servers)      
   nmcli> save
   nmcli> quit 

nmcli connection down your_connection_name
nmcli connection up your_connection_name

Share:
23,537

Related videos on Youtube

user246185
Author by

user246185

Updated on September 18, 2022

Comments

  • user246185
    user246185 over 1 year

    I know how to change the DNS of my network from GUI Network Manager:

    1. Right click on the network manager icon in the panel and choose "Edit connections..."
    2. Select your connection from the wired or wireless tab, choose "Edit"
    3. Choose IPv4 settings tab
    4. Switch method to "Automatic (DHCP) addresses only"
    5. Enter the DNS you want in the box "Additional DNS servers" and press "Apply"

    How do I do exactly the same thing from Terminal?

    • Pablo Bianchi
      Pablo Bianchi over 4 years
      The (current) TUI Network Manager alternative is called nmtui.
  • Thomas
    Thomas over 8 years
    I think you also have to set ipv4.ignore-auto-dns: yes
  • Carlos Verdes
    Carlos Verdes almost 6 years
    Careful with nmcli connection down, if you are on a remote ssh session you can lose connection to the node!! I think is better to call /etc/init.d/network restart
  • Admin
    Admin over 5 years
    There's also nmcli connection modify "Wired connection 1" ipv4.dns "8.8.8.8" and the default 192.168.1.1 that most people see cant be removed because I'm pretty sure It's seen by ip route show
  • Pablo Bianchi
    Pablo Bianchi over 4 years
    If you care about speed and privacy you might prefer using 1.1.1.1, 1.0.0.1 (Cloudflare) instead of Google's one.
  • Dan
    Dan over 3 years
    /etc/init.d/network restart did not work. better yet, just do both commands with && between so it will come right back w/o losing connection. nmcli connection down && nmcli connection up
  • anscmc
    anscmc over 3 years
    nmcli connection up <your_connection_name> by itself (no down) worked for me in Raspbian 10.