216

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
qaxf6auux
  • 2,624
  • 4
  • 17
  • 19
  • 1
    The original error is pointing to the specific issue - whatever `[hostname]` you're using cannot be resolved from where you're trying it from. Does `nslookup [hostname]` give you back the `[local IP]` you're expecting? – admdrew Nov 27 '13 at 20:20
  • 3
    `nslookup [hostname]` spits out; Server: 8.8.8.8 Address: 8.8.8.8#53 ** server can't find [hostname]: NXDOMAIN when run from my Macbook – qaxf6auux Nov 27 '13 at 20:27
  • 2
    Just to be sure - you're putting in an actual hostname in `[hostname]`, right? – admdrew Nov 27 '13 at 20:29
  • That output shows you're using Google's public DNS server(s), which doesn't have any knowledge of whatever hostname you're trying to use. How exactly are you setting up DNS records for your hostname? – admdrew Nov 27 '13 at 20:31
  • Yes, I am using my real hostname. I'm guessing that I don't want to give out my real hostname. – qaxf6auux Nov 27 '13 at 20:34
  • How do I set up DNS records for my hostname? This is probably where the problem is stemming from. – qaxf6auux Nov 27 '13 at 20:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42067/discussion-between-snip3r-and-admdrew) – qaxf6auux Nov 27 '13 at 20:36
  • 2
    `Server: 192.168.0.1 Address: 192.168.0.1#53 ** server can't find mrddr: NXDOMAIN` – Nakilon Sep 29 '14 at 20:55

16 Answers16

233

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

sudo killall -HUP mDNSResponder
Magsafe
  • 3,313
  • 2
  • 12
  • 5
  • I also needed to ping, nslookup, and dig the domain. Not sure which of these helped. Maybe just waiting helped. – Samizdis Jul 24 '19 at 11:23
  • worked for me also..this problem started after i restarted my mac..don't know what messed up during boot – Murli Apr 08 '20 at 14:31
  • 15
    I'm also curious to know why this works... and what's causing the problem in the first place. – mystic cola Apr 23 '20 at 18:52
  • 5
    If `nslookup` works, but ssh/ping/whatever doesn't, this fixes the issue. Thanks. – Erik B Jul 14 '20 at 20:59
  • This worked for me. Accessing the mac via "ping machostname" worked but ssh or git fetch via ssh failed. After running this command on mac it is fixed. Looks like a bug on mac – Luz Oct 10 '20 at 21:34
  • In mycase it was ignoring hosts under the `Include `. this is because when the `Include ` is after the content of `~/.ssh/config` it is being considered a `host`'s config. placing the line `Include ` at the top vs the bottom - caused my `ssh fakehost` to work. reference: https://superuser.com/questions/1162387/whats-wrong-with-my-openssh-include-directive – Ricky Levi Sep 02 '22 at 13:16
  • 1
    This is `The Answer` for me. WRT @RickyLevi cmt: I found a strange empty line had been inserted in my `~/.ssh/known_hosts` file immediately below the host that had suddenly become non-responsive. Don't know if this oddity had anything to do with it - just an FYI. ALSO: My experience was **No correlation** with the `nslookup` results. macOS 10.15.6 –  Nov 29 '22 at 16:34
138

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
tokhi
  • 21,044
  • 23
  • 95
  • 105
  • 1
    finally it works! there must be a bug with the syntax ```ssh myName@hostname:myPort``` – Edoardo Dec 03 '18 at 16:59
  • @Edoardo If it's a bug, it's a bug in the user; that syntax is not and has never been valid. You should review the documentation for the `ssh` command. – Asteroids With Wings Oct 23 '20 at 17:02
  • @AsteroidsWithWings will do – Edoardo Oct 26 '20 at 08:44
  • If anyone was wondering why they're so certain that the `ssh myName@hostname:myPort` is valid, you're probably getting confused with the following valid syntax `ssh ssh://myName@hostname:myPort`. – Sean1708 May 16 '23 at 17:01
33

Recently I came across the same issue. I was able to ssh to my pi on my network, but not from outside my home network.

I had already:

  • installed and tested ssh on my home network.
  • Set a static IP for my pi.
  • Set up a Dynamic DNS service and installed the software on my pi. I referenced these instructions for setting up the static ip, and there are many more instructional resources out there.

Also, I set up port forward on my router for hosting a web site and I had even port forward port 22 to my pi's static IP for ssh, but I left the field blank where you specify the application you are performing the port forwarding for on the router. Anyway, I added 'ssh' into this field and, VOILA! A working ssh connection from anywhere to my pi.

I'll write out my router's port forwarding settings.

(ApplicationTextField)_ssh     (external port)_22     (Internal Port)_22     (Protocal)_Both     (To IP Address)_192.168.1.###     (Enabled)_checkBox

Port forwarding settings can be different for different routers though, so look up directions for your router.

Now, when I am outside of my home network I connect to my pi by typing:

ssh pi@[hostname]

Then I am able to input my password and connect.

halfer
  • 19,824
  • 17
  • 99
  • 186
ekwaters
  • 346
  • 3
  • 4
22

In my case I was trying ssh like this

ssh pedro@192.168.2.179:22 

when the correct format is:

ssh pedro@192.168.2.179 -p 22 
Pedro
  • 728
  • 8
  • 18
14

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.

grebneke
  • 4,414
  • 17
  • 24
  • In addition to above, i used "windows-cmd" for getting to know ip for my server-url. I did "ping url", to get the IP. and hence, added the IP and the URL pair in the "/etc/hosts". It perfectly works there after (git pull, etc.) ! – parasrish Feb 01 '16 at 12:32
  • @grebneke I have the exact same setup. pi + mac. I also have two routers to test with. On one router ssh user@hostname works perfectly, and there is not an issue. On the other router, the EXACT same issue as the ORIGINAL POSTER happens. Turns out, to solve this, I must add a .local to the hostname. So instead of ssh user@hostname I have to use ssh user@hostname.local and then it connects. Can you possibly explain this? How does each router make a difference as to how my mac acknowleges the hostname of the raspberry pi? – DanRan Jan 12 '21 at 03:12
14

I had the same issue, which I was able to resolve by adding a .local to the host name, ala ssh user@hostname.local

horcle_buzz
  • 2,101
  • 3
  • 30
  • 59
  • That did it for me. `ping $(hostname)` was working but need to do `ssh $(hostname).local`. Mac is usually great .. but when it sucks, it really does.. – WestCoastProjects Jan 16 '17 at 22:30
  • I have a further issue: `hadoop` needs to do `ssh user@hostname` and I *can not* simply add `.local` manually. I'm stuck – WestCoastProjects Feb 09 '17 at 21:46
  • Adding `.local` did it for me on Ubuntu 19.10. – prkos Feb 26 '20 at 12:36
  • On Windows, domains for ssh require an extension. Any extension will do. Plus needed to update C:\Windows\System32\drivers\etc\hosts with new host name including extension. – BruceJo Jan 29 '22 at 16:52
6

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

6

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.

jlyonsmith
  • 873
  • 11
  • 15
4

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]
karthik v
  • 1,104
  • 1
  • 14
  • 22
  • I didnt use the pemfile options but still connected using `ssh -p xxxx user@hostname` – Yoku Sep 26 '18 at 20:51
4

I needed to connect to remote Amazon server

ssh -i ~/.ssh/test.pem -fN -L 5555:localhost:5678 ubuntu@hostname.com

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.

Community
  • 1
  • 1
mythicalcoder
  • 3,143
  • 1
  • 32
  • 42
3

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

2

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:\\git@github.com......

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:\\.

Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38
2

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 danran@mypiserver.local (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.

DanRan
  • 95
  • 9
0

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" ec2-user@ec2-1-2-3-4.us-compass-0.compute.amazonaws.com -p 80
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
0

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.

0

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).

Jens
  • 1