How to Use the 'ip address' Command (with Examples)
The ip address
command is part of the larger ip
suite of commands used to manage network configurations on Linux systems. This command specifically deals with managing IP addresses and related attributes of network interfaces. It allows you to view, assign, and remove IP addresses from your network devices, making it a critical tool for system and network administrators. More detailed information about the ip address
command can be found at manned.org/ip-address
.
Use case 1: List all network interfaces and their associated IP addresses
Code:
ip address
Motivation:
Knowing the IP addresses and network interfaces available on a system is crucial for network management, troubleshooting connectivity issues, and ensuring that all devices are configured correctly. By listing these interfaces, administrators can obtain an overview of all network interfaces, their configurations, and their statuses, which is invaluable for both routine audits and diagnosing network-related problems.
Explanation:
ip
: The main command to interact with network interfaces and routing configurations.address
: A subcommand used to manage IP addresses on network interfaces.
Example output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether XX:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86400sec preferred_lft 86400sec
Use case 2: Filter to show only active network interfaces
Code:
ip address show up
Motivation:
It’s often helpful to focus only on active network interfaces, especially when you’re troubleshooting network problems or optimizing network performance. Active interfaces are those currently in use or ready to transmit and receive data, indicating which connections are currently operational.
Explanation:
show
: A primary command option that displays detailed information.up
: A filter applied to theshow
option to restrict output to only interfaces that are active (up).
Example output:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether XX:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86400sec preferred_lft 86400sec
Use case 3: Display information about a specific network interface
Code:
ip address show dev eth0
Motivation:
Sometimes, you need specific information about a single network interface, such as when configuring or troubleshooting it. By isolating one interface, you gain a clear view of its settings, which helps minimize confusion and potential errors.
Explanation:
show
: Instructs the command to display information.dev
: Specifies the device (network interface) to focus on.eth0
: The name of the specific network interface you’re interested in.
Example output:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether XX:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86400sec preferred_lft 86400sec
Use case 4: Add an IP address to a network interface
Code:
ip address add 192.168.1.100/24 dev eth0
Motivation:
Assigning an IP address to a network interface is a fundamental step in setting up a device for network communication. Whether you’re configuring a new server or adjusting settings in an existing network, being able to quickly and accurately assign IP addresses ensures that devices can communicate over the network efficiently.
Explanation:
add
: The operation to insert a new IP address.192.168.1.100/24
: The IP address being assigned, along with its CIDR (Classless Inter-Domain Routing) notation subnet mask, which indicates the network segment.dev
: Indicates the device on which the IP address is being added.eth0
: The network interface to which the IP address is assigned.
Example output:
No direct output is generated. However, if the command is successful, the IP address will be displayed in the ip address
list for eth0
.
Use case 5: Remove an IP address from a network interface
Code:
ip address delete 192.168.1.100/24 dev eth0
Motivation:
Removing an IP address might be necessary when reconfiguring a network, decommissioning a server, or resolving IP conflicts. This command ensures that no stale or conflicting IP assignments exist, which can prevent communication breakdowns in a networked environment.
Explanation:
delete
: The operation to remove an existing IP address.192.168.1.100/24
: The IP address and subnet mask being removed.dev
: Specifies the network device affected by the change.eth0
: The network interface from which the IP is removed.
Example output:
Like with adding, no direct output is generated upon successful execution, but the IP address will no longer appear for eth0
in the ip address
list.
Use case 6: Delete all IP addresses in a given scope from a network interface
Code:
ip address flush dev eth0 scope global
Motivation:
In network management, you may need to reset the configuration of a network interface, which includes clearing out all current IP assignments. Flushing IP addresses by scope is a fast and efficient way to ensure that configurations are reset, thus preparing the interface for new settings without residual data or addresses potentially causing issues.
Explanation:
flush
: A command to clear all IP addresses from a specified interface.dev
: Denotes the target network device.eth0
: The network interface in question.scope
: Defines the scope or range of IP addresses to flush. Options includeglobal
,host
, orlink
.
Example output:
No direct output is produced, but executing ip address show dev eth0
will show that no global IP addresses are assigned to eth0
.
Conclusion:
The ip address
command is a powerful utility in Linux for managing network configurations. It provides granular control over how IP addresses are assigned, viewed, and removed from network interfaces. By mastering these use cases, system administrators can better manage network setups, optimize network performance, and troubleshoot connectivity issues efficiently.