Use ip to control network interfaces
29 September 2006ip 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.
Let’s enable the wlan0 interface
We can observe that the syntax of ip is organized as follows:
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.
The following network interfaces are configured on my system.
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.
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.
Assigning an IP Address to an Interface.
Let’s assign the IP Address 192.168.0.101 to wlan0 interface.
Let’s delete the IP Address 192.168.0.101 from wlan0.
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.
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.
Let’s delete the previous static route.
Let’s add a route to the host 10.21.14.1 through the gateway 192.168.0.50
Let’s delete the previously added route.
Let’s add a default route through the gateway 192.168.0.50.
Let’s delete the default route.
Let’s display the routing table.
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.
No comments yet