Vanstechelman.eu
   

iproute2 instead of ifconfig/route

For many, ifconfig and route are still the preferred commands for configuring a network through the command line. However, in modern network environments, ifconfig has its drawbacks. And as you would expect from a Free Software community, improved packages have been developed. iproute2 is one of them and is getting increasingly popular.

The default command to work with iproute2 is ip. Clean, simple to remember and extremely powerful. But its power is well described in many documents, including the iproute2 document[1] and the Guide to IP Layer Network Administration with Linux[2]. In this short introduction, we'll stay with the simplest basics that most people use just to show you how easy it is to "migrate" from ifconfig (sys-apps/net-tools) to ip (sys-apps/iproute2).

  1. http://www.policyrouting.org/iproute2.doc.html
  2. http://linux-ip.net/html/

To configure a host to use IP address 192.168.0.102, netmask 255.255.255.0 and default gateway 192.168.0.1, the "old" commands were:

+-------------------------------------------------------------------------+
| Code Listing 5.1:                                                       |
| Using ifconfig and route                                                |
+-------------------------------------------------------------------------+
|                                                                         |
|# ifconfig eth0 192.168.0.102 netmask 255.255.255.0 up                   |
|# route add default gw 192.168.0.1                                       |
|                                                                         |
+-------------------------------------------------------------------------+

Using iproute2, this becomes:

+-------------------------------------------------------------------------+
| Code Listing 5.2:                                                       |
| Using iproute2's ip command                                             |
+-------------------------------------------------------------------------+
|                                                                         |
|# ip address 192.168.0.102/24 dev eth0                                   |
|# ip route add default via 192.168.0.1                                   |
|                                                                         |
+-------------------------------------------------------------------------+

The syntax isn't all that difficult, is it? Let's take a look at our current routing table. With route you would run route -n:

+-------------------------------------------------------------------------+
| Code Listing 5.3:                                                       |
| Using route                                                             |
+-------------------------------------------------------------------------+
|                                                                         |
|# route -n                                                               |
|Kernel IP routing table                                                  |
|Destination Gateway Genmask Flags Metric Ref Use Iface                   |
|192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0                           |
|127.0.0.0 127.0.0.1 255.0.0.0 UG 0 0 0 lo                                |
|0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0                                |
|                                                                         |
+-------------------------------------------------------------------------+

With ip, you ask it to show the routes:

+-------------------------------------------------------------------------+
| Code Listing 5.4:                                                       |
| Using ip to show the routing table                                      |
+-------------------------------------------------------------------------+
|                                                                         |
|# ip route show                                                          |
|192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.121        |
|127.0.0.0/8 via 127.0.0.1 dev lo scope link                              |
|default via 192.168.0.1 dev eth0                                         |
|                                                                         |
+-------------------------------------------------------------------------+

You might find this output strange; however, it gives a lot of useful information. For instance, scope link means that the network is reachable while proto kernel informs us that the kernel has added this routing as part of bringing the interface up.

Using ip within Gentoo isn't difficult either. The Gentoo
sys-apps/baselayout package supports both formats (ifconfig and ip):

+-------------------------------------------------------------------------+
| Code Listing 5.5:                                                       |
| Configuring the network through /etc/conf.d/net                         |
+-------------------------------------------------------------------------+
|                                                                         |
|(Old-style configuration)                                                |
|config_eth0=( "192.168.0.102 netmask 255.255.255.0" )                    |
|routes_eth0=( "default gw 192.168.0.1" )                                 |
|                                                                         |
|(Using iproute2 -- don't forget to emerge it first)                      |
|modules=( "iproute2" )                                                   |
|config_eth0=( "192.168.0.102/24" )                                       |
|routes_eth0=( "default via 192.168.0.1" )                                |
|                                                                         |
+-------------------------------------------------------------------------+

Note: For more /etc/conf.d/net magic, please read the commented file /etc/conf.d/net.example

That's it for now; have fun with Gentoo !