Linux dns network manager

How to manage DNS in NetworkManager via console (nmcli)?

I have CentOS 7.2 (guest in VirtualBox, vagrant box centos/7 , no GUI). I see there is a nameserver in file:

$ cat /etc/resolv.conf # Generated by NetworkManager nameserver 10.0.2.3 
$ vi /etc/sysconfig/network-scripts/ifcfg-eth0 PEERDS=no DNS1=8.8.4.4 DNS2=8.8.8.8 

And it works. But is there any way to do this through nmcli ? P.S. No nmtui installed (in a selected system).

4 Answers 4

Here is the command to modify an existing connection.

nmcli con mod $connectionName ipv4.dns "8.8.8.8 8.8.4.4" 

connectionName can be found by command: nmcli con . In the question case, it will be «System eth0»

If you want to ignore automatically configured nameservers and search domains, ie the settings passed from DHCP.

nmcli con mod $connectionName ipv4.ignore-auto-dns yes 

Finally, to enable the changes, bring the connection down then up:

nmcli con down $connectionName nmcli con up $connectionName 
service NetworkManager restart 

Verify with cat /etc/resolv.conf . You should not edit /etc/resolv.conf manually as it is generated by NetworkManager service, it is likely to get overridden at any given time.

I’d rather use nmtui or if possible the GUI version as well. There is no need to be a command line ninja, if you have a more convenient option which is doing the same thing.

there is good TUI tool developed by red hat named nmtui that you really should try. it is pre-installed on various distros, nowadays, but if it is not on yours, try:

 sudo yum install networkmanager-tui 

it uses a curses based text interface — accessible from the command line. nmcli is only especially necessary when writing scripts, and has larger room for error due to the larger variety of possible input.

I cannot use it until set up new dns, because there is no Internet connection. Anyway, thanks for this point.

In addition to setting the ipv4.dns property described above.

Читайте также:  Почему я выбрал линукс

To exclude the DHCP provided DNS servers. set the ipv4.ignore-auto-dns property to yes.

nmcli con mod ipv4.ignore-auto-dns yes 

To enable the changes, bring the connection down then up:

nmcli con down nmcli con up

Verify with cat /etc/resolv.conf

Just in case I have done a little script to do that automatically (here with google DNS) for every ethernet/wireless connections:

nmcli -g name,type connection show --active | awk -F: '/ethernet|wireless/ < print $1 >' | while read connection do nmcli con mod "$connection" ipv6.ignore-auto-dns yes nmcli con mod "$connection" ipv4.ignore-auto-dns yes nmcli con mod "$connection" ipv4.dns "8.8.8.8 8.8.4.4" nmcli con down "$connection" && nmcli con up "$connection" done 

At the end, the wireless connections will be lost. You have to reconnect and voilà !

Источник

How to set DNS name servers on Ubuntu Linux?

DNS nameservers (resolvers) provide a method to translate the domain name into the IP addresses. It is provided by the ISP (internet service providers) and is used by various other devices to do the DNS lookup for a requested domain.

We will show you in this tutorial how to set or configure the DNS nameserver using different methods on the Ubuntu system. All configurations have been performed on Ubuntu 20.04 system.

Two different approaches that can be used to set the DNS NameServer on Ubuntu 20.04 Linux system:

  • Set the DNS NameServer from Ubuntu Desktop using the Network Manager
  • Set the DNS NameServer from command-line using netplan

Method 1: Set the DNS NameServer from Ubuntu Desktop Using the Network Manager

Setting up the DNS NameServer from the Ubuntu desktop using the Network Manager is quite simple and requires no technical knowledge. Just simply follow the below-mentioned steps:

1. Open the system “Settings” window on your system.
2. If you are connected with Wi-Fi then, click on the “wifi” tab. Otherwise, click on the “Network” tab, if you have a “wired” connection.
3. The Settings window can also launch from the top-right menu. Click on the drop-down icon and select the “wired settings” option from the displaying list, which is also shown in the following screenshot:

4. Click on the cog icon that is displayed under the “wired” heading to access the Network Manager configurations.

Читайте также:  Kali linux virtualbox drag and drop

5. Now, go into the “IPV4” settings tab and select the IPV4 method where the default “Automatic (DHCP)” is selected.

Toggle the automatic button to disable this option and enter the DNS resolver IP address separated by a comma to set the DNS nameserver, which is shown in the following screenshot:

We have used the Google DNS server resolver IP address.

Method 2: Set the DNS NameServer Using Netplan with Network Manager and Static IP

In previous Ubuntu distributions, the DNS resolvers were easily configured using the ‘/etc/resolv.conf’ file, just editing the values and saving the file for use. The systemd-resolved service provides DNS name resolution to local applications or services and this can be simply configured with Netplan, which is the default network management tool on Ubuntu 20.04.

To set the DNS resolvers with the netplan, follow the below-mentioned steps:

1. Open the terminal by running the “ip a” command list with your Wi-Fi/ethernet details. Carefully, note your IP address, netmask, and default gateway from the following screenshot that displays on executing the IP command.

2. Open the netplan configuration file. All netplan configuration files are stored in the /etc/netplan directory. So, open the “/etc/netplan/01-network-manager-all.yaml” file. The file name may vary from one setup to another. Type the below-mentioned command to open this configuration file using the nano text editor.

In this file, you can configure the IP address, gateway, network interfaces, DNS nameservers, and so on.

The general syntax of this configuration file is given below, and it can be modified by a system administrator.

network:
version: 2
renderer: NetworkManager
ethernets:
[device-name]:
dhcp4: false
addresses: [ip-address/netmask]
gateway4: default-gateway
nameservers:
addresses: [DNS_nameserver-1, DNS_nameserver-2]

Here, the following lines of code need to change in this configuration file.

version: 2
renderer: NetworkManager
ethernets:
enp0s3:
dhcp4: false
addresses: [10.0.2.15/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]

The “192.168.1.1” is the default gateway and the IP address is “10.0.2.15”. Replace your own gateway, netmask, and IP in the above configuration file. More than two DNS nameservers can be defined in a configuration and these nameservers should be separated with a comma. In the above file, “8.8.8.8, 8.8.4.4” are the DNS resolvers for Google. Save the configuration file and exit from this file by pressing “Ctrl+x”.

Читайте также:  Linux build support il2cpp

3. Now, use the following command to apply changes on this file with netplan:

The above command will generate the configuration systemd resolved service.

4. To verify the DNS resolver configuration, use the following command:

The systemd-resolve -status command shows a bundle of information. This is the reason for using the “grep” command to filter the “DNS Servers” string from all details.

The following output shows on the terminal; it means that it verifies the DNS nameserver and is set now.

Set Custom DNS NameServer from Command-line Using Netplan with Networkd

To set the DNS Name Server using the netplan and network, copy the following code in the netplan configuration file:

Enter the below-given configurations in this file:

network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: true
nameservers:
addresses: [8.8.8.8, 8.8.4.4]

Type the command given below to apply changes with netplan:

Now, verify the Current DNS nameserver using the following command:

The following output prints on the terminal window:

To again the default DNS configurations or remove the netplan configurations, remove all data and paste the following code in the configuration file:

network:
version: 2
renderer: NetworkManager

Now, apply changes using the following command:

Check the current DNS Nameserver by using the following command:

From the following displayed output, you will notice the default DNS nameserver to be set on your system:

Conclusion:

We have discussed in this article, how to configure the DNS nameserver using the desktop interface and command line. We have also seen how to configure the netplan changes on the Ubuntu system to change the DNS nameserver. You can set the custom DNS nameserver by using the netplan and networkd services as well.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she’s working as a Freelancer & Technical writer. She’s a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.

Источник

Оцените статью
Adblock
detector