Nodename nor servname provided, or not known error [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. Nodename nor servname provided, or not known error [Solved]
  2. Make sure you don't have any typos in ~/.ssh/config
  3. Make sure you have sufficient permissions to access the file that contains your key
  4. socket.gaierror: [Errno -2] Name or service not known
  5. Solving the error when using sockets in Python

# Nodename nor servname provided, or not known error [Solved]

The error "ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known" commonly occurs when your DNS responder is glitched.

Try to restart the DNS responder to resolve the issue.

If you are on macOS, open your terminal and run the following command.

shell
sudo killall -HUP mDNSResponder

You can use the following command to SSH into your server.

shell
# ✅ Correct ssh -p 22 YOUR_NAME@hostname

Make sure to replace the YOUR_NAME and hostname placeholders.

For example:

shell
# ✅ Correct ssh -p 22 bobby@192.168.2.179

If that didn't work, try to add a .local suffix to the hostname.

shell
# ✅ Correct ssh -p 22 YOUR_NAME@hostname.local

You can also use the following command.

shell
# ✅ Correct ssh -l YOUR_NAME -p 22 hostname

However, make sure you aren't trying to use the following format.

shell
# ⛔️ Incorrect syntax ssh bobby@192.168.2.179:22

Note that the syntax ssh YOUR_NAME@hostname:YOUR_PORT is not correct as the port should not be specified after the hostname.

Instead, use the -p parameter as shown in the previous commands.

In some cases, you might also have to explicitly specify the path to the pemfile, e.g. ~/.ssh/example.pem.

shell
ssh -p 22 -i /path/to/pemfile YOUR_NAME@[HOST_IP/hostname]

If you're trying to ssh from your Mac or Linux machine to a Raspberry Pi device on your local network:

  1. Edit your /etc/hosts file.

Note: On Windows, your hosts file is most likely located at :\Windows\System32\drivers\etc\hosts.

shell
# With `gedit` sudo gedit /etc/hosts # Or with nano sudo nano /etc/hosts
  1. For example, if your IP address is 192.168.1.67 and your hostname is berry, you'd add the following line.
/etc/hosts
# ip hostname 192.168.1.67 berry

You can also try to add the .local suffix.

/etc/hosts
# ip hostname 192.168.1.67 berry.local
  1. Issue the ssh YOUR_USER@berry command.
shell
ssh YOUR_USER@berry

If that didn't work, try to add the .local suffix after the hostname.

shell
ssh YOUR_USER@YOUR_PI_SERVER.local

If the issue persists, try to ping the hostname.

shell
ping hostname.com

You can also use the echo command to add the line to your /etc/hosts file.

shell
echo 127.0.0.1 $HOST >> /etc/hosts

The $HOST environment variable will resolve to your hostname.

shell
echo $HOST

using host environment variable

# Make sure you don't have any typos in ~/.ssh/config

Another common cause of the error is having typos in your ~/.ssh/config file.

Open the file and make sure you don't have any typos or syntax errors.

shell
# With `gedit` sudo gedit ~/.ssh/config # Or with nano sudo nano ~/.ssh/config

The file should contain something similar to the following.

shell
Host host1 HostName 10.10.1.1 User bobbyhadz

Notice that there is no colon after Host host1.

# Make sure you have sufficient permissions to access the file that contains your key

Another thing to ensure is that you have the necessary permissions to access the file that contains your key.

For example, if your key is at /etc/ssh_host_rsa_key, you'd issue the following command.

shell
sudo chmod 600 /etc/ssh_host_rsa_key

# socket.gaierror: [Errno -2] Name or service not known

The "socket.gaierror: [Errno -2] Name or service not known" is also caused when trying to connect to a hostname or an IP address that cannot be reached.

Check your connection string and make sure the specified hostname (or IP address) is correct and complete.

# Solving the error when using sockets in Python

If you got the error when using sockets in Python, issue the following command from your terminal.

shell
echo 127.0.0.1 $HOST >> /etc/hosts

The $HOST environment variable will resolve to your hostname.

shell
echo $HOST

using host environment variable

If the $HOST environment variable is not defined, try using $HOSTNAME instead.

shell
echo $HOSTNAME

If the $HOSTNAME environment variable is defined (and $HOST is not), update the previous command to the following.

shell
echo 127.0.0.1 $HOSTNAME >> /etc/hosts

The command assumes that you need to connect to localhost.

Your /etc/hosts file should contain the following line.

/etc/hosts
# IP Hostname 127.0.0.1 localhost

You can also try calling the socket.gethostbyname() method as follows.

main.py
import socket socket.gethostbyname('localhost')

This will work only if localhost is defined in your /etc/hosts file.

If that didn't work, try to call socket.gethostbyname() method with an empty string.

main.py
import socket socket.gethostbyname('')

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.