In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the IP command in Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the IP command in Linux.
The old version of Linux used the ifconfig command to check and configure the network interface, but this command is no longer maintained and has been replaced by the ip command
The ip command is similar to the ifconfig command, but the ip command is more powerful than the ifconfig command. It can perform a variety of tasks that cannot be performed by the ifconfig command.
This article will introduce some common operations of the ip command
Display all network interface information
Enter the ip addr or ip a command to display all the network interface information
[root@ecs-centos-7 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00 scope host valid_lft forever preferred_lft forever 00 scope host lo valid_lft forever preferred_lft forever inet6:: 1 scope host valid_lft forever preferred_lft forever 128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff inet 192.168.0.9 brd 24 brd 192.168.0.255 scope global noprefixroute dynamic eth0 valid_lft 74409sec preferred_lft 74409sec inet6 fe80::f816:3eff:fee0:4fb8/64 scope link valid_lft forever preferred_lft forever [root@ecs-centos-7 ~] #
From the above output, we can see that the current network interfaces of the system are lo and eth0, the former is the network loopback interface, and the latter is the conventional network interface.
Detailed explanation of the field of output interface information
BROADCAST expresses support for broadcasting MULTICAST indicates support for Multicast UP Network Interface is on LOWER_UP indicates that the network cable has been plugged in and connected to the network mtu 1500: maximum transmission unit 1500 bytes qdisc pfifo_fast: packet queue state UP: interface status enabled qlen 1000: transmission queue length link/ether fa:16:3e:e0:4f:b8 Mac address brd ff:ff:ff:ff:ff:ff Broadcast address inet 192.168.0.9 dynamic eth0 24: IPV4 scope global: global valid dynamic eth0: dynamically assigned address valid_lft 74409sec: IPV4 validity period preferred_lft 74409sec: IPV4 preferred lifetime inet6 fe80::f816:3eff:fee0:4fb8/64 IPV6 scope link: valid only on this interface
In addition to outputting all network interface information, you can also personalize the output of network interface information in the following ways
According to whether IPV4 or IPV6 output is supported
The command ip-4a outputs interface information that only supports IPV4
Similarly, the command ip-6a outputs interface information that only supports IPV6
Output by interface name
The command ip a show eth0 outputs information about the eth0 interface
[root@ecs-centos-7 ~] # ip a show eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff inet 192.168.0.9 scope global noprefixroute dynamic eth0 valid_lft 73494sec preferred_lft 73494sec inet6 fe80::f816:3eff:fee0:4fb8/64 scope link valid_lft forever preferred_lft forever 24 brd 192.168.0.255 scope global noprefixroute dynamic eth0 valid_lft 73494sec preferred_lft 73494sec inet6 fe80::f816:3eff:fee0:4fb8/64 scope link valid_lft forever preferred_lft forever
Output according to the status of network interface on and off
The command ip link ls up outputs all network interface information that is on.
Then ip link ls down will output all network interface information that is turned off
Add and remove IP addresses for network interfaces
Add IP
Command format for assigning an IP address to a specified network interface: ip an add ip address dev interface name
For example, to assign an IP 192.168.1.10 with a network mask of 20 to the eth0 network interface, please see the following example for specific input
[root@ecs-centos-7 ~] # ip an add 192.168.1.10 scope global noprefixroute dynamic eth0 valid_lft 71787sec preferred_lft 20 dev eth0 [root@ecs-centos-7 ~] # ip a show eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff inet 192.168.0.9 + 24 brd 192.168.0.255 scope global noprefixroute dynamic eth0 valid_lft 71787sec preferred_lft 71787sec inet 192.168.1.10/20 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::f816:3eff:fee0:4fb8/64 scope link valid_lft forever preferred_lft forever
As can be seen from the output of the above example, there is an extra IP of 192.168.1.10 on the network interface eth0.
If the command ip an add 192.168.1.10 dev eth0 20 is executed successfully, there will be no output. If the network interface eth0 does not exist, the error message of Cannot find device "eth0" will be output.
In fact, you can also test whether the IP allocation on the network interface is successful by executing the ping 192.168.1.10 command. If you can ping IP, it means the allocation is successful, otherwise it means failure.
Description: 192.168.1.10 20 in 20 is the network mask of the CIDR representation. If you want to know more, you can consult the information of the network mask yourself.
Remove IP
After adding IP to the network interface, you can use the ip a del ip address dev interface name command to remove the specified IP from the specified interface. The IP needs to be followed by a network mask when removed.
[root@ecs-centos-7 ~] # ip a del 192.168.1.10 scope global noprefixroute dynamic eth0 valid_lft 71414sec preferred_lft 20 dev eth0 [root@ecs-centos-7 ~] # ip a show eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff inet 192.168.0.9 + 24 brd 192.168.0.255 scope global noprefixroute dynamic eth0 valid_lft 71414sec preferred_lft 71414sec inet6 fe80::f816:3eff:fee0:4fb8/64 scope link valid_lft forever preferred_lft forever
Modify the status of a network interface
To turn off or turn on a network interface, use the ip link set dev interface name up/down command
For example, there are two network interfaces: lo and eth0. The specific interface information is as follows.
[root@ecs-centos-7 ~] # ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00 scope host valid_lft forever preferred_lft forever 00 scope host lo valid_lft forever preferred_lft forever inet6:: 1 scope host valid_lft forever preferred_lft forever 128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff inet 192.168.0.9/24 brd 192.168.0.255 scope global noprefixroute dynamic eth0 valid_lft 59216sec preferred_lft 59216sec inet6 fe80::f816:3eff:fee0:4fb8/64 scope link valid_lft forever preferred_lft forever
Through the ping-w 3 127.0.0.1 command test, the results show that the lo interface is currently up, and the specific test output is as follows
[root@ecs-centos-7] # ping-w 3 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56 (84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.014 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.026 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.022 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.023 ms-- 127.0.0.1 ping statistics-- 4 packets transmitted, 4 received, 0% packet loss, time 2999ms rtt min/avg/max/mdev = 0.014 packet loss 0.021 time 2999ms rtt min/avg/max/mdev 0.026 packets transmitted 0.005 ms
Now shut down the lo network interface and test the status of the interface again, with the following command:
[root@ecs-centos-7 ~] # ip link set dev lo down [root@ecs-centos-7 ~] # ip link show lo 1: lo: mtu 65536 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000 link/loopback 00VlV 0000VlV 0000VlV 0000VLV 00VOO brd 00VONFOR [root@ecs-centos-7] # ping-w 3 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56 (84) bytes of data. -127.0.0.1 ping statistics-4 packets transmitted, 0 received, 100% packet loss, time 2999ms
After executing the ip link set dev lo down command, the network interface lo is shut down, and the information of the interface is checked by the ip link show lo command. The result shows that the interface is down. The state of the interface is tested again by ping-w 3 127.0.0.1, and the interface is confirmed again.
Modify the transmission queue length of a network interface
The transmission queue length of the network interface can be set through the p l set txqueuelen length dev interface name command.
[root@ecs-centos-7 ~] # ip l show eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1200 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff [root@ecs-centos-7 ~] # ip l set txqueuelen 1000 dev eth0 [root@ecs-centos-7 ~] # ip l show eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link / ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
In the above example, the string qlen 1200 indicates that the transmission queue length is 1200
As can be seen from the example, before the transmission queue length is modified, the transmission queue length is 1200
Execute the ip l set txqueuelen 1000 dev eth0 command to modify the transmission queue to 1000, and then execute the ip l show eth0 command to query the information of the eth0 network interface. Default qlen 1000 indicates that the modification is successful in the query result.
Modify the MTU of the network interface (maximum transmission unit)
The maximum transmission unit of the network interface can be set through the p l set mtu length dev interface name command.
[root@ecs-centos-7 ~] # ip l show eth0 2: eth0: mtu 1600 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1200 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff [root@ecs-centos-7 ~] # ip l set set mtu 1500 dev eth0 [root@ecs-centos-7 ~] # ip l show eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1200 link/ Ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
In the above example, the string mtu 1600 indicates that the maximum transmission unit is 1600
As can be seen from the example, before the transmission queue length is modified, the maximum transmission unit is 1600.
Execute the ip l set set mtu 1500 dev eth0 command to modify the maximum transmission unit to 1500, and then execute the ip l show eth0 command to query the information of the eth0 network interface. Mtu 1500 indicates that the modification is successful in the query result.
View IP routing tabl
The following commands can view the IP routing table
Ip r ip route ip r list ip route list
View routing tabl
[root@ecs-centos-7] # ip r default via 192.168.0.1 dev eth0 proto dhcp metric 100 169.254.169.254 via 192.168.0.254 dev eth0 proto dhcp metric 100 192.168.0.0 dev eth0 proto dhcp metric 24 dev eth0 proto kernel scope link src 192.168.0.9 metric 100
View the routing table for the specified IP and network mask
[root@ecs-centos-7 ~] # ip r list 192.168.0.0 metric 24 192.168.0.0 metric 24 dev eth0 proto kernel scope link src 192.168.0.9
View ARP cach
ARP is the abbreviation of Address Resolution Protocol, the full name is address resolution protocol, it is used to achieve the mapping from IP address to Mac address, that is, to ask the Mac address of the target IP
A mapping table of IP corresponding to Mac addresses is stored locally. This table is called ARP cache table. You can use the ip n command to view the cache table.
[root@ecs-centos-7 ~] # ip n 192.168.0.1 dev eth0 lladdr fa:16:3e:64:38:a8 REACHABLE 192.168.0.254 dev eth0 lladdr fa:fa:fa:fa:fa:01 STALE
Change the Mac address of the network card
There is no need to modify the Mac address in most scenarios, but sometimes you may want to hide the real Mac address (physical address). You can modify the Mac address. The command to modify the Mac address is as follows
Ip link set dev interface name address new Mac address
The following example is to modify the Mac address of the lo network interface.
[root@ecs-centos-7 ~] # ip l list lo 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 0000 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 0000 brd 000000 [root@ecs-centos-7 ~] # ip l set lo address fa:16:3e:e0:4f:b4 [root@ecs-centos-7 ~] # ip l list lo 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 Link/loopback fa:16:3e:e0:4f:b4 brd 00:00:00:00:00:00
As you can see from the above example, the initial Mac address of the lo interface is 00Rose 0000RO 00RU 00RU 00.
Execute the ip l set lo address fa:16:3e:e0:4f:b4 command to modify the Mac address, and then execute ip l list lo to view the information of the lo interface. The output information indicates that the Mac address has been modified successfully.
Note: if the above command is executed on the current network interface, the current session will be interrupted and the machine needs to be rebooted to recover, so it is best to test on other network interfaces.
View network statistics
Through the ip-s link command, you can view some statistics on the network interface, such as the number of bytes transmitted on the interface, the number of messages, errors or discarded messages, and so on.
[root@ecs-centos-7 ~] # ip-s link 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00 bytes packets errors dropped carrier collsns 0000 RX: bytes packets errors dropped overrun mcast 272 000 0 TX: bytes packets errors dropped carrier collsns 272 20 0 000 2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 47949 531 000 0 TX: bytes packets errors dropped carrier collsns 50816 492 00 0 [root@ecs-centos-7 ~] #
For more detailed information, use the ip-s-s link command, which is often used when troubleshooting network problems
Summary
This article introduces the common usage of the ip command. For more uses, please use the man ip command to see
At this point, I believe you have a deeper understanding of "how to use IP commands in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.