ip Command in Linux: Add IP, Configure Routes & Network Interfaces (Examples)

ip Command in Linux: Add IP, Configure Routes & Network Interfaces (Examples)

The ip is a very powerful utility that is used to perform many tasks. Multiple older utilities such as ifconfig and route command were earlier used to perform these functions. In this article I will share different examples to configure network interfaces and routes using ip command.

IMPORTANT

Changes made using the ip command are temporary and will not persist after a reboot.

This command is mainly used for:

  • Temporary network configuration
  • Rescue or emergency mode
  • Troubleshooting connectivity issues

To make changes permanent, use tools like nmcli command or edit network configuration files.


Quick Cheat Sheet: ip Command

TaskCommand
Show all IP addressesip addr show
Show IP of specific interfaceip addr show dev eth0
Show only IPv4 addressip -4 addr show
Show only IP (clean output)ip -4 addr show eth0 | grep inet
List all interfacesip link show
Bring interface upip link set eth0 up
Bring interface downip link set eth0 down
Change MAC addressip link set dev eth0 address 00:11:22:33:44:55
Rename interfaceip link set eth0 name eth1
Add IP addressip addr add 192.168.1.10/24 dev eth0
Add multiple IPsip addr add 192.168.1.11/24 dev eth0
Replace IP addressip addr replace 192.168.1.20/24 dev eth0
Delete IP addressip addr del 192.168.1.10/24 dev eth0
Remove all IPsip addr flush dev eth0
Show routing tableip route show
Add default routeip route add default via 192.168.1.1
Delete default routeip route delete default
Add routeip route add 192.168.2.0/24 via 192.168.1.1
Replace routeip route replace 192.168.2.0/24 via 192.168.1.1
Delete routeip route delete 192.168.2.0/24
Check route for IPip route get 8.8.8.8
Show interface statisticsip -s link show
Flush routing cacheip route flush cache

Show and Verify IP Address

Show All Interfaces and IP Addresses

Use the following command to display all network interfaces along with their assigned IP addresses:

bash
ip addr show

Show IP of Specific Interface

To display IP address details for a specific interface:

bash
ip addr show dev eth0

Show Only IP Address

To display only the IP address without extra details:

bash
ip -4 addr show eth0 | grep inet

List Interfaces Based on Type

To filter interfaces based on type such as bridge, bond, or vlan:

bash
ip addr show type bridge
ip addr show type bond

Manage IP Address on Interface

Add IP Address to Interface

Assign an IP address to a network interface:

bash
ip addr add 192.168.1.10/24 dev eth0

Add Multiple IP Addresses

You can assign multiple IP addresses to the same interface:

bash
ip addr add 192.168.1.11/24 dev eth0
ip addr add 192.168.1.12/24 dev eth0

Replace Existing IP Address

Replace an existing IP address with a new one:

bash
ip addr replace 192.168.1.20/24 dev eth0

Remove IP Address from Interface

Delete a specific IP address from an interface:

bash
ip addr del 192.168.1.10/24 dev eth0

Remove All IP Addresses (Flush)

Remove all assigned IP addresses from an interface:

bash
ip addr flush dev eth0

Manage Network Interface State

List All Network Interfaces

To list all available network interfaces on the system:

bash
ip link show

Bring Interface Up or Down

Bring an interface up:

bash
ip link set eth0 up

Bring an interface down:

bash
ip link set eth0 down

Change MAC Address of Interface

To change the MAC address of an interface:

bash
ip link set dev eth0 address 00:11:22:33:44:55

Rename Network Interface

To rename a network interface:

bash
ip link set eth0 name eth1

Configure Routing in Linux

Show Routing Table

Display the current routing table using ip route command:

bash
ip route show

Add Default Route

Add a default gateway:

bash
ip route add default via 192.168.1.1

Delete Default Route

Remove the default route:

bash
ip route delete default

Add or Replace Route

Add a new route:

bash
ip route add 192.168.2.0/24 via 192.168.1.1

Replace an existing route:

bash
ip route replace 192.168.2.0/24 via 192.168.1.1

Check Route for Specific IP

Check which route will be used for a destination:

bash
ip route get 8.8.8.8

Real-World Scenarios

Add IP Address to Interface in Linux (Step-by-Step)

  1. Identify the interface:
bash
ip link show
  1. Assign IP address:
bash
ip addr add 192.168.1.10/24 dev eth0
  1. Verify configuration:
bash
ip addr show dev eth0

Bring Network Interface Up After Boot Issue

If a network interface is down after boot:

bash
ip link set eth0 up

Verify status:

bash
ip link show eth0

Temporary Network Setup in Rescue Mode

Assign temporary IP and gateway:

bash
ip addr add 192.168.1.10/24 dev eth0
ip route add default via 192.168.1.1

Assign Multiple IPs for Applications or Services

Assign multiple IP addresses to the same interface:

bash
ip addr add 192.168.1.10/24 dev eth0
ip addr add 192.168.1.11/24 dev eth0

Verify assigned IPs:

bash
ip addr show dev eth0

Fix Common Errors and Issues

This error occurs when a route or configuration already exists.

Fix by deleting the existing route before adding a new one:

bash
ip route delete default
ip route add default via 192.168.1.1

You can also use replace instead of add:

bash
ip route replace default via 192.168.1.1

Cannot Assign Requested Address

This error appears when the IP address is invalid or the interface is not ready.

Fix:

  • Verify the interface name
  • Ensure the interface is up
  • Check subnet and prefix
bash
ip link set eth0 up
ip addr add 192.168.1.10/24 dev eth0

Interface Not Found Error

This happens when the interface name is incorrect.

List available interfaces:

bash
ip link show

Then use the correct interface name in your command.

ip command not persistent after reboot

Changes made using the ip command are temporary and will not persist after reboot.

To make changes permanent:

  • Use NetworkManager tools like nmcli
  • Modify network configuration files based on your distribution

Frequently Asked Questions

1. How do I add an IP address in Linux using ip command?

Use ip addr add 192.168.1.10/24 dev eth0 to assign an IP address to a network interface.

2. How do I bring a network interface up in Linux?

Use ip link set eth0 up to bring the interface up.

3. What is the difference between ip and ifconfig?

The ip command is a modern replacement for ifconfig, providing more advanced and flexible networking features.

4. Why does ip command not persist after reboot?

Changes made using the ip command are temporary and reset after reboot unless configured using persistent network settings tools.

Conclusion

The ip command is a powerful and modern utility to manage network interfaces, IP addresses, and routing in Linux. It replaces older tools like ifconfig and route with a unified and flexible approach.

By understanding common commands, real-world scenarios, and troubleshooting techniques, you can efficiently configure and debug network settings in Linux environments.


Official Documentation

For more details, refer to the official manual:

ip command man page

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.