Linux ip command (manipulates network interface)
The IP Command in Linux is a powerful tool for showing and manipulating network interfaces, configuring IP addresses, changing route tables, and creating tunnels.
With this command it is possible to enter or change entries in the route table, add or modify the default route, and also configure network addresses.
This command replaces the Net-Tools ifconfig and route commands .
The objects with which the ip command can work are: link, addr, addrlabel, route, rule, neigh, ntable, tunnel, tuntap, maddr, mroute, mrule, monitor, xfrm, netns, l2tp, tcp_metrics, token, and netconf.
In everyday life, for a programmer, it is necessary to know how the addr (which configure the IP address), link (which enables an interface) and route (which modifies the route table) objects work.
The first function of IP is to show the IP address of the host interfaces. The ip command followed by the addr object is used:
link/ether 00:26:55:04:d 3:95 brd ff:ff:ff:ff:ff
inet 172.19.1.34/20 brd 172.19.15.255 global scope enp0s25
inet6 fe80:: 226:55 ff:fe04:d395/64 scope link The ip command can be used to assign an IP address to the host using the addr object, followed by the word add:
In this case, the IP 192.168.50.5 will be assigned to the eth1 interface. You may or may not enter the network size in the CIDR notation:
The same idea can be used to delete an IP address from a given interface, with the addr object and the del command:
To enable an interface, we use the link object, followed by the word set, the name of the interface, and the up command:
To disable an interface, the same, but with the down command:
To check the host route table, you can use the route object:
To add a static route to the route table, the route object is also used, followed by the word add, the network that you want to add and the interface and gateway to which it is connected:
In this example, the 10.10.20.0/24 network will be added whose gateway will be the address 172.19.1.10, which is connected to the enp0s25 interface.
When listing the route table again:
The same reasoning applies to removing a route from the route table:
It is still possible to add a default route to the route table using the route object, followed by add and the word default, followed by the IP address of the default gateway:
The ifconfig, route, and netstat tools that are part of the legacy Net-Tools package are paralleled by the iproute2 ip tool, as follows:
**Net-Tools** | **Iproute2** | **Description** |
ifconfig -a | ip link show | Displays all network interfaces |
ifconfig eth1 up | ip link set up eth1 | Enable network interface |
ifconfig eth1 down | ip link set down eth1 | Disable network interface |
ifconfig eth1 192.168.0.1/24 | ip addr add 192.168.0.1/24 dev eth1 | Define IP/network mask |
ifconfig eth1 0 | ip addr del 192.168.0.1 /24 dev eth1 | Remove IP defination/network mask |
ifconfig eth1 | ip addr show dev eth1 | shows information specific to an ifconfig eth1 hw ether interface |
00:52:bc: 33:25:a1 | ip link set dev eth1 address 00:52:bc: 33:25:a1 | Changes the MAC-ADDRESS |
route -n or netstat -rn | ip route show | Displays the routing table |
route add default gw 192.168.0.1 | ip route add default via 192.168.0.1 | the default route route |
add -net 192.168.0.0/24 eth1 | ip route add 192.168.0.0/24 dev eth1 | Add a static route route |
del -net 192.168.0.0/24 | ip route del 192.168.0.0/24 | Delete a static route route |
del default | ip route del default | Delete the default route |
Did you like it?
Share