tecnico conferenza

« PreviousNext »

Use ip to control network interfaces

29 September 2006

ip is a part of iproute2 package and is a kind of universal tool to configure interfaces, assign addresses, adding/deleting routes etc. The current trend is towards using ip and the use of ifconfig, route etc will be reduced. We’ll look at few of the tasks performed by ip. The complete documentation is available here.

Note that you must be root to use ip.


Enable/Disable an interface

Let’s disable the wlan0 interface.

#ip link set wlan0 down

Let’s enable the wlan0 interface

#ip link set wlan0 up

We can observe that the syntax of ip is organized as follows:

ip object command arguments

In the first command, the object was link i.e., the network interface, the command was set, and the arguments were wlan0 , up/down.


To display the Active/Inactive Interfaces.

#ip link show

The following network interfaces are configured on my system.

1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: wlan0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:06:25:48:c2:3f brd ff:ff:ff:ff:ff:ff
3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0

We can see all the active(lo, wlan0) and inactive(sit0) interfaces.

To display only active interfaces append up to the above command.

#ip link show up
1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: wlan0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:06:25:48:c2:3f brd ff:ff:ff:ff:ff:ff

We can see that only the active interfaces(lo, wlan0) are displayed.

Once again, observe that the object is link( network interface), and the command is show with up parameter.

To display only the interface in which you’re interested, append the interface name to the end of the command.

#ip link show wlan0
2: wlan0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:06:25:48:c2:3f brd ff:ff:ff:ff:ff:ff


Assigning an IP Address to an Interface.

Let’s assign the IP Address 192.168.0.101 to wlan0 interface.

#ip address add 192.168.0.101/24 dev wlan0 brd +

Let’s delete the IP Address 192.168.0.101 from wlan0.

ip address del 192.168.0.101/24 dev wlan0 brd +

Here, address is the object, add/del are commands, the rest are arguments. The /number notation is used to assign the subnet mask, dev specifies the interface name, brd + means assign the broadcast address by masking some bits from the interface address.


To display the IP Addresses

Let’s see the IP Addresses assigned to our interfaces.

#ip address show
1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever

2: wlan0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast
qlen 1000 link/ether 00:06:25:48:c2:3f brd ff:ff:ff:ff:ff:ff
inet 192.168.0.101/24 brd 192.168.0.255 scope global wlan0
inet6 fe80::206:25ff:fe48:c23f/64 scope link valid_lft forever preferred_lft forever

3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0

Note that to view only addresses of active interfaces append an up to the command. Also to only view the interfaces in which you’re interested specify the interface name along with the command.


To Add/Delete Static Routes to Networks and Hosts and Display the Routing Table

Let’s add a route to the network 10.121.13 through the gateway 192.168.0.50.

#ip route add 10.121.13.0/24 via 192.168.0.50

Let’s delete the previous static route.

#ip route del 10.121.13.0/24 via 192.168.0.50

Let’s add a route to the host 10.21.14.1 through the gateway 192.168.0.50

#ip route add 10.21.14.1/32 via 192.168.0.50

Let’s delete the previously added route.

#ip route del 10.21.14.1/32 via 192.168.0.50

Let’s add a default route through the gateway 192.168.0.50.

#ip route add default via 192.168.0.50

Let’s delete the default route.

#ip route del default via 192.168.0.50[

Let’s display the routing table.

#ip route show
192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.101 default via 192.168.0.50 dev wlan0

As you can see from the scenarios I’ve discussed ip is indeed a very powerful and modern utility when compared to ifconfig and family.

You can do a lot more with ip.

Posted in Network | Trackback | del.icio.us | Top Of Page

No comments yet

Leave a Reply