Networking – How to Access Host Locally and Remotely in a Unified Way

dnsnetworkingrouter

I have a SSH server running on my local network which is exposed to the public internet via my router's port forwarding. I have also set up a name for the host locally on my machine (via etc/hosts) and remotely (via dynamic DNS), let's say:

            ip            name
locally     192.168.0.5   myhost
remotely    11.22.33.44   myhost.example.com

From within my local network, I can access the server via ssh 192.168.0.5 / ssh myhost and from outside my network I can access it via ssh 11.22.33.44 / ssh myhost.example.com.

To that point, everything works fine. However, using two different hostnames for the same server is a bit cumbersome. I'd like to access the server in a unified way, i.e. using only a single IP / hostname that works both, locally and remotely.

Is there an easy solution to this problem? I'm on macOS if that matters.

BTW: I cannot access my router from the local network using its public IP.

Best Answer

Since you have Dynamic DNS set up, you do not need to make changes to /etc/hosts file.

For external access you would use DNS for host to IP resolution and you want the same for local network.

Most routers do support DNS injection. You need to check your router config and assign 192.168.0.5 IP address to correspond to the same Dynamic DNS hostname as if it were from external network. Then when you are in internal network, and use your router as DNS server, the router will resolve myhost.example.com to local address. And when you go out and try externally access, then Dynamic DNS will take its turn to resolve to external IP.

If you have trouble setting up your router, then post the make and model, so we could look if it supports required settings.

Related Question