How to ssh into Mac on outside network when I can when on the same network

I can ssh just fine into my other Mac when both machines are on the same network using ssh -p 22 [email protected] or ssh [email protected], but when I am on a different network, I cannot ssh successfully. I am met with "ssh: connect to host xxx.xxx.x.xx port 22: Network is unreachable". I have remote login turned on on the machine I am trying to get into. Any help is appreciated. Thanks!


Solution 1:

Different network services use different default "ports" to send information. Web pages use port 80, ssh uses port 22. Most likely your issue is caused by your router blocking port 22 access from outside networks.

To fix this you need to go to the port forwarding (not the firewall) configuration page for your router and add a rule which defines the relationship between your internal (home) network and the external (the Internet) network. Below is an example of port forwarding from my router. example of port forwarding on a router

The Name(User) value does not matter, it is an internal reference only.

The important things are the:

  • LAN IP, that will be the internal address of the computer you are trying to SSH to.
  • External port, this the port the router will open to let you connect ... it does not have to be port 22.
  • Internal port, this is port on your computer ... it does not have to be port 22, but if do not use port 22 though you will need change it to something else in sshd.config in /etc/ssh or have software that does it for you (I am using Screens).

This page is from an ipTime router. Your router will probably be different, but the port forwarding page will have the same options. Once you have set up port forwarding, follow the instructions for saving configuration changes for your brand of router.

Before you close your router configuration webpage there is one more thing to do!

Find the WAN IP address. It may or may not go by a different name on your router, but you will know this IP when you see it because it is the public IP address assigned to your router by your internet provider. You need this IP address to SSH to your computer from anywhere outside your home network.

So instead of using ssh [email protected] you will use this: ssh username@Public-IP-address.