Linux ip command (manipulates network interface) [Basic Guide]

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:

# ip addr 
1: lo: mtu 65536
link/loopback 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

2: enp0s25: mtu 1500

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:

# ip addr add 192.168.50.5 dev eth1

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:

# ip addr add 192.168.50.5/24 dev eth1

The same idea can be used to delete an IP address from a given interface, with the addr object and the del command:

# ip addr of 192.168.50.5/24 dev eth1

To enable an interface, we use the link object, followed by the word set, the name of the interface, and the up command:

# ip link set eth1 up

To disable an interface, the same, but with the down command:

# ip link set eth1 down

To check the host route table, you can use the route object:

# ip route default via 172.19.1.1 dev enp0s25 172.19.0.0/20 dev enp0s25 proto kernel scope link src 172.19.1.34

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:

# ip route add 10.10.20.0/24 via 172.19.1.10 dev enp0s25

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:

# ip route show default via 172.19.1.1 dev enp0s25 10.10.20.0/24 via 172.19.1.10 dev enp0s25 172.19.0.0/20 dev enp0s25 proto kernel scope link src 172.19.1.34

The same reasoning applies to removing a route from the route table:

# ip route of 10.10.20.0/24

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:

# ip route add default via 192.168.50.100

The ifconfig, route, and netstat tools that are part of the legacy Net-Tools package are paralleled by the iproute2 ip tool, as follows:

Add

Net-ToolsIproute2Description
ifconfig -aip link showDisplays all network interfaces
ifconfig eth1 upip link set up eth1Enable network interface
ifconfig eth1 downip link set down eth1Disable network interface
ifconfig eth1 192.168.0.1/24ip addr add 192.168.0.1/24 dev eth1Define IP/network mask
ifconfig eth1 0ip addr del 192.168.0.1 /24 dev eth1Remove IP defination/network mask
ifconfig eth1ip addr show dev eth1shows information specific to an ifconfig eth1 hw ether interface
00:52:bc: 33:25:a1ip link set dev eth1 address 00:52:bc: 33:25:a1Changes the MAC-ADDRESS
route -n or netstat -rnip route showDisplays the routing table
route add default gw 192.168.0.1ip route add default via 192.168.0.1the default route route
add -net 192.168.0.0/24 eth1ip route add 192.168.0.0/24 dev eth1Add a static route route
del -net 192.168.0.0/24ip route del 192.168.0.0/24Delete a static route route
del defaultip route del defaultDelete the default route

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?