Linux ip Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful ip command in Linux to manage network interfaces and troubleshoot network issues. The lab covers the basic syntax and options of the ip command, as well as practical examples of managing network interfaces and using the command to diagnose network problems. You will explore commands to display information about network interfaces, configure IP addresses, and work with routing tables. This lab is designed to provide you with a solid understanding of the ip command and its practical applications in network administration and troubleshooting.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("`Network Configuring`") linux/RemoteAccessandNetworkingGroup -.-> linux/netstat("`Network Monitoring`") linux/RemoteAccessandNetworkingGroup -.-> linux/ping("`Network Testing`") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("`IP Managing`") subgraph Lab Skills linux/ifconfig -.-> lab-422741{{"`Linux ip Command with Practical Examples`"}} linux/netstat -.-> lab-422741{{"`Linux ip Command with Practical Examples`"}} linux/ping -.-> lab-422741{{"`Linux ip Command with Practical Examples`"}} linux/ip -.-> lab-422741{{"`Linux ip Command with Practical Examples`"}} end

Understand the ip Command Syntax and Options

In this step, you will learn about the basic syntax and options of the ip command in Linux. The ip command is a powerful tool for managing network interfaces and configurations.

First, let's explore the general syntax of the ip command:

ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]

Here, OBJECT represents the network entity you want to manage, such as link for network interfaces, addr for IP addresses, route for routing tables, and so on. COMMAND is the specific action you want to perform on the object, and ARGUMENTS are any additional parameters required by the command.

Some common ip command options include:

  • -c: Enables color output
  • -f: Specifies the address family (e.g., inet, inet6, link)
  • -h: Displays the help menu
  • -s: Provides detailed, verbose output
  • -o: Outputs the information in a single-line format

Let's try a few examples to get familiar with the ip command:

## Display information about all network interfaces
sudo ip link show

Example output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff

This command shows the status and configuration of all network interfaces on the system.

## Display IP address information for a specific interface
sudo ip addr show eth0

Example output:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
       valid_lft forever preferred_lft forever

This command displays the IP address and other configuration details for the eth0 interface.

Manage Network Interfaces with the ip Command

In this step, you will learn how to manage network interfaces using the ip command.

First, let's create a new network interface using the ip link add command:

sudo ip link add name myint type dummy

This creates a new dummy interface named myint. You can verify the interface was created using the ip link show command:

sudo ip link show

Example output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
3: myint: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 3a:f9:b6:5a:4a:48 brd ff:ff:ff:ff:ff:ff

Next, let's bring the myint interface up using the ip link set command:

sudo ip link set dev myint up

You can verify the interface is now in the UP state:

sudo ip link show myint

Example output:
3: myint: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noop state UP mode DEFAULT group default qlen 1000
    link/ether 3a:f9:b6:5a:4a:48 brd ff:ff:ff:ff:ff:ff

Finally, let's delete the myint interface using the ip link delete command:

sudo ip link delete dev myint

Verify the interface has been removed:

sudo ip link show

Example output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff

The myint interface is no longer present in the output.

Troubleshoot Network Issues Using the ip Command

In this step, you will learn how to use the ip command to troubleshoot network issues.

Let's start by checking the routing table using the ip route command:

sudo ip route show

Example output:
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2

This shows the default route and the route for the local network interface.

Next, let's simulate a network issue by disabling the eth0 interface:

sudo ip link set dev eth0 down

Now, try to ping a remote host:

ping 8.8.8.8

You should see that the ping fails, as the network interface is down.

To troubleshoot further, you can use the ip addr command to check the IP address configuration:

sudo ip addr show eth0

Example output:
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
       valid_lft forever preferred_lft forever

The output shows that the eth0 interface is in the DOWN state.

To bring the interface back up, use the ip link set command:

sudo ip link set dev eth0 up

Verify the interface is now in the UP state:

sudo ip addr show eth0

Example output:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
       valid_lft forever preferred_lft forever

Now, try pinging the remote host again, and the ping should succeed.

Summary

In this lab, you learned about the basic syntax and options of the ip command in Linux, which is a powerful tool for managing network interfaces and configurations. You explored the general syntax of the ip command, including the use of OBJECT, COMMAND, and ARGUMENTS. You also learned about common ip command options, such as -c for color output, -f for specifying the address family, and -s for detailed, verbose output. You then practiced using the ip command to display information about network interfaces and IP addresses, which can be helpful for troubleshooting network issues.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like