Read

Module 8 · Archives, compression and the network

Network interfaces: ifconfig and ip

Meet the machine's network cards, read their addresses and state with ifconfig and ip, bring them up and down, and get an address by DHCP.

What you will learn

  • Read an interface's MAC address, IPv4 address, mask and UP state.
  • Use both `ifconfig` and `ip addr` / `ip link` for the same jobs.
  • Bring interfaces up and down, add an address and request one with udhcpc.

Every way a Linux machine talks to a network is an interface: a name the kernel gives to a network card, real or virtual. lo is the loopback, a fake card that only talks to the machine itself at 127.0.0.1. eth0 is the first Ethernet card. In this VM there is also sit0, a tunnel device for IPv6 that you can ignore. Each interface has a hardware (MAC) address, zero or more IP addresses, and a state: UP or down.

ifconfig: the classic view

ifconfig alone shows only the interfaces that are UP; ifconfig -a shows all of them. In this VM a fresh boot shows nothing, because even lo starts down. The lines to read are HWaddr (the MAC), inet addr and Mask (the IPv4 address and netmask), and the flags line, where UP and RUNNING mean the card is enabled and has a link.

~% ifconfig -a eth0
eth0      Link encap:Ethernet  HWaddr 00:22:15:75:DD:2A
          inet addr:192.168.86.100  Bcast:192.168.86.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
~% ifconfig lo up
~% ifconfig eth0 down

ip: the modern tool

On current distributions ifconfig is deprecated in favour of ip, which groups its work into objects: ip link for the cards themselves, ip addr for addresses, ip route for routes (lesson 79). Addresses are written in CIDR form: 192.168.86.100/24 means the address plus a 24-bit mask, the same as 255.255.255.0. In the angle brackets, UP means enabled and LOWER_UP means the cable is plugged in.

~% ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:22:15:75:dd:2a brd ff:ff:ff:ff:ff:ff
~% ip addr show eth0 | grep 'inet '
    inet 192.168.86.100/24 brd 192.168.86.255 scope global eth0
~% ip link set lo up
~% ip addr add 10.0.0.5/24 dev eth0     # an extra address
~% ip addr del 10.0.0.5/24 dev eth0     # and remove it

Be careful with ifconfig eth0 10.0.0.5: it replaces the main address, which on a remote server cuts the branch you are sitting on. ip addr add adds a second address and leaves the first alone. Changes made this way last until reboot; distributions store permanent settings in files such as /etc/network/interfaces.

Getting an address: DHCP

Nobody types addresses by hand on a laptop: a DHCP server on the network hands one out. BusyBox's client is udhcpc. If the machine's network is switched on (Built-in mode in the machine settings), udhcpc -i eth0 asks TempMV's virtual router for a lease and configures everything: address, default route and DNS server. The virtual network always gives the guest 192.168.86.100, with the router at 192.168.86.1. With the network off, udhcpc keeps sending discovers that nobody answers; stop it with Ctrl-C.

~% udhcpc -i eth0
udhcpc: started, v1.28.4
udhcpc: sending discover
udhcpc: sending select for 192.168.86.100
udhcpc: lease of 192.168.86.100 obtained, lease time 2147483
adding dns 192.168.86.1

Commands in this lesson

CommandWhat it does
ifconfig -aAll interfaces, up or down.
ifconfig lo upEnable an interface (down disables it).
ip addrAddresses of every interface, in CIDR form.
ip link show eth0State and MAC address of eth0.
ip link set eth0 upEnable eth0 (down disables it).
ip addr add 10.0.0.5/24 dev eth0Add an extra address (del removes it).
udhcpc -i eth0Get an address by DHCP.

Quiz

  1. What is the `lo` interface?

    • The first Ethernet card
    • The loopback: the machine talking to itself at 127.0.0.1
    • A log of network errors
    • The wireless card
  2. What does `/24` mean in `192.168.86.100/24`?

    • The 24th address of the network
    • A netmask of 24 bits, i.e. 255.255.255.0
    • 24 hours of lease time
    • Port 24
  3. Why prefer `ip addr add` over `ifconfig eth0 ADDRESS` on a remote server?

    • ifconfig is slower
    • ifconfig replaces the current address and can cut your own connection
    • ip addr add is permanent
    • ifconfig needs a reboot
  4. Where do you read the MAC address in `ip link show eth0`?

    • After `inet`
    • After `link/ether`
    • After `mtu`
    • It is not shown
  5. What does `udhcpc -i eth0` configure when it gets a lease?

    • Only the hostname
    • The address, the default route and the DNS server
    • The MAC address
    • The firewall

Practice

  1. Save the line of `ip link show eth0` that contains eth0's MAC address into `/root/lab/l75/mac.txt`.

  2. The loopback interface `lo` has been switched off. Bring it back up.

  3. Add the extra address `10.0.0.5` with a 24-bit mask to `eth0`, without removing any address it already has.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.