The IP command in Linux is a powerful tool for managing and configuring the network interfaces of a system. It allows users to view information about the network interfaces, configure IP addresses, routes, and other networking parameters, and perform various other networking-related tasks. With the IP command, administrators can easily perform common networking tasks, such as assigning IP addresses to interfaces, creating and managing virtual network interfaces, and configuring routing tables, without having to use separate networking utilities.
This command is the part of iproute2 package. With the help of ip
command, the system administrators assign an address to a network interface and/or configure network parameters on an interface.
In this tutorial, you will learn about the uses of the Linux ip command with useful examples.
How to Use ip Command
The modern Linux systems provide iproute2
package, which replaces the various commands provided by the net-tools package. Also, the `ip` command comes under this package, with great features.
Syntax:
The `ip` command uses the following syntax:
1 | ip [ OPTIONS ] OBJECT { COMMAND | help } |
The frequently used OBJECTS (or subcommands) with their short abbreviation:
- link (l): display and modify network interfaces.
- address (addr/a): display and modify IP addresses.
- route (r): display and modify the routing table.
- neigh (n): display and manipulate neighbor objects (ARP table).
The changes made from the command line like IP addresses, routes, and policy routing rules (and so on) are not persistent. To make changes permanently, you need to edit the appropriate configuration files.
Get Help
The `ip`
` command provides many other OBJECTS (or subcommands) to manage networking in a Linux system. To see a full list type in the following command:
ip help
Output:Usage: ip [ OPTIONS ] OBJECT { COMMAND | help } ip [ -force ] -batch filename where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila | vrf | sr | nexthop } OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] | -h[uman-readable] | -iec | -j[son] | -p[retty] | -f[amily] { inet | inet6 | mpls | bridge | link } | -4 | -6 | -I | -D | -M | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -br[ief] | -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] | -rc[vbuf] [size] | -n[etns] name | -N[umeric] | -a[ll] | -c[olor]}
In addition, use the help command to get object-specific help, like:
ip addr help
Similarly, try the below command to get help for the route object subcommand.
ip route help
Try the help command with other objects to get help.
Next, you will learn about the uses of the `ip`
command. For the learning purpose, use a system physically accessible. In other words, any wrong command may down the network interface and you may disconnect from the remote systems. But, the physical system still is accessible.
View and Manage Network Interfaces
Use link
subcommand with ip
command to manage and display the state of all network interfaces. The Name of the network interfaces may differ based on the Linux distributions and hardware platforms etc.
- The
ip link
command shows the details about all interfaces.ip link
[OR]ip link show
- Define the network interface name to view information about the specific one.
ip link show eth0
- Using the
-s
option displays the network interface statics. It shows all details about the packet and data sent and receive on that interface.ip -s link show eth0
Remove the interface name (eth0) to view statics for all the available network interfaces.
- Use the
ip link set
command to bring the network interface up and down. Hereup
is used to bring the network interface online.ip link set eth0 up
Use
down
option to bring the network interface offlineip link set eth0 down
Note: While working with remote systems, don’t bring down the interface. It may disconnect your system from the network.
Display and Add IP Address on Network Interfaces
The ip addr
subcommand is used for displaying the IP address configured on network interfaces. You can also use the same command to set an IP address on a network interface.
- The default
addr
subcommand displays the IP address details on all interfaces. You can also includeshow
is optional, to get the same results.ip addr
[OR]ip addr show
You can also display only
IPv4
orIPv6
ip addresses by usingip -4 addr
orip -6 addr
commands. - Specify the interface name with
ip addr show
subcommand to display IP address of a specific interface. For example, view the IP address on the eth0 network interface:ip addr show dev eth0
- The
ip addr add
subcommand is used to add an address on a network interface. The below command will assign ip address192.168.10.100
with netmask24
on eth0 network interface.ip addr add 192.168.10.100/24 dev eth0
- Assigning the multiple IP addresses to a single network interface
ip addr add 192.168.10.100/24 dev eth0
ip addr add 192.168.10.105/24 dev eth0
- Use the
ip addr del
subcommand to delete an IP address from any network interface. For example, use the following command to remove IP address192.168.10.105
with 24 subnet masks from the eth0 interfaceip addr del 192.168.10.105/24 dev eth0
Display and Alter the Routing Table
The ip
command also provides you the option to view or change the routing table of any network interface.
Use ip route
subcommand to work with the routing tables.
- List the kernel routing table. Use
route
subcommand to list all the routing entries in the kernel.ip route
Output:default via 192.168.10.1 dev eth0 proto static 192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.1The above results show that
192.168.10.1
is the default gateway used by the kernel via the eth0 network interface. - Add a default route (for all addresses) via the local gateway 192.168.10.1 that can be reached on device eth0
ip route add default via 192.168.10.1 dev eth0
- Add a route to 192.168.10.0/24 via the gateway at 192.168.10.1
ip route add 192.168.10.0/24 via 192.168.10.1
- Add a route to 192.168.10.0/24 that can be reached on device eth0
ip route add 192.168.10.0/24 dev eth0
- Use the following command to delete the route for 192.168.10.0/24 via the gateway at 192.168.10.1
ip route delete 192.168.10.0/24 via 192.168.10.1
- Replace an exiting route defined for 192.168.10.0/24 to use device eth0
ip route replace 192.168.10.0/24 dev em1
- Display the route for a destination Ip address. This command is very useful during the troubleshooting of the network. In other words, you can find the route used by the system to reach a defined destination IP address.
ip route get 8.8.8.8
Conclusion
In conclusion, the IP command in Linux is an indispensable tool for managing and configuring a system’s network interfaces. Its versatility, ease of use, and powerful features make it a popular choice among system administrators. Whether you need to configure IP addresses, set up virtual interfaces, or manage routing tables, the IP command has you covered. With a little bit of knowledge and practice, you too can take advantage of all that the IP command has to offer and become a more efficient and effective network administrator.