[linux] ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known

I am trying to set up a VPN with a Raspberry Pi, and the first step is gaining the ability to ssh into the device from outside my local network. For whatever reason, this is proving to be impossible and I haven't the slightest clue why. When I try to ssh into my server with user@hostname, I get the error:

ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known 

However, I can log into the server with,

ssh user@[local IP]

The server is a Raspberry Pi Model B running the latest distribution of Raspbian and the machine I am trying to connect to it with is a Macbook Pro running Mavericks. ssh was enabled on the Raspberry Pi when I set up Raspbian.

I have perused Stack Overflow for hours trying to see if anyone else had this problem and I have not found anything. Every ssh tutorial I find says that I should just be able to set it up on the remote machine and log in from anywhere using a hostname, and I have never had success with that.

This question is related to linux networking ssh dns

The answer is


If you're on Mac, restarting the DNS responder fixed the issue for me.

sudo killall -HUP mDNSResponder

I needed to connect to remote Amazon server

ssh -i ~/.ssh/test.pem -fN -L 5555:localhost:5678 [email protected]

I was getting the following error.

ssh: Could not resolve hostname <hostname.com>: nodename nor servname provided, or not known

Solution For Mac OSX

Pinging the host resolved the issue. I am using Mac OSX Seirra.

ping hostname.com

Now problem resolved. Able to connect to the server.

Note: I tried this solution also. But it didn't work out. Then ping resolved the issue.


If your command is:

$ ssh -p 1122  path/to/pemfile user@[hostip/hostname]

You will also face the same error

ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known

when you miss the option -i /path/to/pemfile of ssh

So Command should be:

$ ssh -p 1122 -i path/to/pemfile user@[hostip/hostname]

For me, the problem was a typo on my ~/.ssh/config file. I had:

Host host1:
  HostName 10.10.1.1
  User jlyonsmith

The problem was the : after the host1 - it should not be there. ssh gives no warnings for typos in the ~/.ssh/config file. When it can't find host1 it looks for the machine locally, can't find it and prints the cryptic error message.


I got this error by using a .yml inventory file in ansible that was not properly formatted. For multiple hosts in a group, each hostname needs to end in a hard colon ":". Otherwise ansible runs the host names together and produces this ssh error.


If you need access to your VPN from anywhere in the world you need to register a domain name and have it point to the public ip address of your VPN/network gateway. You could also use a Dynamic DNS service to connect a hostname to your public ip.

If you only need to ssh from your Mac to your Raspberry inside your local network, do this: On your Mac, edit /etc/hosts. Assuming the Raspberry has hostname "berry" and ip "172.16.0.100", add one line:

# ip           hostname
172.16.0.100   berry

Now: ssh user@berry should work.


I had the same problem: The address shown in Preferences -> Sharing -> Remote Login didn't work and I got a '... nodename nor servname provided, or not known'. However, when I manually edited the settings (in Preferences -> Sharing -> Remote Login -> edit) and enabled "Use dynamic global hostname", it suddenly worked.

enter image description here

enter image description here


I have the exact same configuration. This answer pertains specifically to connecting to a raspberry pi from inside the local network (not outside). I have A raspberry pi ssh server, and a macbook pro, both connected to a a router. On a test router, my mac connects perfectly when I use ssh danran@mypiserver, however, when I use ssh danran@mypiserver on my main router, i get the error

ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known

Just as you have gotten. It seems, the solution for me at least, was to add a .local extension to the hostname when connecting from my mac via ssh.

So, to solve this, i used the command ssh [email protected] (remember to replace the "danran" with your username and the "mypiserver" with your hostname) instead of using ssh danran@mypiserver.

To anyone reading this, try adding a .local as the suffix to your hostname you are trying to connect to. That should solve the issue on a local network.


It seems that some apps won't read symlinked /etc/hosts (on macOS at least), you need to hardlink it.

ln /path/to/hosts_file /etc/hosts


I had the same issue connecting to a remote machine. but I managed to login as below:

ssh -p 22 myName@hostname

or:

ssh -l myName -p 22 hostname

In my case I was trying ssh like this

ssh [email protected]:22 

when the correct format is:

ssh [email protected] -p 22 

I had the same problem after testing Visual Studio Code with remote-ssh plugin. During the setup of the remote host the software did ask me where to store the config-file. I thought a good place is the '.ssh-folder' (Linux-system) as it was a ssh-remote configuration. It turned out to be a bad idea. The next day, after a new start of the computer I couldn't logon via ssh on the remote server. The error message was 'Could not resolve hostname:....... Name or service not known'. What happen was that the uninstall from VSC did not delete this config-file and of course it was than disturbing the usual process. An 'rm' later the problem was solved (I did delete this config-file).


Try this, considering your allowed ports. Store your .pem file in your Documents folder for instance.

To gain access to it now all you have to do is cd [directory], which moves you to the directory of the allotted file. You can first type ls, to list the directory contents you are currently in:

ls
cd /Documents
chmod 400 mycertificate.pem
ssh -i "mycertificate.pem" [email protected] -p 80

This was happening to me when trying to access Github. The problem is that I was in the habit of doing:

git remote add <xyz> ssh:\\[email protected]......

But, if you are having this error from the question, removing ssh:\\ may resolve the issue. It solved it for me!

Note that you will have to do a git remote remove <xyz> and re-add the remote url without ssh:\\.


I had the same issue, which I was able to resolve by adding a .local to the host name, ala ssh [email protected]


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to networking

Access HTTP response as string in Go Communication between multiple docker-compose projects Can't access 127.0.0.1 How do I delete virtual interface in Linux? ConnectivityManager getNetworkInfo(int) deprecated Bridged networking not working in Virtualbox under Windows 10 Difference between PACKETS and FRAMES How to communicate between Docker containers via "hostname" java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused) wget: unable to resolve host address `http'

Examples related to ssh

Starting ssh-agent on Windows 10 fails: "unable to start ssh-agent service, error :1058" How to solve "sign_and_send_pubkey: signing failed: agent refused operation"? key_load_public: invalid format ssh connection refused on Raspberry Pi Getting permission denied (public key) on gitlab Verify host key with pysftp Can't connect to Postgresql on port 5432 Checkout Jenkins Pipeline Git SCM with credentials? How to open remote files in sublime text 3 how to setup ssh keys for jenkins to publish via ssh

Examples related to dns

How is VIP swapping + CNAMEs better than IP swapping + A records? ping: google.com: Temporary failure in name resolution What's the source of Error: getaddrinfo EAI_AGAIN? How do I solve the "server DNS address could not be found" error on Windows 10? Creating self signed certificate for domain and subdomains - NET::ERR_CERT_COMMON_NAME_INVALID How to force DNS refresh for a website? ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known How to filter wireshark to see only dns queries that are sent/received from/by my computer? How can I list ALL DNS records? How to redirect DNS to different ports