You may get an error message when trying to connect to a machine that says ssh: Could not resolve hostname server: Name or service not known. This might happen if you are unable connect wirelessly or don’t have enough signal strength for your device, but it can also occur because of typos in the command line prompt.
In this article we’ll talk about how to fix it so that you can access your machine again.
Solution 1: Check the Hostname
When trying to ssh to a server, verify the full command you are running. It’s possible you are typing a command that the ssh program does not understand.
Here is an example of a command you may have run:
ssh [email protected] 184.108.40.206 ssh: Could not resolve hostname myserver: Name or service not known
As you can see, the error shown is about not resolving the hostname myserver.
When you use the ssh program, the command you will use should be the following:
ssh [email protected] ssh [email protected]220.127.116.11
In the previous example, user is root and hostname is myserver which is not a valid hostname. The correct example is shown you provide user as root and hostname as 18.104.22.168.
Solution 2: Add an Entry in /etc/hosts
You may have a scenario where you do want to specify myserver as your hostname because you don’t want to type the IP address 22.214.171.124. In this case, you would need to add an entry in /etc/hosts.
To make this change, open the /etc/hosts file and add myserver line to the end of the file such as the following example:
# Sample /etc/hosts file 127.0.0.1. localhost 126.96.36.199 myserver
Once this entry has been saved in /etc/hosts, you can ssh to the server by typing:
You should not be able to connect to your specified server.
Solution 3: Check the Hosts Directive
If the previous didn’t fix your issue, it may be the ordering of the hosts directive in your /etc/nsswitch.conf file.
Take a look at your /etc/nsswitch.conf. It should have this line:
hosts: files dns
With the above line, your Linux system will look at files first which is /etc/hosts to resolve DNS. If it doesn’t find anything there, it will query DNS in /etc/resolv.conf.
If your /etc/resolv.conf shows a different entry than the above, you may consider updating it and then connect to your server again.