IP Addresses Explained
Every device that communicates on a network needs an address. The Internet Protocol defines what those addresses look like, how they are structured, and what the rules are for routing traffic between them. Understanding IP addressing is a prerequisite for everything else in Linux networking.
IPv4 addresses
An IPv4 address is a 32-bit number written as four decimal numbers (octets) separated by dots. Each octet is 0 through 255.
192 . 168 . 1 . 50
| | | |
8 bits each = 32 bits total
# See your IPv4 addresses
ip -4 addr show
# See just the address on eth0
ip -4 addr show dev eth0 | grep inet | awk '{print $2}'
Subnet masks and CIDR notation
An IP address alone does not tell you which part is the network and which is the host. The subnet mask (or prefix length) provides this.
The same mask can be written two ways:
192.168.1.50/24
192.168.1.50 netmask 255.255.255.0
The /24 means the first 24 bits are fixed (the network portion). The remaining 8 bits identify hosts. A /24 subnet has 2^8 = 256 addresses (254 usable).
Common prefix lengths and their sizes:
| CIDR | Subnet Mask | Usable Hosts | Example Use |
|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,214 | Class A, large enterprise |
| /16 | 255.255.0.0 | 65,534 | Medium network |
| /24 | 255.255.255.0 | 254 | Typical home/office LAN |
| /28 | 255.255.255.240 | 14 | Small segment |
| /30 | 255.255.255.252 | 2 | Point-to-point link |
| /32 | 255.255.255.255 | 1 | Single host route |
# Calculate subnet info from an address and prefix
python3 -c "import ipaddress; n = ipaddress.ip_network('192.168.1.0/24', strict=False); print(f'Network: {n.network_address}, Broadcast: {n.broadcast_address}, Hosts: {n.num_addresses-2}')"
# Or install ipcalc
ipcalc 192.168.1.50/24
Special IPv4 addresses
0.0.0.0/0 default route (all addresses)
127.0.0.0/8 loopback (127.0.0.1 is standard)
169.254.0.0/16 link-local / APIPA (no DHCP available)
255.255.255.255 limited broadcast
Private address ranges (RFC 1918)
These ranges are not routed on the public internet. Routers drop packets destined for these addresses if they arrive from the internet.
10.0.0.0/8 10.0.0.0 - 10.255.255.255 (16 million addresses)
172.16.0.0/12 172.16.0.0 - 172.31.255.255 (1 million addresses)
192.168.0.0/16 192.168.0.0 - 192.168.255.255 (65,536 addresses)
Home networks typically use 192.168.0.0/24 or 192.168.1.0/24. Cloud providers use 10.0.0.0/8 internally. Corporate networks may use any of the three ranges, often subdivided.
IPv6 addresses
IPv6 uses 128-bit addresses written as eight groups of four hexadecimal digits separated by colons:
2001:0db8:85a3:0000:0000:8a2e:0370:7334
Two shorthand rules apply:
- Leading zeros within a group can be dropped:
0db8becomesdb8 - One consecutive run of all-zero groups can be replaced with
:::
2001:db8:85a3::8a2e:370:7334
# See your IPv6 addresses
ip -6 addr show
# The loopback is ::1 (equivalent to 127.0.0.1)
ping6 ::1
ping6 -c 3 ::1
IPv6 address types
::1/128 loopback
fe80::/10 link-local (auto-assigned, not routable)
fc00::/7 unique local (private, RFC 4193)
2000::/3 global unicast (public internet)
ff00::/8 multicast
Link-local addresses (fe80::) are auto-assigned to every interface and are only usable on the local link. You must specify the interface when using them:
ping6 fe80::1%eth0 # the %eth0 specifies the interface scope
Dual-stack
Most modern Linux systems run both IPv4 and IPv6 simultaneously. When connecting to a hostname, the system tries IPv6 first (if available) and falls back to IPv4.
# Check if an address is IPv4 or IPv6
curl -4 https://ifconfig.me # force IPv4
curl -6 https://ifconfig.me # force IPv6
# See which protocol a connection uses
ss -tnp | grep ESTAB
How Linux assigns IP addresses
DHCP (dynamic assignment)
Most machines get their IP from a DHCP server automatically at boot.
# See if an interface is using DHCP
ip addr show eth0 # look for "dynamic" in the output
nmcli device show eth0 | grep METHOD
# Request a new DHCP lease
sudo dhclient eth0 # legacy
sudo networkctl renew eth0 # systemd-networkd
nmcli con up "Wired connection 1" # NetworkManager
# See current DHCP lease info
cat /var/lib/dhcp/dhclient.leases # dhclient
cat /run/systemd/netif/leases/* # systemd-networkd
Static assignment
For servers and infrastructure that need a predictable address:
# Temporary (lost on reboot)
sudo ip addr add 10.0.0.10/24 dev eth0
sudo ip route add default via 10.0.0.1
# Persistent via Netplan (Ubuntu)
# /etc/netplan/01-netcfg.yaml:
# network:
# version: 2
# ethernets:
# eth0:
# addresses: [10.0.0.10/24]
# routes:
# - to: default
# via: 10.0.0.1
# nameservers:
# addresses: [1.1.1.1, 8.8.8.8]
sudo netplan apply
# Persistent via NetworkManager
nmcli con mod "Wired connection 1" \
ipv4.method manual \
ipv4.addresses "10.0.0.10/24" \
ipv4.gateway "10.0.0.1" \
ipv4.dns "1.1.1.1,8.8.8.8"
nmcli con up "Wired connection 1"
Finding your public IP
The IP address on your local interface is your private address. Your public IP (what the internet sees) is assigned by your ISP and visible on your router.
# Query an external service for your public IP
curl -s ifconfig.me
curl -s icanhazip.com
curl -s api.ipify.org
# With IPv4 or IPv6 forced
curl -4s ifconfig.me
curl -6s ifconfig.me
# On a server with a direct public IP, ip addr shows it
ip addr show eth0
Useful IP inspection commands
# Show all interfaces and addresses
ip addr show
# Check if an address is in use on the network (ARP ping)
arping -c 3 192.168.1.1
# Show the ARP cache (MAC addresses of known neighbours)
ip neigh show
# Convert between address formats
python3 -c "import socket; print(socket.inet_aton('192.168.1.1').hex())"
python3 -c "import ipaddress; print(int(ipaddress.ip_address('192.168.1.1')))"
# Test if an address is in a given subnet
python3 -c "import ipaddress; print(ipaddress.ip_address('10.0.0.5') in ipaddress.ip_network('10.0.0.0/24'))"
Frequently Asked Questions
What is an IP address?
An IP address is a numerical label assigned to each device on a network that uses the Internet Protocol for communication. It serves two purposes: identifying the host and providing the location of the host in the network. IPv4 addresses are 32-bit numbers written as four decimal octets separated by dots (e.g., 192.168.1.1). IPv6 addresses are 128-bit numbers written as eight groups of four hexadecimal digits separated by colons (e.g., 2001:0db8:85a3::8a2e:0370:7334).
What is CIDR notation?
CIDR (Classless Inter-Domain Routing) notation expresses an IP address and its subnet mask together as address/prefix-length. For example, 192.168.1.0/24 means the first 24 bits are the network address (192.168.1) and the remaining 8 bits identify hosts within that network, giving 256 possible addresses (254 usable: .1 through .254, since .0 is the network address and .255 is the broadcast). A /32 is a single host. A /0 covers all addresses.
What are private IP addresses?
Private IP addresses are ranges reserved for use within private networks and are not routable on the public internet. The three private IPv4 ranges defined in RFC 1918 are: 10.0.0.0/8 (10.0.0.0 to 10.255.255.255), 172.16.0.0/12 (172.16.0.0 to 172.31.255.255), and 192.168.0.0/16 (192.168.0.0 to 192.168.255.255). Home routers assign addresses from these ranges to local devices and use NAT (Network Address Translation) to share a single public IP address for internet access.
What is the difference between IPv4 and IPv6?
IPv4 uses 32-bit addresses (about 4.3 billion unique addresses) written in dotted decimal notation. IPv6 uses 128-bit addresses (about 340 undecillion unique addresses) written in hexadecimal with colon separators. IPv4 address exhaustion has driven IPv6 adoption. IPv6 eliminates NAT (every device can have a globally unique address), includes better built-in security options, and has more efficient routing. Most modern Linux systems support both protocols simultaneously (dual-stack). Many ISPs still primarily issue IPv4 to consumers, with IPv6 adoption varying by region and provider.
What is a subnet mask?
A subnet mask defines which portion of an IP address identifies the network and which identifies individual hosts within that network. Written as 255.255.255.0 (or /24 in CIDR notation), it has 1s in the network portion and 0s in the host portion. Bitwise AND of an IP address with its subnet mask gives the network address. Two hosts with the same network address (i.e., the same result when their IPs are ANDed with the subnet mask) are on the same subnet and can communicate directly without a router.
How do I find my public IP address from Linux?
Your public IP address is the address your router presents to the internet, not the private address assigned to your machine. You cannot see it with ip addr — that only shows addresses on your local interfaces. To find your public IP, query an external service: curl ifconfig.me, curl icanhazip.com, or curl api.ipify.org. On a server with a directly assigned public IP, ip addr will show it directly on the interface.