Linux IP Command
Linux IP command is the newer version of the ifconfig command. It is a handy tool for configuring the network interfaces for Linux administrators. It can be used to assign and remove addresses, take the interfaces up or down, and much more useful tasks.
The basic syntax of the IP command is as follows:
The IP command supports the following command-line options:
-V or -Version: It is used to display the version of the IP command.
-h, -human, -human-readable: It is used to display the statistics in the form of human-readable values.
-b, -batch <FILENAME>: It is used to read and invoke commands from the given file or input. The failure may cause the termination of IP utility. The '-force' option will not let the IP terminated on errors in batch mode. If there were any errors during execution, the return code would be non zero.
-s, -stats, -statistics: It is used to display more information such as statistics or time values.
-d, -details: It is used to display the detailed output.
-l, -loops <COUNT>: It is used to specify a maximum number of loops.
-f, -family <FAMILY>: It is used to determine the protocol family. These protocol family identifiers may be inet, inet6, bridge, ipx, dnet, mpls, or link. Following shortcuts are used for the protocol family:
-o, -oneline: It is used to display each record on a single line. It replaces line fields with back-slash '\' character. It is useful for counting records with wc and grep command.
-r, -resolve: It is specified when we want to use the system's name resolver to print DNS names rather than host addresses.
-n, -netns <NETNS>: It is used to switch the IP to specified NETNS (network namespaces).
-a or -all: It is used to execute the given command over all objects.
-c, -color: It is used to display the colored output.
-t, -timestamp: It is used to display the current time.
-ts, -tshort: It is quite similar to the 'timestamp' option, but it uses a shorter format.
-rc, -rcvbuf<SIZE>: It is used to set the Netlink socket to receive buffer size, the defaults value is to 1MB.
-iec: It is used to display the human-readable rates in IEC units (e.g., 1Ki = 1024).
-br,-brief: It is used to display only essential information in a tabular format for better readability.
Useful IP - COMMAND SYNTAX
Some useful command syntax of the IP command is given in the below table.
The object name can also be written in an abbreviated form. For example, the address object can be written as addr or just a.
How does the IP command work?
The IP command manages how a Linux system handles the IP addresses, network interfaces controllers, and more. The changes made through the IP command take immediate action on the system; we do not need to reboot it.
The IP command has many other applications. It has many subcommands; each command works on a specific object like routes and IP addresses. It provides rich functionality.
The following are some useful objects used by the IP command:
Address: It contains IP addresses and ranges.
Link: It contains network interfaces, like connections and Wi-Fi adapters.
Route: It contains the protocols that manage the routing of traffic sent to addresses via interfaces (links).
Usage of the IP Command
Let's see the following usage of the IP command:
Listing the IP addresses
The essential utility of the IP command is to know the settings and addresses of our system. To display our computers IP addresses, execute the IP command with an address as follows:
The above command will display the settings and addresses of our system. However, we can also use one of the following commands for the same result:
All of the above commands will produce the same output. Consider the below output:
From the above output, we can see two IP addresses and a lot of related other information. Also, there is additional information about the interfaces displayed. The first IP address is the internal IP address, which is used to communicate locally. The second is the external IP address used on the LAN ( local area network).
Display the Ipv4 and Ipv6 Addresses
if you want only to display the Ipv4 address, execute the command as follows:
The above command will only display the IP version 4 address. Consider the below output:
To display the Ipv6 address, execute the command as follows:
The above command will only display the IP version 6 address. Consider the below output:
Display a Single Interface
To display the information of a single interface, use the "show dev" option with the interface name. For example, to view the details of the 'wlp6s0' interface, execute the command as follows:
The above command will display the output something like this:
We can also use the '-4' and '-6' flag to filter the output to display the Ipv4 and Ipv6 information related to the given interface.
Add an IP address to an Interface
The 'add' and 'dev' options are used to add an IP address to an interface. We have to specify the IP address and interface to be added. For example, add the IP address "192.168.7.22" to the 'wlp6s0' interface. To add this Ip address, execute the command as follows:
The above command will add the given IP address to the specified interface. If it is successfully added, it will not produce any output. Consider the below output:
To verify the added Ip address, execute the below command:
Consider the below output:
From the above output, we can see the given IP address is added to the specified interface.
Deleting an IP Address
Deleting an IP address is the same as adding, except we have to replace the 'add' option with the 'del.' To delete an IP, execute the command as follows:
The above command will delete the given IP from the 'wlp6s0' interface. Consider the below output:
IP with Network Interfaces
We can use the link object for working and inspecting the network interfaces. To display the installed interface on our system, execute the below command:
The above command will display the installed interface on our machine. Consider the below output:
Start or Stop a Network Interface
The 'set' option with up and down arguments is used to start and stop a network interface. Consider the below command:
The above command will down the 'wlp6s0' interface. To display the status of the interface, execute the below command:
Consider the below output:
To start the interface, execute the below command:
The above command will start the given interface. Consider the below output:
IP with Routes
The root object is used to inspect and manipulate routes. A route defines the forwarding process of network traffic and applied network interface. In the case of a shared network, the sending device can forward the packet directly. However, if the destination device is not directly connected, the sending device sends the packet to the default router. In this condition, the router deals with packets.
To display the defined routes of our system, execute the below command: