Readers like you help support MUO. If you make a purchase through links on our site, we may receive an affiliate commission. Continue reading.
The most basic part of Linux administration and troubleshooting is checking a system’s IP configuration to ensure that the system has a valid IP and is accessible on the local network.
The ip command in Linux is a powerful tool that not only shows a system’s current IP address, but also allows you to view and manage the current configuration of network interfaces, IP addresses, routes, and ARP tables.
Let’s look at some of the most common use cases of the ip command in Linux.
Find the IP address on Linux
To find the IP address of your Linux system, use the ip command followed by address, addressor a Possibility:
This will show the configuration of all network interfaces including their IP addresses.
In the following example you can see that the system has three network interfaces: please refer (loopback address), ens33and ens37. The output contains the following information:
- Layer 1 information such as interface capability and physical layer connectivity state, MTU, operational state of the interface (e.g. UP or DOWN), and transmission queue length (qlen).
- Layer 2 information such as the MAC address of the interface.
- Layer 3 information includes the IP address and its type (dynamic IP addressing or static IP addressing).
To display brief information about network interfaces, use the ip command with the –meager Possibility as follows:
ip --brief address show
Use the following command to display only IPv4 address information:
ip -4 addr
Use the following syntax to find out the IP address information of a specific network interface:
ip address show dev [interface]
For example, to display the IP address of a network interface ens33the command would be:
ip address show dev ens33
View and change MAC address
You can also use the ip command to view and change your system’s MAC address.
Use the following command to view the MAC address of your Linux system:
ip --brief link show
To view the MAC address of a specific interface, use:
ip --brief link show dev [interface]
To change the MAC address, first bring down the interface:
sudo ip link set dev [interface] down
Then use the following command to change the MAC address of your interface:
sudo ip link set dev [interface] address [new-mac-adddress]
After that, call the interface:
sudo ip link set dev [interface] up
View network interface statistics
You can also use the ip command to display network interface statistics. Use the following ip command to view statistics for all network interfaces on your system:
ip -s link
The output includes statistics such as bytes/packets transmitted and received, errors, packets dropped, multicast, etc. To view statistics for a specific interface, use the following syntax:
ip -s link show dev [interface]
Change link properties with ip
To invoke an interface, use the following ip command:
sudo ip link set [interface] up
To shut down the interface, run:
sudo ip link set [interface] down
You can also change the MTU (maximum transmission unit) of an interface with the following command:
sudo ip link set mtu [number] dev [interface]
For example, to set the MTU of a network interface ens33 to 8000the command would be:
sudo ip link set mtu 8000 dev ens33
Add/remove IP address on Linux
To add an IP address to a network interface, use the following syntax:
ip addr add [ip-address] dev [interface]
How to add an IP address 192.168.42.140/24 to the network interface ens33the command would be:
ip addr add 192.168.42.140/24 dev ens33
Use the following command syntax to remove the IP address from an interface:
ip addr del [ip-address] dev [interface]
To remove the IP address 192.168.42.140/24 from the interface ens33the command would be:
ip addr del 192.168.42.140/24 dev ens33
View the routing table on Linux
The ip route command is used to view and change the routes in a Linux system. To view your system’s routing table, use the ip route command with no option:
Each line in the output represents a configured route. A route consists of a destination network address, a next hop (i.e. the IP address of the router), an interface over which the packet will be sent, and the metric (a value used to determine the preferred route if it multiple routes are available at the destination). The route with the lower metric is preferred in this case.
In the example above, the first two entries represent the default route used when no other route is available for the destination address. 192.168.42.2 and 192.168.10.1 are the IP addresses of the router. The developer ens33 and ens37 Specify the interface used to send the packets to the router. the proto dhcp Fields indicate that the default route is learned from DHCP.
The second entry represents the APIPA address (Automatic Private IP Addressing) 169.254.0.0/16. If a host does not obtain an IP address from a DHCP server, it will assign itself a random IP address from that network. This allows them to communicate with other hosts on the subnet that have also not been given an IP address.
The third and fourth entries show the network addresses of the local networks to which the system is connected. the 192.168.10.0 is the network, the interface ens37 is attached while the 192.168.42.0 is the network ens33 is attached.
the source Field specifies the IP address of the interface used as the source address when packets are sent over this route.
Change the routing table with ip
To add a route manually, use the ip route command followed by the destination network address and gateway IP:
sudo ip route add [network-id] via [gateway-ip]
For example, to add a route that directs all traffic to the 192.168.20.0 network to the gateway 192.168.10.16you would use the following command:
sudo ip route add 192.168.20.0/24 via 192.168.10.16
Use the following command to add a default route:
sudo ip route add default via [ip-address] dev [interface]
For example, to add the default route that forwards traffic to the router 192.168.10.1 by ens33the command would be:
sudo ip route add default via 192.168.10.1 dev ens33
ip route get [ip-address]
To delete a routing table entry, use the following syntax:
sudo ip route delete [network-address] via [gateway-ip]
You can also show the route an address will take using the following syntax:
ip route get [ip-address]
Manage neighbor table on Linux
You can use the… ip whinny Command in Linux to display and change the neighbor table, also known as the ARP table. Use the following command to display current neighbor table entries:
ip neigh show
To add a new entry in the neighbor table, use the following syntax:
sudo ip neigh add [ip-address] lladdr [mac-address] dev [interface]
Use the following syntax to remove an entry from the neighboring table:
sudo ip neigh del [ip-address] dev [interface]
Add color to the output of the ip command
To make the output easier and quicker to understand, you can add color to the ip command output using color -c Possibility:
ip -c a
Managing Networks and IP Addresses on Linux
The ip command in Linux is a useful tool for managing and troubleshooting network connections. From viewing network interfaces and changing connection properties to finding IP addresses and managing routes, you can use the ip command to perform several system administration tasks from the command line.
This article was previously published on Source link