12 ip command examples
Year after year, we have been usingifconfig
Command to perform network-related tasks, such as checking and configuring Nic information. Howeverifconfig
It is no longer maintained and abolished in the latest version of Linux!ifconfig
The command has beenip
Command is replaced.
ip
Command andifconfig
The command is somewhat similar, but more powerful. It has many new features.ip
Many Commands are completedifconfig
Tasks that cannot be completed by the command.
This tutorial will discussip
The most common method in command 12, let's get started.
Case 1: Check Nic Information
Check network information such as IP address and subnet of the network adapter, and useip addr show
Command:
[linuxtechi@localhost]$ ip addr show
Or
[linuxtechi@localhost]$ ip a s
This will display the network information of all available NICs in the system. However, if you want to view the information of a nic, the command is:
[linuxtechi@localhost]$ ip addr show enp0s3
Hereenp0s3
Is the name of the ENI.
IP-addr-show-commant-output
Case 2: enable/disable NICs
Useip
Command to enable a disabled NIC:
[linuxtechi@localhost]$ sudoiplinkset enp0s3 up
To disable the NIC, usedown
Trigger:
[linuxtechi@localhost]$ sudoiplinkset enp0s3 down
Case 3: assign an IP address and other network information to the NIC
To assign an IP address to the NIC, run the following command:
[linuxtechi@localhost]$ sudoip addr add 192.168.0.50/255.255.255.0 dev enp0s3
You can also useip
Command to set the broadcast address. The broadcast address is not set by default. The command to set the broadcast address is:
[linuxtechi@localhost]$ sudo ip addr add broadcast 192.168.0.255 dev enp0s3
You can also use the following command to set a standard broadcast address based on the IP Address:
[linuxtechi@localhost]$ sudoip addr add 192.168.0.10/24 brd + dev enp0s3
As shown in the above example, we can usebrd
Replacebroadcast
To set the broadcast address.
Case 4: Delete the IP address configured in the NIC
To delete an IP address from the NIC, use the followingip
Command:
[linuxtechi@localhost]$ sudoip addr del192.168.0.10/24 dev enp0s3
Case 5: add an alias for the NIC (assuming the NIC is named enp0s3)
Add an alias to add more than one IP address to the NIC. Run the following command:
[linuxtechi@localhost]$ sudoip addr add 192.168.0.20/24 dev enp0s3 label enp0s3:1
Ip-command-add-alias-linux
Case 6: Check the routing/Default Gateway Information
View the route information to display the route path of the data packet to the destination. To view the network route information, run the following command:
[linuxtechi@localhost]$ iproute show
Ip-route-command-output
In the above output result, we can see the route information of data packets on all NICs. We can also obtain the route information of a specific IP Address:
[linuxtechi@localhost]$ sudoiprouteget192.168.0.1
Case 7: Add a static route
We can also use IP addresses to modify the default route of data packets. The method is to useip route
Command:
[linuxtechi@localhost]$ sudoiproute add default via 192.168.0.150/24
In this way, all network packets pass through192.168.0.150
Instead of the default route. To modify the default route of a nic, run the following command:
[linuxtechi@localhost]$ sudoiproute add 172.16.32.32 via 192.168.0.150/24 dev enp0s3
Case 8: Delete the default route
To delete the default route set earlier, open the terminal and run:
[linuxtechi@localhost]$ sudoiproutedel192.168.0.150/24
Note: The default route modified using the above method is only temporarily valid, and all changes will be lost after the system is restarted. To permanently modify a route, you must modify or create a route entry.route-enp0s3
File. Add the following line:
[linuxtechi@localhost]$ sudovi/etc/sysconfig/network-scripts/route-enp0s3
172.16.32.32 via 192.168.0.150/24 dev enp0s3
Save and exit the file.
If you are using an Ubuntu or debian-based operating system, the file to be modified is/etc/network/interfaces
And then addip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3
This line ends at the end of the file.
Case 9: Check all ARP records
ARP, short for Address Resolution Protocol, is used to convert an IP Address to a physical Address (that is, a MAC Address ). All IP addresses and their MAC details are stored in a table, which is called ARP cache.
To view records in the ARP cache, that is, the MAC address of the device connected to the LAN, run the following ip command:
[linuxtechi@localhost]$ ip neigh
Ip-neigh-command-linux
Case 10: Modify ARP records
The command to delete an ARP record is:
[linuxtechi@localhost]$ sudoip neigh del192.168.0.106 dev enp0s3
To add a new record to the ARP cache, run the following command:
[linuxtechi@localhost]$ sudoip neigh add 192.168.0.150 lladdr 33:1g:75:37:r3:84 dev enp0s3 nud perm
Herenud
The value of "neghbour state" (network neighbor state) can be:
perm
-Permanently valid and can only be deleted by administrators
noarp
-The record is valid, but can be deleted after the lifecycle expires.
stale
-The record is valid but may have expired
reachable
-The record is valid but becomes invalid after timeout.
Case 11: View network statistics
Passip
Command can also view network statistics, such as the number of bytes transmitted on all NICs and the number of packets, the number of errors or dropped packets. Useip -s link
Command to view:
[linuxtechi@localhost]$ ip-s link
Ip-s-command-linux
Case 12: get help
If you want to view the options that are not available in the preceding example, you can view the help information. In fact, you can seek help for any command. To listip
Run:
[linuxtechi@localhost]$ ip help
Remember,ip
Commands are especially important for Linux system management. learning and mastering them can make it easy to configure the network. This tutorial ends. If you have any suggestions, please leave a message in the following message box.
Via: https://www.linuxtechi.com/ip-command-examples-for-linux-users/
Author: Pradeep Kumar Translator: lujun9972 proofreaders: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China