Categories: How-To

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-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

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.

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.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.