While using Linux, you must have encountered the “Name or service not known” error message when pinging or connecting to a certain address. So what is the cause of this error and how to fix it, will be solved by AZDIGI in this article.
The cause of this error is usually because the DNS on your server is not configured or the configured DNS servers are having problems.
II. How to fix the error: Name or service not known on Linux
To fix the error: Name or service not known on Linux, we perform the following 2 steps.
Step 1: SSH into your server
To fix the error: Name or service not known on Linux, we first need to SSH or access your VPS or server as root. If you do not know how to SSH into your VPS/Server, you can refer to the following tutorial:
After SSH successfully, we continue with step 2 to start the error handling process: Name or service not known on Linux.
Step 2: Commands to fix the error: Name or service not known on Linux
There will be many options for you to choose from.
Option 1: Switch to using the carrier’s DNS
Before switching to using the network’s DNS, we need to know the IP list of the network’s servers first. Here’s a list of all your carrier’s DNS servers:
|Home network||DNS Server 1||DNS Server 2|
|DNS VNPT Hanoi||126.96.36.199||188.8.131.52|
|DNS VNPT HCM||184.108.40.206||220.127.116.11|
Once you have the list of carrier IPs, just run the following command to add these DNS to the server:
sudo sh -c "echo nameserver DIA_CHI_IP_1 > /etc/resolv.conf" sudo sh -c "echo nameserver DIA_CHI_IP_2 >> /etc/resolv.conf"
For example, I do with DNS of Viettel DNS, the command will be as follows:
sudo sh -c "echo nameserver 18.104.22.168 > /etc/resolv.conf" sudo sh -c "echo nameserver 22.214.171.124 >> /etc/resolv.conf"
Option 2: Switch to using Google’s DNS
Similarly to change to using Google’s DNS, we use the following commands:
sudo sh -c "echo nameserver 126.96.36.199 > /etc/resolv.conf" sudo sh -c "echo nameserver 188.8.131.52 >> /etc/resolv.conf"
Option 3: Switch to using OpenDNS’s DNS
Similarly, to change to OpenDNS‘s DNS, we use the following commands:
sudo sh -c "echo nameserver 184.108.40.206 > /etc/resolv.conf" sudo sh -c "echo nameserver 220.127.116.11 >> /etc/resolv.conf"
Option 4: Switch to Cloudflare DNS (Recommended)
To change to using Cloudflare‘s DNS, we use the command:
sudo sh -c "echo nameserver 18.104.22.168 > /etc/resolv.conf"
Of these 4 options, I recommend using option 4 because I think Cloudflare’s DNS servers work the best and update the fastest.
We retry the ping command to see that everything is working normally.
Hopefully, through this article, you have learned how to handle the “Name or service not known” error by adjusting the DNS on your Linux server. And how to quickly change the appropriate DNS server if you want. Hope the article is useful to you.
Wishing you success!