Nội dung
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.
I. Cause
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 | 203.162.0.181 | 203.162.0.182 |
DNS VNPT HCM | 203.162.4.190 | 203.162.4.191 |
DNS FPT | 210.245.24.20 | 210.245.24.22 |
DNS Viettel | 203.113.131.1 | 203.113.131.2 |
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 203.113.131.1 > /etc/resolv.conf" sudo sh -c "echo nameserver 203.113.131.2 >> /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 8.8.8.8 > /etc/resolv.conf" sudo sh -c "echo nameserver 8.8.4.4 >> /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 208.67.222.222 > /etc/resolv.conf" sudo sh -c "echo nameserver 208.67.220.220 >> /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 1.1.1.1 > /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.
III. Summary
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!