Wednesday, February 20, 2013

Some useful command with iproute2

http://linuxaria.com/howto/useful-command-of-iproute2?lang=en


As I wrote more than 2 years ago, the network tools (often referred as net-tools) ifconfig, netstat and route that should be familiar to anyone that has worked with a terminal, have been deprecated in favour of the iproute2 suite from some years.
iproute2 is intended to replace this entire suite of legacy Unix networking tools that were previously used for the tasks of configuring network interfaces, routing tables, and managing the ARP table, but which have not been developed since 2001.
You can find some examples of the usage of the iproute commands on my articles about:
- Policy routing
- Socket Statistics on Linux
- MAC Address Managment on Linux
And today I want to share with you some of the most useful commands that you can use with this “new” suite of commands and the translation of some old commands that we were all used to use on the terminal.



Interface configuration

With this task you can set an interface up and configure an IP address over it, so 2 classic command with ifconfig were:
[root@myserver.com ~]# ifconfig eth0 up
[root@myserver.com ~]# ifconfig eth0 192.168.1.1 netmask 255.255.255.0
With iproute2, control of interfaces themselves – both physical and logical – is through the link subcommand. Bringing up eth0 can be done with
[root@myserver.com ~]# ip link set eth0 up
While to add an IP address to an interface you can use:
[root@myserver.com ~]# ip addr add 192.0.2.1/24 dev eth0
If you prefer you can use also this notation:
[root@myserver.com ~]# ip addr add 192.0.2.1 255.255.255.0 dev eth0
To verify the result, or just check which IP addresses are configured on your system you can use:
[root@myserver.com ~]#ip addr ls
[root@myserver.com ~]#ip addr show
[root@myserver.com ~]#ip addr ls eth0
The first 2 command gave exactly the same output, while the third just shows the IP of the eth0 device.

Creating ethernet alias

Assuming that your eth0 IP is 192.0.2.1 and you would like to create an alias eth0:0 with IP 192.0.2.2. You would use:
ifconfig eth0:0 192.0.2.2 up
Where the key was to put :number to indicate that the IP was an alias, with iproute2 you can simply use the same command :
[root@myserver.com ~]# ip addr add 192.0.2.2 255.255.255.0 dev eth0

Routing

What’s the modern alternative of the command route -n ?
A simple:
[root@myserver.com ~]# ip ro
The output is slightly different but you get exactly the same information:
[me@mydesktop ~] route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 wlan0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 wlan0
192.168.0.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan0
 
[me@mydesktop ~] ip ro
default via 192.168.0.1 dev wlan0  proto static 
169.254.0.0/16 dev wlan0  scope link  metric 1000 
192.168.0.0/24 dev wlan0  proto kernel  scope link  src 192.168.0.3  metric 9
And to add and remove routes you can use the syntax ip ro add|del destination via gateway, so to add and remove a route to the lan 10.0.0.0/16 I could use:
[me@mydesktop ~]# ip ro add 10.0.0.0/16 via 192.168.0.1
[me@mydesktop ~]# ip ro del 10.0.0.0/16 via 192.168.0.1


Find the Route to an IP Address

If you have multiple interfaces and switch between them (eth0 for work, wlan1 for home, tun0 for vpn) and you want to get the ip and gateway of the interface actually used to connect to an IP try this:
[root@myserver.com ~]# ip route get IP
So for example you could use the IP 8.8.8.8 (Google DNS server) to check which interface the computer will use:
[me@mydesktop ~] ip route get 8.8.8.8
8.8.8.8 via 192.168.0.1 dev wlan0  src 192.168.0.3
So to reach 8.8.8.8 my desktop uses wlan0, the gatway located at 192.168.0.1 and the private ip 192.168.0.3

Neighbours

In iproute2 there is also a subcommand equivalent to the traditional arp -na, useful to know the ARP table on a UNIX machine.
You can get the same result with iproute2 using ip neighbor, with ip n being the shortened extreme:
[me@mydesktop ~] ip neigh
192.168.0.1 dev wlan0 lladdr 00:18:4d:af:a0:64 REACHABLE
So what are you waiting for ?
It’s time to switch to the “new” iproute suite, the commands are easy and powerful !

No comments:

Post a Comment