MacOS – Give the router a hostname

catalinamacbook promacosNetworkrouter

How can I give my router a hostname?

So that at the terminal I can type stuff like ping router and it instantly resolves 192.168.0.1.

I can't just edit /etc/hosts and hardcode router 192.168.0.1, because then when I take my laptop to another network it will no longer work.

I would like router to always reflect the System Preferences-found router IP, enter image description here

It should automatically update whenever my router's IP address changes.

Best Answer

Thats not the way it works.

Your Mac is a client on the network(s) you attatch to and it gets its settings from from the DHCP server. In that DHCP response, theres a number of things including a DNS server assignment(s) which is where all the names on the network come from. Those name to address relationships are ultimately managed by the network admin.

Your client can’t arbitrarily assign names to IP addresses as your Mac is not the DNS server.

The /etc/hosts file is for you and you alone and must be manually updated (if necessary) when you change networks because there is nothing that’s mapping the actual router address to the name you’ve arbitrarily given it.