Linux › Section I · Your network and its configuration › Topic I.10

DHCP and automatic IPv6 configuration

Almost no Linux laptop has its address typed in by hand: it asks the network. This topic shows which programs on Linux do the asking for IPv4 and IPv6, how to read the leases and lifetimes they leave behind, and how to renew them when something goes wrong.

Back to the simple network

Return to the small LAN from the start of this section: a few hosts and a router that leads to the internet. The remaining topics fill in how it really works: how a host gets its settings automatically, how a Linux machine can be the router, which addresses the LAN should use, and how a firewall protects it. Most of the examples use IPv4 because its addresses are shorter to read, with IPv6 described wherever it behaves differently. The first question is how a laptop that has just joined the network learns its address, gateway and DNS servers without anyone typing them.

DHCP in brief

For IPv4 the answer is the Dynamic Host Configuration Protocol. A client broadcasts a request, a server on the same network offers an address, and after two more messages the client has an address, prefix length, default gateway and DNS servers, on a lease of limited length. The Networking topic DHCP and network address translation describes the four messages, UDP ports 67 and 68, and the timers: the client renews with its server halfway through the lease, and tries any server at seven-eighths. On a home network the router is the DHCP server. DHCP spares administrators typing and prevents two hosts being given the same address.

DHCP clients on Linux

Network managers choose a DHCP client for you. NetworkManager uses its own built-in client, internal, by default; where support was built in, the dhcp key in NetworkManager.conf can choose dhcpcd instead. systemd-networkd also has a built-in client, enabled by DHCP=yes in a .network file; it cannot be run by itself from the command line. The classic program is dhclient from ISC, the Internet Systems Consortium. ISC stopped maintaining its DHCP software in 2022, so dhclient is legacy, though older guides use it: dhclient -r enp3s0 releases a lease and dhclient enp3s0 requests a new one. dhcpcd is a standalone client found on some smaller distributions.

Seeing the lease

A leased IPv4 address shows in ip address with the word dynamic and a valid_lft that counts down in seconds, such as inet 192.168.1.20/24 brd 192.168.1.255 scope global dynamic noprefixroute enp3s0, valid_lft 21600sec (illustrative). The default route is typically marked proto dhcp. For more detail, nmcli -f DHCP4 device show enp3s0 lists the options NetworkManager received, including the lease time, server and DNS servers. On systemd-networkd hosts, networkctl status enp3s0 shows the address, gateway and DHCP server. The client, not the server, keeps renewing; the kernel removes the address only if the lease runs out.

Worked example: where is the lease?

A router hands out 12-hour leases. nmcli -f DHCP4 device show enp3s0 reports a lease time of 43200 seconds, and ip address shows valid_lft 21600sec. 1. A 12-hour lease is \(12 \times 3600 = 43{,}200\) seconds, which matches. 2. Time used so far: \(43{,}200 - 21{,}600 = 21{,}600\) seconds, which is 6 hours, exactly half the lease. 3. Halfway is the renewal point, so the client should now send a renewal request to its server. 4. If the server answers, valid_lft jumps back up to about 43200. 5. If it does not, the client keeps the address and broadcasts to any server at seven-eighths, which is \(37{,}800\) seconds into the lease, with 5,400 seconds left. Check: \(43{,}200 - 37{,}800 = 5{,}400\).

Renewing and troubleshooting

When a laptop has the wrong address or none, start by asking for a fresh lease. With NetworkManager, nmcli connection up NAME reactivates the profile and runs DHCP again. On systemd-networkd, networkctl renew enp3s0 asks for a renewal. If no DHCP server answers, some systems fall back to an IPv4 link-local address in 169.254.0.0/16, while NetworkManager by default leaves the interface with no IPv4 address; either way, check the link and the server. The journal records what the client did; journalctl -u NetworkManager or journalctl -u systemd-networkd shows the exchange, as System logging: the journal and syslog explains.

Linux as a DHCP server

Linux can also hand out leases. dnsmasq, a small combined DHCP and DNS server, is common on small networks and is used by tools such as libvirt for virtual machines; NetworkManager's shared mode, ipv4.method shared, starts its own DHCP service to share a connection. Kea, ISC's successor to its old DHCP server, suits large networks with many subnets. For a home or small office, the DHCP server built into the router is usually simpler. The one rule that matters most: run only one DHCP server on a subnet, or clients may receive clashing addresses or a wrong gateway.

IPv6: configuring without a server

IPv6 usually works differently. Stateless address autoconfiguration (SLAAC), outlined in the Networking topic IPv6, needs no server and no leases. 1. The host forms a link-local address in fe80::/64 and checks that no neighbour already uses it. 2. It listens for, or asks for, a router advertisement (RA), an ICMPv6 message from the router giving the global prefix, which must be a /64 for SLAAC, and often DNS servers too. 3. It fills in its own 64-bit interface identifier to make a global address. 4. It adds a default route through the router's link-local address. The RA can also tell hosts to use DHCPv6 for addresses or extra settings.

Worked example: reading SLAAC results

ip -6 address on a laptop shows a global address with dynamic mngtmpaddr and valid_lft 86400sec preferred_lft 14400sec, plus a temporary address, and ip -6 route shows default via fe80::1 dev wlp2s0 proto ra metric 1024 expires 1790sec (illustrative). 1. dynamic with finite lifetimes: the prefix came from an RA, which sets those lifetimes. 2. preferred_lft is shorter than valid_lft: after 14,400 seconds, 4 hours, the address is used for no new connections, but it stays valid for 24 hours. 3. proto ra: the default route also came from an RA. 4. expires 1790sec: unless a new RA arrives, the route is removed in about 30 minutes; routers resend RAs well before then. Check: no DHCP lease appears anywhere.

Common mistakes to avoid

First, running dhclient by hand on a machine whose network manager already runs a DHCP client, so two clients fight over the interface. Second, starting a second DHCP server on a LAN that already has one in its router. Third, treating a 169.254.x.x address as a working configuration; it means DHCP failed. Fourth, expecting a DHCP server to be required for IPv6; SLAAC uses router advertisements. Fifth, assuming a lease ends without warning; the client renews at half time. Sixth, confusing preferred_lft with valid_lft: an address past its preferred lifetime still works for existing connections.

Key takeaways

DHCP gives IPv4 hosts an address, prefix, gateway and DNS servers on a renewable lease. On Linux the network manager runs the client: NetworkManager's built-in client, systemd-networkd's, or the legacy dhclient. ip address shows a lease as dynamic with a counting valid_lft, nmcli -f DHCP4 device show and networkctl status show details, and nmcli connection up or networkctl renew gets a fresh lease. dnsmasq and Kea are Linux DHCP servers, and only one should serve a subnet. IPv6 hosts usually configure themselves by SLAAC from router advertisements, giving dynamic addresses and proto ra routes with lifetimes.

Flashcards

Flashcard

DHCP lease

An address and settings granted for a limited time

Renewed at half the lease.

Test your understanding

Question 1 of 5

A laptop has a 24-hour DHCP lease, and ip address shows valid_lft 43200sec. What should its DHCP client be doing now?

Question 2 of 5

A desktop shows the address 169.254.37.12/16 after booting. What does this suggest?

Question 3 of 5

On a host managed by systemd-networkd, which command asks for its DHCP lease to be renewed?

Question 4 of 5

How does a typical IPv6 host get a global address without DHCPv6?

Question 5 of 5

Why should a LAN that already has a DHCP server in its router not get a second one on a Linux machine?

← Previous topicNext topic →