A Linux host with several uplinks may need separate routes for management and application traffic. A virtual routing and forwarding (VRF) device created with iproute2 gives its member interfaces a dedicated routing table.

When an interface joins a VRF, Linux moves its directly connected and local routes into the VRF table. Higher-priority routing policy rules can still direct lookups to another table.

The changes apply to the running kernel. A reboot clears them unless the system's network configuration recreates the VRF and its interface assignments.

Work from a console with sudo access on a Linux host with VRF support. Use a spare, standalone interface with no global IPv6 addresses and no IPv6 routes you need to keep. Reserve an unused VRF name and a numeric table ID with no existing routes, policy rules, or VRF assignments.

Steps to create a VRF with iproute2:

  1. Confirm that your spare interface is up with an IPv4 address, using its name in place of eth1 throughout.
    $ ip -4 address show \
      dev eth1

    The interface flags should include UP, and your IPv4 address should appear on an inet line. Stop if either is missing or the address has a /32 prefix or the noprefixroute flag.

  2. Confirm that eth1 has a directly connected route for the IPv4 subnet from step 1.
    $ ip -4 route show \
      table all dev eth1

    The subnet route should show proto kernel scope link. Stop if the route is missing or the subnet has no other usable address. If the list contains routes beyond the connected subnet and its local and broadcast entries, stop and plan how to migrate them.

  3. Create the VRF using your unused name and table ID in place of vrf-blue and 100 throughout.
    $ sudo ip link add \
      dev vrf-blue type vrf \
      table 100

    If the name already exists, choose a free name and rerun this step.

  4. Bring the vrf-blue device up.
    $ sudo ip link set \
      dev vrf-blue up
  5. Add an unreachable default route to the VRF's IPv4 table.
    $ sudo ip -4 route add \
      table 100 \
      unreachable default \
      metric 4278198272

    The unreachable default stops lookups that reach table 100 without a more specific match.

  6. Attach eth1 to vrf-blue.

    Attaching or detaching brings the interface down and back up. This can disconnect sessions, remove dependent routes, and drop global IPv6 addresses. To detach the interface, run sudo ip link set dev eth1 nomaster from the console with your interface name. Detaching restores only the connected and local routes regenerated from addresses the interface still has.

    $ sudo ip link set \
      dev eth1 master vrf-blue
  7. Look up a route in vrf-blue using a different usable IPv4 address in the subnet from step 2 in place of 192.0.2.1.
    $ ip -4 route get \
      192.0.2.1 vrf vrf-blue
    192.0.2.1 dev eth1 table 100 src 192.0.2.10 uid 1000
        cache

    A matching result shows your interface in dev and your table ID or name in table. If the lookup fails or either field differs, check the interface assignment and routing rules before rerunning it.

Share this guide