Skip to content
Three Simple Ways to Configure DNS on a Linux Machine
Operations 3 min read

Three Simple Ways to Configure DNS on a Linux Machine

This guide explains three practical methods for setting up DNS resolution on Linux—editing the local hosts file, modifying the system's resolv.conf, and configuring network interface files—along with the precedence order and necessary service restarts.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations

This article assumes you have already set up a DNS server and shows three ways to configure DNS resolution on a Linux machine.

IP addresses identify sites on a network; domain names are used for easier memorization, and DNS (Domain Name System) converts domain names to IP addresses.

There are three methods to configure DNS on Linux, after which automatic resolution works.

Local hosts

Edit the local /etc/hosts file (or C:\WINDOWS\system32\drivers\etc\hosts on Windows) to map domain names to IP addresses.

$ vim /etc/hosts
123.123.123.123 www.baidu.com
124.123.123.123 www.qq.com

System default DNS configuration

Modify the /etc/resolv.conf file and add multiple nameserver entries, for example:

$ vim /etc/resolv.conf
nameserver 114.114.114.114
nameserver 123.123.123.123

Network interface configuration file

Assuming the network interface is eth0, edit /etc/sysconfig/network-scripts/ifcfg-eth0 to add DNS rules:

$ vim /etc/sysconfig/network-scripts/ifcfg-eth0
# add rules, e.g.:
DNS1=114.114.114.114
DNS2=xxx.xxx.xxx.xxx
DNS3=xxx.xxx.xxx.xxx

Restart the network service: $ service network restart After configuring DNS in /etc/sysconfig/network-scripts/ifcfg-eth0 and restarting the service, the same DNS entries will be automatically generated in /etc/resolv.conf.

System resolution priority

The resolution order is: Local hosts > Network interface configuration > System default DNS configuration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DNSsystem administrationnetwork configurationhosts file
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.