Dynamic IP with static DNS

15,807

Solution 1

To answer part 2 of your question, you can edit /etc/network/interfaces, and add the following line:

dns-nameservers 8.8.8.8 4.2.2.2

So that your interfaces file looks something like this:

auto <interfacename>
iface <interfacename> inet dhcp
dns-nameservers 8.8.8.8 4.2.2.2

Make sure to replace < interfacename > with wlan0 or whatever your wifi interface is called.

Solution 2

To set a dns server:

Edit /etc/dhcp/dhclient.conf ; uncomment line supersede domain-name-servers ( or add this line, if you don't have it ) and list your desired dns servers separated by coma, end with semicolon. It is line 20 of the /etc/dhcp/dhclient.conf. For example, mine looks like this:

supersede domain-name-servers 208.67.222.222,208.67.220.220,8.8.8.8;

With this option set , I get IP from no matter what access point I connect to, but DNS is same everywhere.

15.04 has very interesting feature that you can modify connection with nmcli. Doing nmcli connection modify id "My Connection" +ipv4.dns "" +ipv4.dns 208.67.220.220 +ipv4.ignore-auto-dns yes will be equivalent to setting "Automatic(DHCP) Address Only" and adding your own dns server in GUI.

For multiple connections you can run this script:

#!/bin/bash
set -x
for file in /etc/NetworkManager/system-connections/*; do
    file=$(echo $file | cut -d'/' -f5-)
    nmcli connection modify id "$file" +ipv4.dns "" +ipv4.dns 208.67.220.220 +ipv4.ignore-auto-dns yes
done

Also , 15.04 has nmtui command line tool, which allows you to edit information for connections that you have saved on your machine. Open nmtui in terminal, go to Edit Connection, select one from the list, and by hitting TAB switch to side panel to click <Edit . . .> option. There you will be able to see information about particular settings for your connection and under IPv4 CONFIGURATION -> you can set DNS servers. Scroll down to the bottom of the page, hit . The big disadvantage IMHO of this is that you have to set DNS for every single one of connections and it does not seem to have "get ip but let me use my own dns" type of option, which is present in GUI app.

To find out another way of setting DNS, which does the job for all network connections, see my answer to another question. I wrote a little script that you can do to update DNS to whatever you want after connecting to an access point at least one time.

To see what DNS server you are using

14.04 and earlier versions have nm-toool command which will show extended information about your connection. To see only DNS use nm-tool | grep -i dns or nm-tool | awk '/DNS/' . You can also do nmcli dev list | awk '/DNS/' or nmcli dev list iface wlan0 | awk '/DNS/' for specific interface

15.04 does not have nm-tool, however nmcli still works with different syntax. nmcli dev show will show extended information about interfaces, nmcli dev show wlan0 will show only about wireless interface, and nmcli dev show | awk '/DNS/' ornmcli dev show | grep DNS` will show specifically what DNS you are currently using

Regardless of the release you should be able to cat /run/resolvconf/resolv.conf to see nameservers for your active, current connection.

Share:
15,807
Luis Zuniga
Author by

Luis Zuniga

Updated on September 18, 2022

Comments

  • Luis Zuniga
    Luis Zuniga over 1 year

    Getting a dhcp'd IP ok, but DNS is not resolving.

    2 part question:

    1. using nmcli how can I see what DNS server's I am using?
    2. where do I go to configure a STATIC DNS server for a wireless interface that is getting it's IP through DHCP? (/etc/network/interfaces..?)

    note, I would prefer to do this via cli and not gui.

    Thanks!

    Luis

  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy almost 9 years
    Like my answer ? Leave upvote. Solved your problem ? Check the grey checkmark to accept the answer
  • Luis Zuniga
    Luis Zuniga almost 9 years
    nm-tool is no longer bundled with ubuntu
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy almost 9 years
    Luis , are you on 15.04 ?
  • Luis Zuniga
    Luis Zuniga almost 9 years
    Yes. I'm using 15.04
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy almost 9 years
    @LuisZuniga added some updates, review when you have time