Skip to content

Public vs private IP addresses, localhost and the default gateway

The RFC 1918 private ranges, how NAT gets them online, why 127.0.0.0/8 never leaves the machine, and how the default gateway routes everything else.

6 min read

A public IP address is one that is unique on the internet and routed across it. A private IP address comes from a range set aside for local networks, reused in millions of homes and offices at once, and never routed on the public internet. Every device on a home network has a private address, and the router holds the one public address the whole network shares.

Two more addresses complete the picture: localhost, which always means "this machine", and the default gateway, the address a machine sends everything to when it doesn't know where else to send it. To find all four on your own machine, skip to Check yours on Linux and Windows.

Public vs private at a glance

Public addressPrivate address
UniqueAcross the whole internetOnly inside its own network
Routed on the internetYesNo, dropped by internet routers
Who assigns itYour internet service provider (ISP), from blocks allocated to itYour own router, usually by DHCP
Example1.1.1.1 (Cloudflare's resolver)192.168.100.40
Reachable from outsideYes, unless a firewall blocks itNot directly

Dynamic Host Configuration Protocol (DHCP) is how a device gets its address from the network automatically.

Private IP address ranges

RFC 1918 reserves three IPv4 blocks for private networks. Any address inside them is private, and anything outside them (apart from the special blocks below) is public:

BlockRangeAddresses
10.0.0.0/810.0.0.0 to 10.255.255.25516,777,216
172.16.0.0/12172.16.0.0 to 172.31.255.2551,048,576
192.168.0.0/16192.168.0.0 to 192.168.255.25565,536

Home routers almost always hand out addresses from 192.168.0.0/16. 172.16.0.0/12 is easy to misread: it stops at 172.31.255.255, so 172.32.0.1 is a public address.

The number after the slash is how many bits are fixed, and the rest count the addresses: /8 leaves 24 bits, 2 to the power of 24, which is 16,777,216. How that works for any prefix is in IP addressing and subnetting.

Two more ranges look private and behave differently:

BlockNameWhat it means
100.64.0.0/10Shared address space (RFC 6598)Your ISP is doing its own address translation in front of your router, often called carrier-grade NAT
169.254.0.0/16Link-local (RFC 3927)The machine gave itself an address because nothing handed it one

If the router's own internet-facing address starts with 100.64 to 100.127, the address websites see isn't the router's at all: it belongs to the ISP's equipment, shared with other customers.

Find your private address

ifconfig lists every network interface on the machine with its addresses.

List your interfaces.

bash
ifconfig
text
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)

wlp0s20f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.40  netmask 255.255.255.0  broadcast 192.168.100.255
        RX packets 261860  bytes 239426830 (228.3 MiB)
        TX packets 184483  bytes 85396481 (81.4 MiB)

The lines with the hardware address, the IPv6 addresses and the error counters are cut from the wlp0s20f3 block.

Read the inet line of the interface that's up. wlp0s20f3 is the Wi-Fi interface (names starting wl are wireless, en wired), and inet 192.168.100.40 is its private address. netmask 255.255.255.0 says the first three numbers are the network, so every address from 192.168.100.0 to 192.168.100.255 is on the same local network. RX and TX count the packets received and sent.

lo is the loopback interface, covered below. On newer Linux systems ip addr shows the same thing. If ifconfig is missing, it is in the net-tools package.

How a private address reaches the internet

A packet from 192.168.100.40 can't cross the internet as it is. RFC 1918 says routers outside private networks, especially those of ISPs, "are expected to be configured to reject (filter out) routing information about private networks", so no reply could ever find its way back to it. The router fixes that with Network Address Translation (NAT): it swaps the private source address for its own public one on the way out, remembers the swap, and reverses it when the reply arrives.

So every website sees the router's public address, never 192.168.100.40. Every device in the house shares that one public address, and the router tells their conversations apart by port number, the same ports covered in TCP vs the User Datagram Protocol (UDP).

To see the public address the internet sees for you, ask a website that echoes it back:

bash
curl ifconfig.me

No output is shown here for the same reason you shouldn't post yours: a public address points at one connection, and often one household.

Localhost and the loopback address

Loopback is a network interface that never leaves the machine. Anything sent to a loopback address is delivered straight back to the same host, without touching a network card.

IPv4 reserves a whole block for it, 127.0.0.0/8, and RFC 1122 is blunt about it: "Addresses of this form MUST NOT appear outside a host." IPv6 has a single loopback address, ::1 (RFC 4291). localhost is the name for it: RFC 6761 says users may assume that address queries for localhost names will always resolve to the loopback address.

Ping the name.

bash
ping -c 1 localhost
text
PING localhost (::1) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.020 ms

-c 1 sends one packet and stops; without it, Linux pings until you press Ctrl+C. The name resolved to ::1, the IPv6 loopback, which this laptop tries first.

Ping any address in 127.0.0.0/8. 127.0.0.1 is the one everyone uses, but the whole block loops back:

bash
ping -c 1 127.45.6.7
text
PING 127.45.6.7 (127.45.6.7) 56(84) bytes of data.
64 bytes from 127.45.6.7: icmp_seq=1 ttl=64 time=0.010 ms

0.010 milliseconds, against at least 25 milliseconds to reach 1.1.1.1 from the same laptop, because the packet never left it.

Why loopback matters for security

A service listening on 127.0.0.1 can only be reached from the same machine. A service listening on 0.0.0.0 listens on every interface, including the one facing the network.

That one setting decides whether a development database, an admin panel or a debugging port is private to the machine or open to everyone on the same Wi-Fi. You can see the difference with two throwaway web servers, started from an empty folder:

Start one server on loopback and one on every interface.

bash
python3 -m http.server 8000 --bind 127.0.0.1 &
python3 -m http.server 8001 --bind 0.0.0.0 &

--bind sets the address the server listens on, and & runs each one in the background.

List the listening ports.

bash
netstat -tln
text
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN

-t shows TCP, -l only ports that are listening, and -n prints numbers instead of names. Read the Local Address column: port 8000 answers only this machine, and port 8001 answers anyone who can reach 192.168.100.40. Other listening lines on this laptop are cut.

Stop both servers.

bash
kill %1 %2

Find your default gateway

The default gateway is the router a machine sends a packet to when the destination isn't on its local network. It's "default" because it's the route used when no more specific one matches.

Print the routing table.

bash
route -n
text
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.1   0.0.0.0         UG    600    0        0 wlp0s20f3
192.168.100.0   0.0.0.0         255.255.255.0   U     600    0        0 wlp0s20f3

-n prints addresses as numbers instead of looking up names.

Find the line whose destination is 0.0.0.0. That's the default route, and its Gateway column, 192.168.100.1, is the default gateway. The G in UG marks a route that goes through a gateway. The second line is the local network: anything in 192.168.100.0 with mask 255.255.255.0 is sent directly over Wi-Fi, so its gateway is 0.0.0.0, meaning none.

A packet for 1.1.1.1 keeps 1.1.1.1 as its destination IP address, but the Ethernet frame carrying it is addressed to the gateway's MAC address, which the laptop finds with ARP. That's exactly why poisoning the gateway's ARP entry redirects all of a machine's internet traffic at once. Media access control (MAC) addresses and the Address Resolution Protocol (ARP) are covered in that post.

On newer Linux systems ip route prints the same table.

Check yours on Linux and Windows

On Linux

Private address: ifconfig, then the inet line of the interface that's up (what each line means).

Public address: curl ifconfig.me.

Loopback: ping -c 1 localhost, which should answer from ::1 or 127.0.0.1.

Default gateway: route -n, then the Gateway column of the 0.0.0.0 line.

What listens where: netstat -tln, then the Local Address column: 127.0.0.1 is this machine only, 0.0.0.0 is every interface.

On Windows

These run in Command Prompt. Nothing here was captured on Windows, so no output is shown.

Private address and default gateway: ipconfig. Each adapter has a line labelled "IPv4 Address", one labelled "Subnet Mask" and one labelled "Default Gateway".

Public address: curl ifconfig.me.

Loopback: ping localhost. Windows sends four packets by default.

The routing table: route print. The default route is the line with destination 0.0.0.0 and mask 0.0.0.0.

What listens where: netstat -an, then the lines whose state is LISTENING.

On macOS

macOS ships ifconfig, ping -c 1 localhost and curl ifconfig.me as Linux does. The default gateway is in netstat -rn, on the line whose destination is default, and netstat -an | grep LISTEN lists the listening ports.

If your address starts with 169.254, your machine never heard back from a DHCP server, and the problem is between it and the router.