IP Address
IP Address
IP address is used to identify all internet devices. (Each internet device may have one or more IP address.)
There are 2 versions of IP address: IPv4 and IPv6.
- IPv4 address = 32 bits. Usually written in 4 groups, each as a decimal. e.g.
172.16.254.1
, each decimal group represent 8 bits. - IPv6 address = 128 bits. Usually written in 8 groups, each is 4 digits of hexadecimal, separated by colon, with leading 0 omitted. e.g.
2001:db8:0:1234:0:567:8:1
. Each group of hexadecimal represents 16 bits.
IPv4 is the older standard. Because it's only 32 bits, good for 2^32 unique address (about 4.2 billion). This is not enough since late 1990s. So, IPv6 was invented.
How to find the IP address of my network adapter?
- Linux: Type
ip addr
orifconfig -a
- Windows: Type
ipconfig
How to find the IP address of my router?
- Linux: Type
ip route
. The line containing “default” has the IP address of default router. - Windows: Type
ipconfig
, then the “Default Gateway” line contains your router's IP address.
IP Address Structure: Network, Host, Special Addresses
IP addresses are divided into 2 parts: network and host. The beginning bits are the network, the rest are host.
When a router gets a packet, it needs to know where to send this packet to (of all devices connected to it). Ultimately, this is done by a look-up table called Routing table (aka Routing Information Base, RIB)
When the network part of a destination IP address matches the network part of a from IP address, then the router knows it's from the same network, so it can send it to the host machine. Else, it is a different network, it can send it to another router.
The reason IP address is divided into network and host parts is because it makes routing much more efficient. Similar to a home address is divided into Country, State/Province, City, then finally street address.
Netmask: Network Bitmask
- Each IPv4 address comes with a 32 bits number called bitmask.
- Bitmask is used to indicate how many bits are the network part.
- The network bits are 1, and host bits are 0.
For example, if a IP address has a bitmask of 11111111 11111111 00000000 00000000
, it means the first 16 bits of the IP address is network, and rest the host.
CIDR Notation
CIDR notation is used to indicate how many bits in the beginning of a IP address are network. (CIDR means Classless Inter-Domain Routing)
CIDR notation is like this
x.x.x.x/n
, where the x.x.x.x
is the
usual dotted decimal notation for IP address, and the n
is
the number of bits for the network part.
example:
192.0.2.0/24
It means the first 24 bits are network.
IPv4 Special Address
When the network part of a IP address or the host part of a IP address is all 0 or all 1, it has special meaning.
- If the host part's bits are all 0, it refers to the local network.
- If the host part's bits are all 1, it's a Broadcast address, meant to sent to all hosts that belongs to the destination network/subnet. (this is called “directed broadcast”)
- If the entire IP address are all 1 (that is 255.255.255.255), it means local network broadcast. Router never forwarded packets with this destination outside the local network.
- Default route = 0.0.0.0/0
- 127.0.0.0 = loopback address. Localhost = localhost as a hostname translates to an IPv4 address in the 127.0.0.0/8 (loopback) net block, usually 127.0.0.1, or ::1 in IPv6.
There are more special addresses. See Reserved IP addresses