fbpx Linux ip Command: How to View IP and Configure Network | Certificação Linux

Linux ip Command: Show IP, Configure Network (ip addr, ip route)

📅 01/01/2019 ⏱ 4 min ✍️ Uira Ribeiro
Linuxcommands
Linux ip Command: Show IP, Configure Network (ip addr, ip route)
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The ip command in Linux is a powerful tool to show and manipulate network interfaces, configure IP addresses, modify routing tables, and create tunnels.

With this command it is possible to insert or modify entries in the routing table, add or modify the default route, and also configure network addresses.

This command replaces the Net-Tools commands ifconfig and route.

The objects that the ip command can work with are: link, addr, addrlabel, route, rule, neigh, ntable, tunnel, tuntap,  maddr,  mroute,  mrule,  monitor,  xfrm, netns, l2tp,  tcp_metrics, token and netconf.

In daily use, for a programmer, it is necessary to know how the objects addr (which configure the IP address), link (which enables an interface) and route (which modifies the routing table) work.

The first function of ip is to show the IP address of the host interfaces. Use the ip command followed by the addr object:

# ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536      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     2: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500     link/ether 00:26:55:04:d3:95 brd ff:ff:ff:ff:ff:ff    inet 172.19.1.34/20 brd 172.19.15.255 scope global enp0s25    inet6 fe80::226:55ff: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 interface eth1. You may or may not specify the network size in CIDR notation:

# ip addr add 192.168.50.5/24 dev eth1

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

# ip addr del 192.168.50.5/24 dev eth1

To enable an interface, we use the link object, followed by the word set, the interface name 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 routing table, you can use the route object:

# ip routedefault 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 routing table, the route object is also used, followed by the word add, the network you want to add, and the interface and gateway it is connected to:

# ip route add 10.10.20.0/24 via 172.19.1.10 dev enp0s25

In this example, the network 10.10.20.0/24 will be added whose gateway will be address 172.19.1.10, which is connected on interface enp0s25.

When listing the routing table again:

# ip route showdefault 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 logic applies to removing a route from the routing table:

# ip route del 10.10.20.0/24

It is also possible to add a default route to the routing table, using the route object, followed by add and the word default, followed by the default gateway IP address:

# ip route add default via 192.168.50.100

The ifconfig, route and netstat tools that are part of the legacy Net-Tools package have a parallel in the ip tool from iproute2, as follows:

Net-ToolsIproute2Description
ifconfig -aip link showDisplay 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 eth1Set IP/Network mask
ifconfig eth1 0ip addr del 192.168.0.1/24 dev eth1Remove IP/Network mask definition
ifconfig eth1ip addr show dev eth1Show specific information for an interface
ifconfig eth1 hw ether 00:52:bc:33:25:a1ip link set dev eth1 address 00:52:bc:33:25:a1Change MAC-ADDRESS
route -n  ou netstat -rnip route showDisplay routing table
route add default gw 192.168.0.1ip route add default via 192.168.0.1Add 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 default ip route del defaultDelete default route

Learn much more about Linux in our online course. You can enroll 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

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp