Understanding 127.0.0.1 vs. 0.0.0.0

· 1 min read · 140 Words · -Views -Comments

When starting a web service we often bind to 0.0.0.0 or 127.0.0.1. Both work, but they have different behaviors. Here’s a quick refresher.

Example using Express:

app.listen(conf.server.port, '0.0.0.0', function () {
  console.log(`Example app listening on port http://0.0.0.0:${conf.server.port}!`);
});

127.0.0.1

  1. Loopback address. If the service binds here, neither the public IP nor the LAN IP can reach it.

  2. Even if you disconnect from the network, ping 127.0.0.1 still responds because the packets never leave the host.

    https://static.1991421.cn/2022/2022-10-23-123702.jpeg

  3. We often use localhost when binding here. Strictly speaking they’re not the same—localhost is a hostname that typically maps to 127.0.0.1 via /etc/hosts.

0.0.0.0

  1. Represents all addresses on the machine (loopback, LAN, WAN). Bind here when you want the service reachable via any of them.

Final Thoughts

Choose deliberately based on exposure. Internal services should bind to 127.0.0.1 to reduce risk.

References

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover