12 ip command examples

Source: Internet
Author: User
Tags sudoip

12 ip command examples

Year after year, we have been usingifconfigCommand to perform network-related tasks, such as checking and configuring Nic information. HoweverifconfigIt is no longer maintained and abolished in the latest version of Linux!ifconfigThe command has beenipCommand is replaced.

ipCommand andifconfigThe command is somewhat similar, but more powerful. It has many new features.ipMany Commands are completedifconfigTasks that cannot be completed by the command.

This tutorial will discussipThe 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 showCommand:

  1. [linuxtechi@localhost]$ ip addr show
  2. Or
  3. [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:

  1. [linuxtechi@localhost]$ ip addr show enp0s3

Hereenp0s3Is the name of the ENI.

IP-addr-show-commant-output

Case 2: enable/disable NICs

UseipCommand to enable a disabled NIC:

  1. [linuxtechi@localhost]$ sudoiplinkset enp0s3 up

To disable the NIC, usedownTrigger:

  1. [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:

  1. [linuxtechi@localhost]$ sudoip addr add 192.168.0.50/255.255.255.0 dev enp0s3

You can also useipCommand to set the broadcast address. The broadcast address is not set by default. The command to set the broadcast address is:

  1. [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:

  1. [linuxtechi@localhost]$  sudoip addr add 192.168.0.10/24 brd + dev enp0s3

As shown in the above example, we can usebrdReplacebroadcastTo set the broadcast address.

Case 4: Delete the IP address configured in the NIC

To delete an IP address from the NIC, use the followingipCommand:

  1. [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:

  1. [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:

  1. [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:

  1. [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 routeCommand:

  1. [linuxtechi@localhost]$ sudoiproute add default via 192.168.0.150/24

In this way, all network packets pass through192.168.0.150Instead of the default route. To modify the default route of a nic, run the following command:

  1. [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:

  1. [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-enp0s3File. Add the following line:

  1. [linuxtechi@localhost]$  sudovi/etc/sysconfig/network-scripts/route-enp0s3
  2. 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/interfacesAnd then addip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3This 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:

  1. [linuxtechi@localhost]$  ip neigh

Ip-neigh-command-linux

Case 10: Modify ARP records

The command to delete an ARP record is:

  1. [linuxtechi@localhost]$ sudoip neigh del192.168.0.106 dev enp0s3

To add a new record to the ARP cache, run the following command:

  1. [linuxtechi@localhost]$ sudoip neigh add 192.168.0.150 lladdr 33:1g:75:37:r3:84 dev enp0s3 nud perm

HerenudThe 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

PassipCommand 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 linkCommand to view:

  1. [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 listipRun:

  1. [linuxtechi@localhost]$ ip help

Remember,ipCommands 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.