Last updated: Apr 11, 2024
Reading time·4 min

~/.ssh/configThe 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.
sudo killall -HUP mDNSResponder
You can use the following command to SSH into your server.
# ✅ Correct ssh -p 22 YOUR_NAME@hostname
Make sure to replace the YOUR_NAME and hostname placeholders.
For example:
# ✅ Correct ssh -p 22 bobby@192.168.2.179
If that didn't work, try to add a .local suffix to the hostname.
# ✅ Correct ssh -p 22 YOUR_NAME@hostname.local
You can also use the following command.
# ✅ Correct ssh -l YOUR_NAME -p 22 hostname
However, make sure you aren't trying to use the following format.
# ⛔️ 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.
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:
/etc/hosts file.Note: On Windows, your hosts file is most likely located at
:\Windows\System32\drivers\etc\hosts.
# With `gedit` sudo gedit /etc/hosts # Or with nano sudo nano /etc/hosts
192.168.1.67 and your hostname is
berry, you'd add the following line.# ip hostname 192.168.1.67 berry
You can also try to add the .local suffix.
# ip hostname 192.168.1.67 berry.local
ssh YOUR_USER@berry command.ssh YOUR_USER@berry
If that didn't work, try to add the .local suffix after the hostname.
ssh YOUR_USER@YOUR_PI_SERVER.local
If the issue persists, try to ping the hostname.
ping hostname.com
You can also use the echo command to add the line to your /etc/hosts file.
echo 127.0.0.1 $HOST >> /etc/hosts
The $HOST environment variable will resolve to your hostname.
echo $HOST

~/.ssh/configAnother 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.
# With `gedit` sudo gedit ~/.ssh/config # Or with nano sudo nano ~/.ssh/config
The file should contain something similar to the following.
Host host1 HostName 10.10.1.1 User bobbyhadz
Notice that there is no colon after Host host1.
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.
sudo chmod 600 /etc/ssh_host_rsa_key
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.
If you got the error when using sockets in Python, issue the following command from your terminal.
echo 127.0.0.1 $HOST >> /etc/hosts
The $HOST environment variable will resolve to your hostname.
echo $HOST

If the $HOST environment variable is not defined, try using $HOSTNAME
instead.
echo $HOSTNAME
If the $HOSTNAME environment variable is defined (and $HOST is not), update
the previous command to the following.
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.
# IP Hostname 127.0.0.1 localhost
You can also try calling the socket.gethostbyname() method as follows.
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.
import socket socket.gethostbyname('')
You can learn more about the related topics by checking out the following tutorials: