Fixing DNS Issues when Moving a Headless Ubuntu 22.04.5 LTS Server to a New Network
When moving a headless Ubuntu 22.04.5 LTS server to a new network, you might encounter issues connecting to the internet, such as being unable to access websites or perform software updates. One common issue is related to Domain Name System (DNS) configuration. This article will guide you through the key concepts of DNS and provide detailed instructions for fixing DNS-related issues when moving your headless Ubuntu server to a new network.
Understanding DNS
DNS is a system that translates human-readable domain names, like example.com, into IP addresses, like 192.0.2.1. DNS servers maintain a database of domain names and their corresponding IP addresses. When you enter a domain name into your web browser, your computer queries a DNS server to get the IP address associated with that domain name. DNS makes it easier for users to access websites and other online resources without having to memorize numerical IP addresses.
Checking Current DNS Settings
Before you start troubleshooting, it's essential to check the current DNS settings of your headless Ubuntu server. You can do this by running the following command:
sudo cat /etc/resolv.conf
This command will display the DNS servers configured for your server. If you see IP addresses from your old network, it indicates that you need to update your DNS settings.
Updating DNS Settings
To update DNS settings, you need to edit the /etc/netplan/config_file.yaml file, where config_file depends on your network interface name. For example, if you are using the eth0 network interface, you would edit the file named /etc/netplan/01-netcfg.yaml.
Use your preferred text editor, like nano or vi, to edit the file:
sudo nano /etc/netplan/config_file.yaml
Find the section that looks like this:
nameservers:
addresses: [old_dns_server_1, old_dns_server_2]
Replace the old_dns_server_1 and old_dns_server_2 with the IP addresses of the DNS servers from your new network.
After updating the DNS settings, apply the changes by running:
sudo netplan apply
Now, check the DNS settings again:
sudo cat /etc/resolv.conf
If the new DNS servers' IP addresses are displayed, you have successfully updated your DNS settings.
Flushing DNS Cache
Sometimes, DNS issues persist even after updating the DNS settings because your system might still be using stored DNS records from the cache. You need to flush the DNS cache to ensure your system uses the updated DNS settings. Run the following command:
sudo systemd-resolve --flush-caches
- DNS translates domain names into IP addresses, making it easier for users to access online resources.
- Check the current DNS settings by running
sudo cat /etc/resolv.conf. - Update DNS settings by editing the
/etc/netplan/config_file.yamlfile and replacing old DNS servers with new ones. - Apply the changes by running
sudo netplan apply. - Flush the DNS cache by running
sudo systemd-resolve --flush-caches.