I'm trying to connect to Ubuntu via SSH from OS X, and I took the same step as explained in this page (https://help.ubuntu.com/10.04/serverguide/openssh-server.html). After setting it, I tried to connect by the form of username@ip_address, then it was successful and I was able to be in Ubuntu from OS X Terminal (this time I used port 22). However, when I tried to SSH to Ubuntu by the form of username@hostname, then I couldn't and the error ssh: Could not resolve hostname my_user_name: nodename nor servname provided, or not known occurred. I also tried to connect via port 2222 by changing /etc/ssh/sshd_config, then it failed too by spitting out the error debug1: connect to address my_ip_address port 22: Connection refused (this time I tried to connect by IP address).

So I think the latter issue stems from the fact that I have to make some sort of modifications also on OS X, but don't know where/how to do and don't know it's true in the first place.

The former issue is more annoying, as I'm not sure whether I could understand it correctly. I used hostname as the value that I got from running hostname on Ubuntu Terminal...but is it right? And when I searched for the issue on the Web, I knew that it's from DNS issue...but don't know what it is and how to fix it...



In Terminal on OS X when using ssh to establish a remote connection using a hostname on a local network, add the suffix .local such that the command reads ssh username@hostname.local If you need to override the default port 22, as your example of using port 2222, add -p 2222 to the command. You can also set this up in Terminal's "New Remote Connection.." dialog (Command-Shift-K) for easy access.

| improve this answer | |
  • Thanks. I succeeded by either adding .local suffix as you suggested or modifying /private/etc/hosts Alaa suggested in the above post. Which is the better way? I guess yours is better because IP address changes once I reboot my Ubuntu... but I'm not sure. – Blaszard Jul 6 '13 at 14:45
  • With changing IP addresses, the .local option will ensure that you can connect regardless of the machine IP. Even though my server is on a reserved IP, I still use the .local option using the hostname. – douggro Jul 6 '13 at 15:25
  • This doesn't work for me, and it's not terribly surprising as local isn't the name of my (private, internal) domain. Interestingly, I am able to connect to other hosts by hostname via SSH on the same network. – Kenny Evitt Jan 3 '15 at 21:24
  • @KennyEvitt So, you're running an internal DNS server? – douggro Jan 3 '15 at 23:15
  • @douggro, yes, effectively; my router provides DNS. What's particularly strange is that both host and nslookup resolve the problematic host. – Kenny Evitt Jan 4 '15 at 0:36

When you ran ssh username@hostname, the computer that you are connecting from (your Mac OS) does not know the IP of this hostname, that' why it's telling you that ssh: Could not resolve hostname. So, what you need to do is tell your Mac that this hostname is this ip_address. To do this on your Mac, this page tells you how to do it. Basically, what you need to do is sudo nano /private/etc/hosts (sudo nano /etc/hosts on Mac OS X 10.7 and higher), and add a line at the end of the file like this:

ip_address       hostname

With the actual IP and hostname of your Ubuntu machine. Once done, Control+O, then Enter, then Control+X to save and close the file. Then do dscacheutil -flushcache for the changes to take effect. Now, when you do ssh username@hostname, it should work.

And about that port 2222:

You're right, you have to do some modification on Mac OS...well, it's just a couple more characters. When you do ssh username@hostname, what is actually happening in the background is ssh username@hostname -p22, because port 22 is the default SSH port. But, if you know that the machine you are trying to connect to is using a different port, you'll have to manually specify it. So, since you've changed the port on Ubuntu to 2222, you'll have to do ssh username@hostname -p2222. This has nothing to do with using the hostname or ip_address.

| improve this answer | |
  • Thanks for the great explanation. I've been able to run it successfully, both on hostname and port issue. I want to ask one more question... Running ssh username@hostname -p2222 every time when I want to SSH is a bit tiresome... what or how do you (or the community convention) do to cope with the issue? I came up with creating an alias, but is it the best way? – Blaszard Jul 6 '13 at 14:40
  • The problem with using an alias is (I think) you won't be able to specify another port if you wish to connect to another machine. Here's a better way: not sure if it's the same file on Mac, but edit the file /etc/ssh/ssh_config (note, it's ssh_config, not sshd_config) on your Mac and add the line Port 2222 in the end. This will make the default SSH port 2222. However, you can just change back the port to 22 on Ubuntu. – Alaa Ali Jul 6 '13 at 14:48
  • I found the file in /etc/ssh_config, so I think it is what you pointed to. If I change from port 22 to 2222 in the file, then it means every time I execute ssh command, I always use port 2222 as default...right? And it also means when I try to connect to another machine which doesn't use port 2222, then I have to add -p argument to connect to the specific machine...right? – Blaszard Jul 6 '13 at 14:55
  • Yes. Also, don't forget to mark an answer as your accepted answer to indicate to others that the question has been solved. – Alaa Ali Jul 6 '13 at 14:58
  • 1
    @Alaa Yes, the .local directs the connection to the local network for hostname resolution. Works in web browsers too: I connect to my Webmin instance using it. – douggro Jul 6 '13 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.