Linux › Section I · Your network and its configuration › Topic I.2
The internet layer and IP addresses
Every conversation a Linux machine has on a network starts with an IP address on one of its interfaces. This topic shows how to find those addresses, read what the ip command says about them, and work out which subnet each one belongs to.
Addresses live on interfaces
The internet layer moves datagrams from one host to another using IP addresses. As the Networking topic IPv4 datagrams and addressing explains, an address belongs to an interface, not to the whole machine. On Linux that is easy to see. A laptop typically has a loopback interface, lo, plus one interface for its wired port and one for Wi-Fi, and each connected interface carries its own address. A server with two network cards on two subnets has at least one address on each. The theory of the 32-bit address is taught in the Networking course; here the focus is on what Linux shows you.
Viewing addresses with ip
The standard command is ip address show, which can be shortened to ip addr or simply ip a. It lists every interface, numbered, with its state, its hardware address and its IPv4 and IPv6 addresses. Useful variations: ip -4 address shows only IPv4, ip address show dev enp3s0 limits the output to one interface, and ip -br address prints one brief line per interface, which is ideal for a quick check. Ordinary users can run all of these; only changing addresses needs root. The legacy command ifconfig shows similar information in a different layout, with the mask written out, but it may not be installed.
Reading the inet line
Here is an illustrative entry: inet 192.0.2.150/26 brd 192.0.2.191 scope global dynamic enp3s0, followed by valid_lft 3412sec preferred_lft 3412sec. The word inet marks an IPv4 address (inet6 marks IPv6). The address is 192.0.2.150 and /26 is its prefix length. brd gives the subnet's broadcast address. scope global means the address is valid beyond this machine; the loopback address 127.0.0.1 shows scope host instead. dynamic, with a finite valid_lft, means the address was handed out for a limited time, typically by DHCP. The last word names the interface.
Subnets, prefixes and masks
A subnet is a block of addresses whose hosts can reach each other directly. Its network prefix is the run of leading bits that every address in the block shares, and the prefix length after the slash counts those bits. The same boundary written as a subnet mask has that many 1 bits followed by 0 bits. For /26 the mask is 26 ones then 6 zeros: 255.255.255.192, because the last byte is 11000000, which is 192. The 6 host bits give \(2^6 = 64\) addresses. Linux tools nearly always use the slash form, called CIDR notation, and accept it when you configure an address.
Common prefix lengths
A handful of prefix lengths cover most networks you will meet. A /24, mask 255.255.255.0, holds 256 addresses, the usual size for a home or small-office LAN. A /16, mask 255.255.0.0, holds 65,536 and a /8, mask 255.0.0.0, holds about 16.8 million. Smaller blocks appear on servers and links: a /26 holds 64, a /28 holds 16 and a /30 holds 4. In each subnet the first address names the network and the last is its broadcast address, so a block of \(2^n\) addresses has \(2^n - 2\) usable for hosts; the tiny blocks are special cases: a /31 on a point-to-point link uses both its addresses, and a /32 names a single address. The general rule is \(2^{32-x}\) addresses for a /x block.
Worked example: decoding an address
ip address shows inet 192.0.2.150/26 on enp3s0. Find the subnet, broadcast address and host range. 1. Host bits: \(32 - 26 = 6\), so the block has 64 addresses. 2. Blocks of 64 start at multiples of 64 in the last byte: 0, 64, 128, 192. The largest not above 150 is 128, so the network is 192.0.2.128/26. 3. The block ends just before the next one, at 192.0.2.191, the broadcast address. 4. Usable hosts are 192.0.2.129 to 192.0.2.190, which is 62 addresses. Check: the brd field in the output also says 192.0.2.191, which agrees.
Worked example: checking with Python
Most Linux systems include Python 3, whose ipaddress module does subnet arithmetic exactly. Is 192.0.2.200 on the same subnet as 192.0.2.150/26? 1. Run python3 -c "import ipaddress as i; print(i.ip_interface('192.0.2.150/26').network)" and it prints 192.0.2.128/26. 2. Run python3 -c "import ipaddress as i; print(i.ip_address('192.0.2.200') in i.ip_network('192.0.2.128/26'))" and it prints False. 3. So 192.0.2.200 lies in the next block, 192.0.2.192/26, and the host must reach it through a router. 4. Repeating step 2 with 192.0.2.140 prints True, because that address falls between .129 and .190. Check: this matches the hand method from the previous card.
Several addresses on one interface
An interface can hold more than one address, for instance when a web server answers on two addresses or while a machine moves from an old subnet to a new one. ip address lists each on its own inet line. When a second address falls inside a subnet the interface already has, Linux marks it secondary in the output. Adding and removing addresses by hand with ip address add and ip address del is covered in Configuring interfaces and network managers, together with the reason such changes vanish at the next reboot unless they are saved in configuration.
Common mistakes to avoid
First, assuming every subnet is a /24 and that the broadcast address always ends in .255; read the prefix length. Second, giving a host the network or broadcast address of its subnet. Third, thinking a machine has one IP address; each interface has its own, and lo always has 127.0.0.1. Fourth, mixing up inet and inet6 lines in ip output. Fifth, treating dynamic as an error; it simply means the address has a lifetime, usually from DHCP. Sixth, confusing the prefix length with the number of hosts: /26 means 26 network bits and 6 host bits, not 26 hosts.
Key takeaways
IP addresses belong to interfaces, and ip address show (ip addr, ip a) lists them; ip -4 address, ip address show dev NAME and ip -br address narrow the view, while ifconfig is legacy. In an inet line, the address carries a prefix length, brd is the broadcast address, scope global or host says where it is valid, and dynamic with valid_lft marks a leased address. A /x block holds \(2^{32-x}\) addresses, two of which, the network and broadcast addresses, are not given to hosts. Python's ipaddress module checks subnet arithmetic exactly.
Flashcards
Flashcard
Lists every interface with its IPv4 and IPv6 addresses
Test your understanding
Question 1 of 5
ip address shows inet 192.0.2.150/26. What is the network address of its subnet?
Question 2 of 5
How many addresses in a /28 subnet can be assigned to hosts?
Question 3 of 5
In ip address output, what does scope host on the line inet 127.0.0.1/8 tell you?
Question 4 of 5
Which command gives one short summary line per interface, with its state and addresses?
Question 5 of 5
A host is configured as 192.0.2.150/26. Which address is on the same subnet, so the host can reach it without a router?
