How to Set DNS Server in Ubuntu Command Line?


How to Set DNS Server in Ubuntu Command Line?

Setting up DNS servers in Ubuntu via the command line can be a straightforward and efficient process. Whether you're configuring DNS for network optimization, privacy, or troubleshooting connectivity issues, this guide will walk you through the steps to set DNS servers using the command line interface.

Checking Current DNS Configuration:

Before making any changes, it's essential to verify your current DNS configuration. Open a terminal and use the following command:

cat /etc/resolv.conf

This will display the current DNS settings, including the nameservers and search domains.

Determining the Network Interface:

Identify the network interface you want to configure. Use the following command to list all network interfaces:

ip link show

Typically, you'll see interfaces like "eth0" or "wlan0". Choose the interface you're connected to.

Changing DNS Servers:

Now, let's change the DNS servers for your chosen network interface. Replace "interface_name" with your actual interface name and "DNS_server_IP" with the desired DNS server address.

sudo nano /etc/network/interfaces

Add the following lines at the end of the file:

dns-nameservers DNS_server_IP

Save and exit the editor.

Applying Changes:

To apply the changes without restarting the network service, use the following command:

sudo systemctl restart networking

Verifying Changes:

Confirm that the changes were successful by checking the updated configuration:

cat /etc/resolv.conf

You should now see the new DNS server listed.

Alternative Method - Using Netplan:

If you're using Ubuntu 18.04 or later, Netplan might be the preferred method. Open the Netplan configuration file for editing:

sudo nano /etc/netplan/01-netcfg.yaml

Add the DNS servers under the appropriate network interface:

network:
version: 2
renderer: networkd
ethernets:
interface_name:
nameservers:
addresses: [DNS_server_IP]

Apply the changes:

sudo netplan apply

Testing DNS Resolution:

Verify that DNS resolution is working as expected by using the following command:

nslookup example.com

Replace "example.com" with the domain you want to test.

Additional Considerations:

  • Backup: Before making changes, consider creating a backup of configuration files to avoid potential issues.

  • Multiple DNS Servers: You can specify multiple DNS servers by separating their IP addresses with commas.

Related Searches and Questions asked:

  • How to Find DNS on Linux Command?
  • How to Set DNS Server in Linux Ubuntu?
  • How to Configure Primary and Secondary DNS Server in Linux?
  • How to Create DNS Server in CentOS?
  • That's it for this topic, Hope this article is useful. Thanks for Visiting us.