ip (iproute2) (1/6)
ip (from iproute2) is the one Linux tool for addresses, interfaces, routes and the neighbour cache. This part covers its syntax and global options, the object list, ip address add/change/replace/del/show/flush/save/restore, ip link add with its link types, and a real 802.1Q VLAN.
17.1 What ip does and the syntax
- Syntax: ip [OPTIONS] OBJECT { COMMAND | help }.
- Built into Linux: package iproute2 (Debian/Ubuntu) or iproute (RHEL).
- Not on Windows/macOS: there you use ipconfig, netsh, ifconfig.
- It talks to the kernel over netlink, and changes are live immediately (but not persistent).
- Step 1
1.host-b (192.168.37.20) pings 192.168.37.50
- Step 2
2.Packet crosses bridge n017-lan to host-a: no such address, 100% loss
- Step 3
3.On host-a: ip address add 192.168.37.50/24 dev eth0
- Step 4
4.Same ping reaches host-a and the reply comes back
- Step 5
5.host-b: 0% packet loss
17.2 Global options
- -V version; -br brief (one line per interface: name, state, address); -4/-6 family; -o one record per line.
- -s statistics (counters) with -h human-readable; -j -p pretty JSON for scripts.
- -d details, -n NS runs inside a network namespace, -c colours, -b FILE batch with -force to continue after errors.
ip -Vip -br addrip -4 -o addr showip -s -h link show eth0ip -j -p addr show dev lo$ ip -Vip utility, iproute2-6.9.0$ ip -br addrlo UNKNOWN 127.0.0.1/8 ::1/128gre0@NONE DOWNeth0@if398 UP 192.168.37.10/24$ ip -4 -o addr show1: lo inet 127.0.0.1/8 scope host lo\9: eth0 inet 192.168.37.10/24 brd 192.168.37.255$ ip -s -h link show eth09: eth0@if398: <BROADCAST,MULTICAST,UP,LOWER_UP> RX: bytes packets errors dropped missed 1.89k 21 0 0 0 TX: bytes packets errors dropped carrier 126 3 0 0 0$ ip -j -p addr show dev lo[ { "ifindex": 1, "ifname": "lo", "mtu": 65536, "operstate": "UNKNOWN",-d details: link type params-f FAM inet inet6 bridge mpls link-6 -B -M -0 family shortcuts-r DNS names instead of IPs-n NS run the command in netns NS-N numbers instead of names-a on all objects (-all)-c color (-color=auto/never)-t -ts timestamps in monitor-rc SIZE netlink receive buffer-iec IEC units (1Ki = 1024)-echo print the applied config-b FILE batch: commands from a file-force go on after batch errors-l N max loops for flush17.3 Objects (ip OBJECT)
- The word after ip decides which kernel table you touch.
- Everyday four: address (a), link (l), route (r), neigh; short forms like ip a and ip r work.
- rule, netns, tunnel, monitor and others come in the next parts; ila, ntable, tcp_metrics are advanced.
address a IPv4/IPv6 addresseslink l network devices (L2)route r routing tableneigh ARP / NDP cacherule policy routing rulesnetns network namespacesmaddress multicast addressesmonitor netlink events livetunnel tuntap IP tunnels, TUN/TAPvrf nexthop VRF, nexthop objectsstats interface statisticsxfrm IPsec policy and statemptcp mroute MPTCP, multicast routestoken sr ioam IPv6 token, SRv6, IOAMfou l2tp ... FOU, L2TPv3, macsec, ntableila Identifier-Locator Addressingntable / ntbl neighbour cache parameterstcp_metrics / tcpmetrics TCP metrics cache17.4 ip address add / change / replace
- add: new address; label eth0:1 names it; valid_lft/preferred_lft give it a lifetime (300 s / 120 s).
- Extra addresses show as secondary; the ping from host-b now works.
- change: update an existing address (preferred_lft 0 makes it deprecated).
- replace: add, or update if it already exists; -echo prints what was applied.
ip address add 192.168.37.50/24 dev eth0 \ label eth0:1ip address add 192.168.37.60/24 dev eth0 \ valid_lft 300 preferred_lft 120ip -4 addr show dev eth0ip address change 192.168.37.60/24 dev eth0 \ preferred_lft 0ip -echo address replace 192.168.37.70/24 \ dev eth0[host-b] $ ping -c 2 -W 1 192.168.37.502 packets transmitted, 0 received, 100% packet loss$ ip address add 192.168.37.50/24 dev eth0 \ label eth0:1$ ip -4 addr show dev eth0 inet 192.168.37.10/24 brd 192.168.37.255 inet 192.168.37.50/24 scope global secondary eth0:1 inet 192.168.37.60/24 scope global secondary dynamic valid_lft 299sec preferred_lft 119sec[host-b] $ ping -c 2 192.168.37.5064 bytes from 192.168.37.50: icmp_seq=2 ttl=642 packets transmitted, 2 received, 0% packet loss$ ip -echo address replace 192.168.37.70/24 \ dev eth09: eth0 inet 192.168.37.70/24 scope global secondarylocal ADDR interface address (default)peer ADDR point-to-point remote endbroadcast ADDR broadcast addressbrd + broadcast calculated for youanycast ADDR anycast address (IPv6)scope global valid everywherescope link only on this linkscope host only inside this hostmetric N priority of the prefix routehome Mobile IPv6 home addressmngtmpaddr privacy temp addressesnodad skip DAD (IPv6)optimistic optimistic DAD (IPv6)noprefixroute no prefix routeautojoin join the multicast group17.5 ip address show (filters) and delete
- to PREFIX filters by prefix, label 'eth0:*' by label, deprecated shows the address whose preferred lifetime is 0, up only running interfaces.
- ip address del needs the device (dev eth0).
- A minus before a flag inverts it (e.g. -tentative); primary shows only main addresses.
ip address show to 192.168.37.0/24ip -4 address show label 'eth0:*'ip -4 address show secondaryip -4 address show deprecatedip -br address show upip address del 192.168.37.60/24 dev eth0ip address del 192.168.37.70/24 dev eth0ip -4 -br addr show dev eth0$ ip -4 address show label 'eth0:*' inet 192.168.37.50/24 scope global secondary eth0:1$ ip -4 address show deprecated inet 192.168.37.60/24 scope global secondary deprecated$ ip -br address show uplo UNKNOWN 127.0.0.1/8 ::1/128eth0@if398 UP 192.168.37.10/24$ ip address del 192.168.37.60/24 dev eth0$ ip address del 192.168.37.70/24 dev eth0$ ip -4 -br addr show dev eth0eth0@if398 UP 192.168.37.10/24 192.168.37.50/24dev IFNAME only this devicescope SCOPE host, link, globalto PREFIX addresses in this prefixlabel PATTERN label match (eth0:*)master DEV ports of this masternomaster devices without a mastervrf NAME interfaces of this VRFtype TYPE only devices of this typeup only running interfacesdynamic SLAAC addresses (IPv6)permanent static addresses (IPv6)tentative DAD still pendingdeprecated preferred lifetime overdadfailed DAD failedsecondary extra (alias: temporary)17.6 flush / save / restore / showdump
- Step 1
1.Add 172.16.10.1/24 and .2/24 on dummy0
- Step 2
2.ip address save dev dummy0 > /tmp/addr.bin (binary dump)
- Step 3
3.ip -s address flush: deletes 3 addresses (incl. IPv6 link-local)
- Step 4
4.ip address showdump < file: readable view of the dump
- Step 5
5.ip address restore < file: addresses are back
ip address add 172.16.10.1/24 dev dummy0ip address add 172.16.10.2/24 dev dummy0ip address save dev dummy0 > /tmp/addr.binip -s address flush dev dummy0ip -br address show dummy0ip address showdump < /tmp/addr.binip address restore < /tmp/addr.binip -br address show dummy0$ ip -s address flush dev dummy0*** Round 1, deleting 3 addresses ****** Flush is complete after 1 round ***$ ip -br address show dummy0dummy0 UNKNOWN$ ip address showdump < /tmp/addr.bin inet 172.16.10.1/24 scope global dummy0 inet 172.16.10.2/24 scope global secondary dummy0 inet6 fe80::ff:fe00:37/64 scope link$ ip address restore < /tmp/addr.bin$ ip -br address show dummy0dummy0 UNKNOWN 172.16.10.1/24 172.16.10.2/2417.7 ip link add and link types
- ip link add NAME ... type TYPE creates a virtual interface; common params: mtu, address (MAC), txqueuelen, index, netns, GSO size.
- ip -d link show prints the type-specific details; type dummy as a filter lists only dummy interfaces.
- The type filter works in address show and link show; write the type name exactly (it goes straight to the kernel).
ip link add dummy0 mtu 9000 \ address 02:00:00:00:00:37 \ txqueuelen 2000 type dummyip link set dummy0 upip -d link show dummy0ip link show type dummy$ ip -d link show dummy010: dummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 9000 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 2000 link/ether 02:00:00:00:00:37 dummy addrgenmode eui64 numtxqueues 1$ ip link show type dummy10: dummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 9000 qdisc noqueue state UNKNOWN link/ether 02:00:00:00:00:37| Where | Most common link types |
|---|---|
| Containers | veth, bridge |
| Servers | bond, vlan |
| Cloud networking | vxlan, geneve |
| Testing | dummy, netdevsim, virt_wifi |
dummy fake interface, testingveth virtual Ethernet pairvlan 802.1Q tagged VLANbridge software switchbond team links combine (bonding)macvlan sub-interface with own MACipvlan L3 based sub-interfacevxlan L2 over UDP (overlay)geneve generic overlaygre ipip IPv4 tunnelssit IPv6 over IPv4vrf separate routing domainmacsec 802.1AE encrypted linkcan vcan CAN bus devicesifb nlmon traffic shaping, netlink monitorlowpan 6LoWPAN: IPv6 over 802.15.4/BTvirt_wifi Wi-Fi simulation device (rtnetlink)wwan WWAN modem link (in ip link help)17.8 VLAN (ip link add ... type vlan)
- A VLAN splits one physical network into many logical networks; each frame carries an 802.1Q tag with the VLAN ID.
- host-a's ping leaves eth0.37, the kernel adds tag 37, the switch forwards it, eth0.37 on host-b strips the tag.
- Step 1
1.host-a sends ping out eth0.37 (172.16.37.1)
- Step 2
2.Kernel adds 802.1Q tag: vlan 37 (ethertype 0x8100)
- Step 3
3.Bridge n017-lan forwards the tagged frame
- Step 4
4.host-b eth0.37 removes the tag and accepts the packet
- Step 5
5.Reply returns the same way, tagged vlan 37
# host-a (host-b: same, 172.16.37.2)ip link add link eth0 name eth0.37 \ type vlan id 37ip addr add 172.16.37.1/24 dev eth0.37ip link set eth0.37 upip -d link show eth0.37ping -c 2 172.16.37.2# host-btcpdump -e -n -i eth0 -c 2 vlan and icmp$ ip -d link show eth0.3711: eth0.37@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> vlan protocol 802.1Q id 37 <REORDER_HDR>$ ping -c 2 172.16.37.264 bytes from 172.16.37.2: icmp_seq=1 ttl=642 packets transmitted, 2 received, 0% packet loss[host-b] $ tcpdump -e -n -i eth0 -c 2 vlan and icmp16:21:30.580578 fa:4b:13:4f:d4:c0 > fa:f6:3f:52:ef:27, ethertype 802.1Q (0x8100), length 102: vlan 37, p 0, ethertype IPv4 (0x0800), 172.16.37.1 > 172.16.37.2: ICMP echo request, id 5841, seq 1- ID 0-4095; protocol 802.1ad builds QinQ (double tag); gvrp/mvrp register the VLAN on switches; QoS maps translate VLAN priority to Linux priority.
id VLANID 0-4095 (0x = hex)protocol 802.1Q normal VLAN tag (C-tag)protocol 802.1ad QinQ service tag (S-tag)reorder_hdr on VLAN header hide (default)gvrp on|off register VLAN via GARPmvrp on|off register VLAN via MVRPloose_binding free from parent statebridge_binding follows bridge port stateingress-qos-map receive: VLAN prio to Linuxegress-qos-map send: Linux prio to VLAN17.9 Comparison: ip vs ifconfig
| Point | ifconfig | ip |
|---|---|---|
| Package | net-tools (unmaintained) | iproute2 (netlink) |
| Secondary IPs without label | Not shown | Shown (secondary) |
| Scope | Interfaces only | Addresses, links, routes, neigh, netns, tunnels |
| Output for scripts | Text only | -o, -br, JSON (-j) |
In the lab, 192.168.37.60 (added without a label) is missing from ifconfig:
ip -4 addr show dev eth0ifconfig -a | grep -E '^[a-z]|inet '$ ip -4 addr show dev eth0 inet 192.168.37.10/24 brd 192.168.37.255 inet 192.168.37.50/24 scope global secondary eth0:1 inet 192.168.37.60/24 scope global secondary dynamic$ ifconfig -a | grep -E '^[a-z]|inet 'eth0 Link encap:Ethernet inet addr:192.168.37.10eth0:1 Link encap:Ethernet inet addr:192.168.37.50lo Link encap:Local Loopback17.10 Common mistakes
- Expecting ip changes to survive a reboot: save them in netplan, nmcli or NetworkManager.
- Trusting ifconfig for the full picture: it only shows labelled aliases.
- Forgetting dev on ip address del, or mistyping a type name in the type filter.
- Forgetting ip link set DEV up after ip link add.
17.11 Interview tips
- ip vs ifconfig: iproute2 over netlink vs old net-tools; ip does addresses, routes, namespaces in one tool, with JSON.
- Syntax = options + object + command; know -br, -s, -j.
- replace = idempotent add (good for scripts); save/restore back up addresses.
- VLAN: ip link add link eth0 name eth0.37 type vlan id 37; frames carry an 802.1Q tag.