Friday, March 1, 2013

The ip command (iproute2) and virtual interfaces

I've never quite gotten the hang of using "ip" in place of ifconfig, but this blog post shows how to do several things I have wondered about.
The main thing I was looking for was how to add and delete a virtual interface which required some additional research.

The not at all obvious way to get help on this is:
# ip link add help
or
# man ip-link

Show status of interfaces

# ip ad
<... snipped loopback stuff ...>
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 5c:26:0a:2b:29:54 brd ff:ff:ff:ff:ff:ff
    inet 64.112.251.9/21 brd 64.112.255.255 scope global eno1
    inet6 fe80::5e26:aff:fe2b:2954/64 scope link
       valid_lft forever preferred_lft forever

Add a virtual interface

# ip link add type veth
# ip ad
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 5c:26:0a:2b:29:54 brd ff:ff:ff:ff:ff:ff
    inet 64.112.251.9/21 brd 64.112.255.255 scope global eno1
    inet6 fe80::5e26:aff:fe2b:2954/64 scope link
       valid_lft forever preferred_lft forever
17: veth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 9e:59:c1:cc:3a:e3 brd ff:ff:ff:ff:ff:ff
18: veth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 2e:5e:c1:b0:71:44 brd ff:ff:ff:ff:ff:ff

For some reason, it always adds two interfaces.

Set address for a virtual interface

# ip addr add 10.1.2.3/8 dev veth0
# ip ad
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 5c:26:0a:2b:29:54 brd ff:ff:ff:ff:ff:ff
    inet 64.112.251.9/21 brd 64.112.255.255 scope global eno1
    inet6 fe80::5e26:aff:fe2b:2954/64 scope link
       valid_lft forever preferred_lft forever
17: veth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 02:d1:8f:79:fe:db brd ff:ff:ff:ff:ff:ff
    inet 10.1.2.3/8 scope global veth0
18: veth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether ee:84:34:2c:ce:bc brd ff:ff:ff:ff:ff:ff
The link is still in the "down" state, and can't be used yet.

Set status of virtual interface to "up"

# ip link set up dev veth0
# ip ad
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 5c:26:0a:2b:29:54 brd ff:ff:ff:ff:ff:ff
    inet 64.112.251.9/21 brd 64.112.255.255 scope global eno1
    inet6 fe80::5e26:aff:fe2b:2954/64 scope link
       valid_lft forever preferred_lft forever
17: veth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 100
    link/ether 02:d1:8f:79:fe:db brd ff:ff:ff:ff:ff:ff
    inet 10.1.2.3/32 scope global veth0

Notice that the state is still listed as down, but ifconfig shows the same device with a state of up. Go figure.

# ifconfig veth0
veth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 10.1.2.3  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 02:d1:8f:79:fe:db  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Remove virtual interface

# ip link delete dev veth0

No comments:

Post a Comment