How to Add Dns Entry in Linux

How to Add Dns Entry in Linux

If you want to know how to add DNS entry in Linux, the quickest method is usually to add a hostname-to-IP mapping in /etc/hosts for a local override. If you want a permanent network-wide DNS change, you should update your resolver settings through NetworkManager or systemd-resolved instead. The right method depends on whether you are adding a one-off local entry or changing how Linux looks up DNS names system-wide.

What adding a DNS entry in Linux usually means

What adding a DNS entry in Linux usually means

The phrase “add DNS entry” can mean two different things in Linux:

  • Add a local host entry, which tells your machine to resolve a name to an IP address before querying external DNS servers.
  • Change DNS resolver settings, which tells Linux which upstream DNS servers to use for all lookups.

If you only need one computer to resolve a name, /etc/hosts is usually the simplest option. If you want Linux to use a new DNS server, you need to change the network configuration instead.

Fast answer: add a local entry with /etc/hosts

Fast answer add a local entry with etc hosts

For most quick fixes, edit /etc/hosts and map a name to an IP address. This is the most direct way to add a DNS entry in Linux for one machine.

  1. Open the hosts file as root.
  2. Add a line with the IP address and hostname.
  3. Save the file.
  4. Test the name with ping, getent, or nslookup.

Method 1: Edit /etc/hosts

This method is best when you want a local override for a server name, a lab environment, a test domain, or a temporary internal service.

Step 1: Back up the file

Before editing system files, make a backup so you can restore the original if needed.

sudo cp /etc/hosts /etc/hosts.bak

Step 2: Open the file with a text editor

Step 2 Open the file with a text editor

Use any editor you are comfortable with. For example:

sudo nano /etc/hosts

Step 3: Add the new mapping

Use this format:

IP-address hostname alias

Example:

192.168.1.50 appserver.local appserver

This tells Linux to resolve appserver.local and appserver to 192.168.1.50.

Step 4: Save and close the file

After saving, the change usually takes effect immediately on the local machine. You do not normally need to restart networking for /etc/hosts changes.

Step 5: Verify the entry

Check whether the system resolves the name correctly:

getent hosts appserver.local

You can also try:

ping -c 1 appserver.local

If the mapping is correct, Linux should return the IP address you added.

Method 2: Change DNS settings with NetworkManager

If your goal is to change which DNS servers Linux uses, NetworkManager is the right tool on many desktop and server systems. This is common on Ubuntu, Fedora, RHEL, CentOS, and other distributions that use NetworkManager.

Find the active connection

List your saved or active connections:

nmcli connection show

Set DNS servers for a connection

Replace CONNECTION_NAME with your actual connection name and use one or more DNS server IP addresses:

sudo nmcli connection modify "CONNECTION_NAME" ipv4.dns "8.8.8.8 1.1.1.1"

If you also want to ignore DNS servers supplied by DHCP, set manual DNS handling for that connection:

sudo nmcli connection modify "CONNECTION_NAME" ipv4.ignore-auto-dns yes

Bring the connection back up

Apply the changes by reconnecting the profile:

sudo nmcli connection down "CONNECTION_NAME" && sudo nmcli connection up "CONNECTION_NAME"

Confirm the change

Check the applied DNS servers:

nmcli device show | grep IP4.DNS

Or inspect the connection itself:

nmcli connection show "CONNECTION_NAME" | grep ipv4.dns

Method 3: Update resolver settings for systemd-resolved

Many modern Linux distributions use systemd-resolved for DNS handling. In that case, you can manage DNS either through the network connection or directly through resolvectl, depending on your setup.

Check whether systemd-resolved is active

resolvectl status

If the command returns resolver information, your system is likely using systemd-resolved.

Set DNS servers for an interface

Replace eth0 with your actual interface name and use the DNS servers you want:

sudo resolvectl dns eth0 8.8.8.8 1.1.1.1

You can also set a search domain if needed:

sudo resolvectl domain eth0 example.com

Flush or reset cached lookups if needed

If old results still appear, restart the resolver service or clear cached data if your distribution supports it:

sudo systemctl restart systemd-resolved

After that, test resolution again.

Which method should you use?

Use the table below to choose the right approach.

GoalBest methodNotes
Map one hostname to one IP on your computer/etc/hostsFastest option, local only
Change the DNS servers your system usesNetworkManager or systemd-resolvedApplies to network lookups, not just one host
Make a temporary test override/etc/hostsEasy to edit and revert
Set DNS for a specific network profileNetworkManagerUseful for laptops and changing networks

How to verify the new entry

After making changes, confirm that Linux is resolving the name the way you expect. Useful checks include:

  • getent hosts name, which shows what the system resolver returns
  • ping -c 1 name, which confirms basic name resolution
  • nslookup name or dig name, which query DNS directly
  • resolvectl query name, on systems using systemd-resolved

If getent shows the correct result but dig does not, that usually means the entry is local in /etc/hosts and not published in external DNS. That is expected.

Common issues and fixes

The change does not seem to work

Check whether another service is overriding your setting. On many systems, NetworkManager, DHCP, or systemd-resolved can replace manual DNS settings. Verify which component is managing the connection before editing files.

You edited /etc/resolv.conf and it reverted

This is common. On many Linux systems, /etc/resolv.conf is generated automatically, so manual edits may be overwritten. If that file is managed by NetworkManager or systemd-resolved, change DNS through the owning service instead of editing the file directly.

The hostname still resolves to the old address

This can happen because of caching. Restart systemd-resolved if it is in use, reconnect the network profile, or wait briefly and test again. Also confirm that your entry has the correct syntax in /etc/hosts.

The entry works as root but not as a normal user

The entry works as root but not as a normal user

That usually points to a resolver or caching issue, not a permissions problem. Use getent hosts and check nsswitch.conf only if you need to confirm the resolver order.

The file permissions look wrong

System files like /etc/hosts and /etc/resolv.conf should be edited carefully as root. If permissions were changed accidentally, restore the default ownership and permissions for your distribution.

When to use /etc/hosts vs DNS

A simple rule helps avoid confusion:

  • Use /etc/hosts when you need a local, quick hostname override.
  • Use DNS settings when you want Linux to ask a specific DNS server for names.
  • Use NetworkManager when the change should follow a connection profile.
  • Use systemd-resolved tools when your system relies on that resolver layer.

If your goal is to make one machine point a name to a different IP, /etc/hosts is usually the right answer. If you are trying to fix domain lookups across your network, change the DNS servers instead.

Rollback steps

If you need to undo your change, reverse the method you used:

  • Remove the line you added in /etc/hosts, then save the file.
  • Restore the backup copy if needed: sudo cp /etc/hosts.bak /etc/hosts
  • Reset DNS values in NetworkManager with the original settings.
  • Reconnect the interface or restart the resolver service after changes.

Frequently Asked Questions About how to add dns entry in linux

How do I add a DNS entry in Linux quickly?

Edit /etc/hosts and add an IP-to-hostname mapping. This is the fastest way to add a local entry.

How do I make a hostname entry permanent in Linux?

Entries in /etc/hosts are persistent until you remove them. If you need a permanent DNS server change, configure it through NetworkManager or your system resolver.

Should I edit /etc/hosts or /etc/resolv.conf?

Use /etc/hosts for local hostname overrides. Use resolver or network settings for DNS servers. Avoid manually editing /etc/resolv.conf unless you know it is not managed automatically.

How do I verify that Linux is using the new DNS entry?

Run getent hosts name or resolvectl query name. These commands show what the system resolver returns.

How do I flush DNS cache in Linux after a change?

If your system uses systemd-resolved, restart it with sudo systemctl restart systemd-resolved. You may also need to reconnect the network profile if NetworkManager is managing DNS.

Why does the entry work in ping but not in dig?

ping often uses the system resolver, which can include /etc/hosts. dig queries DNS directly, so it will not show local host file entries.

Conclusion

To add a DNS entry in Linux, first decide whether you need a local hostname override or a real DNS configuration change. For a single machine or test setup, /etc/hosts is the easiest solution. For a broader DNS change, use NetworkManager or systemd-resolved so the setting persists and survives network reconnects. After any change, verify the result with getent, ping, or resolvectl to make sure name resolution behaves as expected.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *