The IP command is a networking command that provides information about network interfaces and also performs network configuration. For instance, you can manually assign an IP address, view all network interfaces, manage the routing table, enable or disable a network interface, and so on.
In this tutorial, we examine various use cases of the Linux IP command using some practical examples.
Basic Syntax
The IP command takes the following syntax:
$ ip [ OPTIONS ] OBJECT { COMMAND | help }
Display information about network interfaces
One of the common uses of the IP command is to display the system’s network interface. You can use the command below to display information about all the system’s network interfaces including the IP address, mac address, link status ( whether up or down ), and MTU ( Maximum Transmission Unit )
$ ip addr
OR
$ ip a
As mentioned, the commands provide detailed information about the attached network interface as indicated,
Display IPv4 and IPv6 addresses only
You can choose to list either IPv4 or IPv6 addresses as shown. To display all the IPv4 addresses of network interfaces, run the command:
$ ip -4 a
To list IPv6 addresses of all the interfaces, execute:
$ ip -6 a
Display information about a specific network interface
So far, we have displayed information about all the network interfaces. But what if you are not interested in getting information about one interface. How do you go about doing it?
To display information about an interface, use the syntax:
$ ip a show dev interface_name
For instance, to gather information on the enp0s3 interface only, run the command:
$ ip a show dev enp0s3
OR
$ ip a list enp0s3
Assign an IP address to an interface
Let’s now focus on how you can assign an IP address to an interface. To do so, use the command syntax:
$ ip a add {ip_addr/mask} dev {interface}
For example, to assign the enp0s3 interface an IP of 192.168.2.120 with 255.255.255.0 subnet mask, run the command.
$ sudo ip a add 192.168.2.120/255.255.255.0 dev enp0s3
Or you can use the /24 CIDR notation for the subnet mask as follows
$ sudo ip a add 192.168.2.120/24 dev enp0s3
Delete an IP address from a network interface
To delete the IP address assigned to the interface, run the command:
$ sudo ip a del 192.168.2.120/255.255.255.0 dev enp0s3
You can then verify the changes as follows:
$ ip a show dev enp0s3
Bring an interface UP or DOWN
To modify the state of a network interface, by enabling or disabling it, run the command syntax
$ sudo ip link set dev DEVICE_NAME {up|down}
To bring down the enp0s3 interface, run:
$ sudo ip link set dev enp0s3 down
To bring it up, execute:
$ sudo ip link set dev enp0s3 up
Display Routing table
To display the routing table of your Linux system, run the command
$ ip r
Conclusion
That was a summary of the practical use cases of the Linux IP command. Those are some of the widely used command examples that Linux users run from time to time.