- 16 Linux ip command examples to configure network interfaces (cheatsheet)
- Install iproute rpm
- 1. Show IP Address of all the available interfaces using ip command
- 2. Show IP Address of individual interface
- 3. List interfaces based on interface type
- 4. Check if NIC’s link state is enabled or disabled
- 5. Change NIC’s Link state
- 6. Assign/Add IPv4 and IPv6 address
- 7. Un-Assign/Delete IPv4 and IPv6 address
- 8. Un-assign/delete all protocol addresses from individual interface
- 9. Display detailed statistics of interface
- 10. List available routes
- 11. Add default route to routing table
- 12. Delete default route from routing table
- 13. Add additional route to the routing table
- 14. Delete additional route from routing table
- 15. Replace existing routes from routing table
- 16. Display the route an address will take
- Conclusion
- References
- 10 Useful “IP” Commands to Configure Network Interfaces
- How Do I Configure Static IP Address Internet Protocol (IPv4)
- For RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux
- For Ubuntu/Debian/Linux Mint
- 1. How to Assign an IP Address to a Specific Interface
- 2. How to Check an IP Address
- 3. How to Remove an IP Address
- 4. How to Enable Network Interface
- 5. How to Disable Network Interface
- 6. How do I Check Route Table?
- 7. How do I Add Static Route
- 8. How to Remove Static Route
- 9. How do I Add Persistence Static Routes
- For RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux
- For Ubuntu/Debian/Linux Mint
- 10. How do I Add Default Gateway
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How to Set a Static IP Address in Linux
- Ubuntu
- Editing your interface file
- CentOS
- Using ip and netplan
- Show your IP using ip
- Show only one interface using ip
- Bring an interface up or down using ip
- Only show IPv4 interfaces
- Written By Daniel Miessler
16 Linux ip command examples to configure network interfaces (cheatsheet)
Table of Contents
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.
Install iproute rpm
By default iproute rpm is installed on all variants of RHEL/CentOS Linux, you can also find ip binary command in the rescue and emergency target.
The absolute path is /usr/sbin/ip
1. Show IP Address of all the available interfaces using ip command
Use ip addr show to show the IP Address of all the available network interfaces on your Linux system
Here I have three interfaces (including the loopback interface), wherein we can check the IP Address of individual interface as highlighted above.
2. Show IP Address of individual interface
You can use » dev » argument along with » ip addr show » to display information of provided interface only. Here we are displaying IP Address detail for enp0s9 interface only.
3. List interfaces based on interface type
There are various types of NIC which can be available on your Linux system such as vlan, bond, bridge, vxlan etc.
Now to list only bridge interfaces on your system:
To list bond interface from your system
4. Check if NIC’s link state is enabled or disabled
» ip link » will manage and display the state of all network interfaces. To display the current state of all the interfaces
To check the link state of individual interface
5. Change NIC’s Link state
Since our NIC interface is already in enabled state, let us turn down link state for enp0s9 interface
Check the link state again for enp0s9
Now since it is in DOWN state, let us change the link status for enp0s9 to UP
Check the link state again for enp0s9
6. Assign/Add IPv4 and IPv6 address
To assign ipv4 address using you can use » ip addr add «. To demo this step I have already temporarily unassigned/removed IP address from enp0s9 interface.
To assign IPv4 address using ip command you would need netmask prefix value which here for me is «24». You can either calculate this prefix value or you can use other online network calculators which can help you get the prefix value depending upon your netmask.
To check the assigned IPv4 Address for enp0s9 interface
To assign IPv6 address using ip command you can use the same command as used for IPv4 address but with the IPv6 address and netmask
Verify the IPv6 address assigned to the provided interface
7. Un-Assign/Delete IPv4 and IPv6 address
You can un-assign or delete an assigned ip address from an interface using » ip addr del » argument. Here you must give the exact IP address along with the prefix value to un-assign the ip address.
Check and verify if the ip address was un-assigned sucessfully from the provided interface
You can un-assign or delete the IPv6 address for individual interface using the same command as used for IPv4 address above.
Next check the IPv6 Address of the interface to make sure it is un-assigned from the provided interface
8. Un-assign/delete all protocol addresses from individual interface
By using » ip addr del » we only remove the provided address assigned to the respective interface. But if you wish to remove all the protocol address assigned to individual interface then you must use flush
Next check the interface detail
As you see both IPv4 and IPv6 addresses are removed.
9. Display detailed statistics of interface
To get more detailed information on packet statistics for individual interface using » -s » argument
If the «-s» option appears twice or more, the amount of information increases.
10. List available routes
To list all the available routes on your Linux system
You can also use » ip route » or » ip route show » to get the list of available routes in the routing table
11. Add default route to routing table
To add default route (adding an entry in the routing table) use » ip route add default «
Verify the available default routes in the routing table
To add more details for the default route as we have for other routes
Verify the default route again using ip command
12. Delete default route from routing table
To delete the default route, instead of using » add «, use » delete «. Now here I have two default routes, where I wish to delete the route with 10.0.2.2 for enp0s3 interface
Delete the default route
Verify the available routes in the routing table
You can also remove the default route using interface name to delete all the default routes mapped to provided interface
13. Add additional route to the routing table
Now after adding default route, you can also add additional routes to the routing table. Here we will add a route to 192.168.168.0/24 via the route 192.168.0.1
Verify the available routes in the routing table
14. Delete additional route from routing table
Here we had added a new route for 192.168.168.0/24 via 192.168.0.1. so we will attempt to delete the same here
Verify the route list again
15. Replace existing routes from routing table
We can also replace certain directives from existing routes in the routing table. In our last example we added route 192.168.168.0/24 via 192.168.0.1 so we will change this to 192.168.168.0/24 via 10.0.2.2 gateway via a different interface (enp0s3)
My existing list of routes
Replace the route values
Verify the new route list
16. Display the route an address will take
Now we have added all the routes, but we would like to know the route which any address on the linux system will take
To know the route which will be used by 192.168.168.10 address
To know the route which will be used by 192.168.0.10 address
Conclusion
Linux ip command is a very handy tool for Linux Administrators. The only drawback with this tool is that the changes are non-persistent which means this will not survive a reboot. But in many cases such as in rescue mode or to verify temporary network changes we can use this tool to create and assign network to interfaces.
Lastly I hope this article with different ip command examples to perform network related activities (cheatsheet) on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
References
I have used below external references for this tutorial guide
man page for ip command
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
to stay connected and get the latest updates
10 Useful “IP” Commands to Configure Network Interfaces
The ip command is a new networking command-line utility that is used to assign an IP address to a network interface or configure/update useful network variables on a Linux system.
It is a part of the iproute2 package and offers several network administration tasks such as bringing up or down network interfaces, assign and remove IP addresses and routes, manage ARP cache, and much more.
The ip command is much similar to the old ifconfig command, but it is greatly more powerful with more functions and capabilities added to it.
The ifconfig command has been deprecated and replaced by the ip command in all modern Linux distributions. However, the ifconfig command is still works and available for most Linux distributions.
10 IP Command Examples
Note: Please take configuration file backup before doing any changes.
How Do I Configure Static IP Address Internet Protocol (IPv4)
To configure static IP Addresses in Linux, you need to update or edit the network configuration file to assign a Static IP Address to a system. You must be a superuser with a su (switch user) command from the terminal or command prompt.
For RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux
Open and edit the network configuration files for (eth0 or eth1) using your favorite text editor. For example, assigning IP Address to eth0 interface as follows.
Simple output:
For Ubuntu/Debian/Linux Mint
Assign Static IP Address to eth0 interface editing configuration file /etc/network/interfaces to make permanent changes as shown below.
Next, restart network services after entering all the details using the following command.
1. How to Assign an IP Address to a Specific Interface
The following command is used to assign an IP Addresses to a specific interface (eth1) on the fly.
Note: Unfortunately all these settings will be lost after a system restart.
2. How to Check an IP Address
To get the depth information of your network interfaces like IP Address, MAC Address information, use the following command as shown below.
Sample Output
3. How to Remove an IP Address
The following command will remove an assigned IP address from the given interface (eth1).
4. How to Enable Network Interface
The “up” flag with interface name (eth1) enables a network interface. For example, the following command will activate the eth1 network interface.
5. How to Disable Network Interface
The “down” flag with interface name (eth1) disables a network interface. For example, the following command will De-activates the eth1 network interface.
6. How do I Check Route Table?
Type the following command to check the routing table information of the system.
Sample Output
7. How do I Add Static Route
Why do you need to add Static routes or Manual routes, because that the traffic must not pass through the default gateway. We need to add Static routes to pass traffic from the best way to reach the destination.
8. How to Remove Static Route
To remove the assigned static route, simply type the following command.
9. How do I Add Persistence Static Routes
All the above routes will be lost after a system restart. To add permanent Static route, edit file /etc/sysconfig/network-scripts/route-eth0 (We are storing static route for (eth0).
For RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux
and add the following lines and save and exit. By default route-eth0 file will not be there, need to be created.
For Ubuntu/Debian/Linux Mint
Open the file /etc/network/interfaces and at the end add the persistence Static routes. IP Addresses may differ in your environment.
Next, restart network services after entering all the details using the following command.
10. How do I Add Default Gateway
The default gateway can be specified globally or for interface-specific config files. The advantage of the default gateway is If we have more than one NIC is present in the system. You can add the default gateway on the fly as shown below the command.
Kindly correct me if I missed out. Please refer manual page doing man ip from terminal/command prompt to know more about IP Command.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
How to Set a Static IP Address in Linux
Created/Updated: February 2, 2022
ifconfig is being replaced by the ip command.
Configuring a static IP can be difficult in Linux because it’s different based on the distro and version you’re using. This guide will show you how to configure a static IP address on the most popular Linux distros.
Ubuntu
As of version 17 of Ubuntu, networking is configured using Netplan , which is a YAML-based configuration system. It allows you to set your IP, netmask, gateway, and DNS all in one place.
Start by editing the file for your interface: in this case 01-netcfg.yaml .
vi /etc/netplan/ 01-netcfg.yaml
Editing your interface file
You’ll either see networkd or systemd in the renderer spot; keep that the same.
To have your changes take effect, restart networking with this command:
You can then apply this configuration by running netplan apply .
YAML configs are crazy about indentation, so if you get an error check there first.
CentOS
Now let’s do the same thing in CentOS. Here we’ll need to edit things the old way using sysconfig and network-scripts:
vi /etc/sysconfig/network-scripts/ ifcfg-eth0
You’ll change what you see there to something like this:
You can then apply this configuration by running:
/etc/init.d/ network restart
Ok, that will get you up and running with a static IP on the two most common Linux distros. Now let’s take a deeper look at the new ip command.
Using ip and netplan
Most Linux nerds have been using ipconfig for a long time, but it’s now being replaced with a new command called ip . Here’s how to do some basic tasks using the new command.
Show your IP using ip
or even shorter and more efficient…
(both commands show all interfaces)
Show only one interface using ip
Bring an interface up or down using ip
ip link set eth1 up
ip link set eth1 down
Only show IPv4 interfaces
Ok, so now you should know how to set a static IP on both Ubuntu and CentOS, as well as how to get some basic network information using ip instead of ipconfig .
Written By Daniel Miessler
The premier networking community for smart and curious people interested in security, technology, and society.
I read 20+ hours a week about security, tech, and society to find the patterns that help you stay ahead. Add your best email to get the update every Monday……