Commands / Interfaces and addresses

Interfaces and addresses

ip addr

Also ip a, ip address, ifconfig

Shows each network card with its addresses and prefix, and adds or removes addresses.

The first thing to run on any device: which cards it has, whether they are up, and the address with its prefix on each (10.0.1.10/24). The prefix decides which addresses count as "my own network": everything else goes to a router. ip -br addr is the one-line-per-card view. It replaced ifconfig, which you still meet in old guides.

ip addr command usage

ip [-br] [-4|-6] addr [show [dev CARD]]
ip addr add|del ADDRESS/PREFIX dev CARD
ip addr help

Options

-br
brief: one line per card, name, state, addresses
help
print every form the command takes, and stop
-4 / -6
only IPv4 or only IPv6 addresses
show dev eth0
only that card
add 10.0.1.50/24 dev eth0
put one more address on the card, with its prefix
del 10.0.1.50/24 dev eth0
take an address off the card, named with its prefix as the card lists it
scope global
skip link-local and loopback addresses

ip addr examples

ip -br addr
Lists every card on one line each, with its state and addresses.
ip addr show eth0
Shows eth0 in full: flags, MTU, MAC address, addresses with prefix and broadcast.
ip -6 addr show scope global
Shows only the global IPv6 addresses, the ones SLAAC or DHCPv6 gave.
ip addr add 10.0.1.50/24 dev eth0
Gives eth0 a second address, 10.0.1.50 in the 10.0.1.0/24 network.
ip addr del 10.0.1.50/24 dev eth0
Removes that address again.
ip -4 -br addr
Lists only the IPv4 addresses, one card per line.
ip -br -c addr
The same list in colour: states and address families stand out.
ip -br addr show up
Lists only the cards that are switched on.
ip addr show to 10.0.1.0/24
Finds which card holds an address in 10.0.1.0/24.
ip -6 addr show dev eth0 scope link
Shows eth0's link-local IPv6 address, the fe80:: one every IPv6 card has.
ip addr add 2001:db8:1::10/64 dev eth0
Gives eth0 a static IPv6 address.
ip addr flush dev eth0
Removes every address from eth0 at once.
ip -o -4 addr show eth0 | awk '{print $4}'
Prints eth0's IPv4 address and prefix alone, for a script: -o puts each address on one line.
ifconfig eth0
The old net-tools view of the same card, still found on older systems and in old guides.

Reading the output

$ ip addr show eth0
11: eth0@if6378: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether fe:26:a1:c3:bb:c3 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.0.1.10/24 brd 10.0.1.255 scope global eth0
       valid_lft forever preferred_lft forever
  • <…UP,LOWER_UP>: UP means switched on, LOWER_UP means a cable (or its virtual twin) is connected. UP without LOWER_UP is a dead link.
  • mtu 1500: the largest packet this card sends without fragmenting.
  • link/ether fe:26:…: the card's MAC address.
  • inet 10.0.1.10/24: the IPv4 address and prefix: this device treats 10.0.1.0–10.0.1.255 as its own network. brd is that network's broadcast address.
  • @if6378: the card is one end of a virtual cable; the other end is interface 6378 on the host.

Pitfalls

  • A wrong prefix is a classic fault: /16 where the network is /24 makes the device ARP for addresses that are really behind a router.
  • ip addr add without /PREFIX means /32: an address with no network, and no route for its neighbours.
  • Changes made with ip last until the device restarts: they are not saved anywhere.

Run it on real machines

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Start with a free account: Google or email, no card.

Start free