How to use IP commands in CentOS (Linux) system

Time:2019-12-1

[root @ Linux ~] (IP [option] [action] [command]

Parameters:

Option: set parameters, mainly including:
– s: display the statistics of the device, such as the total number of packets received, etc;
Action: refers to which network parameters can be used for action, including:
Link: About device settings, including MTU, MAC address, etc
Addr / address: about additional IP settings, such as the implementation of multiple IPS, etc;
Route: route related settings

We can know from the above syntax that in addition to setting some basic network parameters, IP can also set additional IP settings, including the implementation of multi IP, which is perfect! Next, we will introduce this IP command in three parts (link, addr, route).

Related settings of device: IP link

IP link can set related settings related to device, including MTU, MAC of the network device, etc., and of course, it can also start (up) or shut down (down) a network device. The whole syntax is as follows:

[root @ Linux ~] (IP [- S] link show < = = simply check the information related to the device
[root @ Linux ~] (IP link set [device] [actions and parameters]

Parameters:

Show: only display the relevant content of this device. If – s is added, more statistics will be displayed;
Set: you can start setting items. Device refers to eth0, eth1 and other device codes;
Actions and parameters: including the following actions:
Up and down: start (up) or shut down a device, and other parameters use preset Ethernet parameters;
Address: if this device can change the Mac, use this parameter to change it;
Name: give this device a special name;
MTU: set the maximum transmission unit.

Example 1: display all equipment information

[[email protected] ~]# ip link show

1: lo: <LOOPBACK,UP,10000> mtu 16436 qdisc noqueue

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: <BROADCAST,MULTICAST,UP,10000> mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:50:fc:22:9a:cb brd ff:ff:ff:ff:ff:ff

3: sit0: <NOARP> mtu 1480 qdisc noop

    link/sit 0.0.0.0 brd 0.0.0.0

[[email protected] ~]# ip -s link show eth0

2: eth0: <BROADCAST,MULTICAST,UP,10000> mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:50:fc:22:9a:cb brd ff:ff:ff:ff:ff:ff

    RX: bytes packets errors dropped overrun mcast

    484011792 2247372 0       0       0       0

    TX: bytes packets errors dropped carrier collsns

    2914104290 2867753 0       0       0       0

Use IP link show to display the hardware related information of the whole device, as shown above, including MAC address, MTU, etc. What is more interesting is the device of sit0, which is to convert the packets of IPv4 and IPv6, which has no effect on the network using only IPv4. Lo and sit0 are set by the host itself. If the – S parameter is added, the relevant statistics of the network card will be listed, including the number of packets received (Rx) and transmitted (TX), etc. the detailed content is the same as that of ifconfig.

Example 2: information about starting, shutting down and setting equipment

[[email protected] ~]# ip link set eth0 up
#Start eth0 device;
[[email protected] ~]# ip link set eth0 down
#Turn off eth0;
[[email protected] ~]# ip link set eth0 mtu 1000
#Change MTU to 1000 bytes, and the unit is bytes.

If ifconfig is used to update MTU of network card, it can also be used. If you want to change the “network card code, MAC address information”, you need to use IP. You need to close the network card before setting, otherwise it will fail. As follows:

Example 3: modify network card code, MAC and other parameters

[[email protected] ~]# ip link set eth0 name vbird

SIOCSIFNAME: Device or resource busy

#Because the device is currently started, this setting cannot be done. You should do this:

[root @ Linux ~] (IP link set eth0 down < = = shut down the device
[root @ Linux ~] (IP link set eth0 name vbird < = = reset
[root @ Linux ~] (IP link show < = = view information

2. vbird: <BROADCAST,MILTICASE> mtu 900 qdisc pfifo_fast qlen 1000

    link/ehter 00:40:d0:13:c3:46 brd ff:ff:ff:ff:ff:ff

#Ha ha, even the network card code can be changed! However, remember to change back after playing!

#Because our ifcfg-eth0 still uses the original device code! Avoid problems, change back

[root @ Linux ~] (IP link set vbird name eth0 < = = change the device back
[[email protected] ~]# ip link set eth0 address aa:aa:aa:aa:aa:aa
[[email protected] ~]# ip link show eth0

#If your network card supports MAC changes,
#Then the command above can change your network card MAC!
#However, it’s still the old saying. Please change it right after the test!

The hardware related information settings of this device, including MTU, MAC and transmission mode, can be set here. What’s interesting is the address project. It’s easy to make a mistake that the MAC address is followed by the address instead of the IP address! Remember! For more hardware parameters, you can use man IP to check the settings related to IP link.

About additional IP related settings: IP address

If IP link is related to the second layer of data link layer of OSI seven layer model, then IP address (IP address R) is related to the third layer of network layer. It mainly sets various parameters related to IP, including netmask, broadcast, etc.

[root @ Linux ~] (IP address show < = = view IP parameters
[root @ Linux ~] ා IP address [address| del] [IP parameter] [dev? Standby name] [related parameter]

Parameters:

Show: simply display the IP information of the device;

Add del: add or delete related parameters, mainly including:

IP parameters: mainly refers to the settings of the domain, such as 192.168.100.100/24;

Dev: the device to be set for this IP parameter, such as eth0, eth1, etc;

Relevant parameters: as follows:

Broadcast: set the broadcast address. If the setting value is + it means that the system will automatically calculate it;

Label: alias of the device, such as eth0:0;

Scope: the domain of this device is usually divided into the following categories:

Global: Allow connections from all sources;

Site: only IPv6 is supported, only the connection of this host is allowed;

Link: only this device can connect itself;

Host: only internal connections of the host are allowed;

So of course, it’s global. Default is also global!

Example 1: display the IP parameters of all devices:

[[email protected] ~]# ip address show

1: lo: <LOOPBACK,UP,10000> mtu 16436 qdisc noqueue

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,10000> mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:50:fc:22:9a:cb brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0

    inet6 fe80::250:fcff:fe22:9acb/64 scope link

       valid_lft forever preferred_lft forever

3: sit0: <NOARP> mtu 1480 qdisc noop

    link/sit 0.0.0.0 brd 0.0.0.0

Let’s further add virtual network devices:

Example 2: add a new device. The name is assumed to be eth0: vbird

[[email protected] ~]# ip address add 192.168.50.50/24 broadcast + /

> dev eth0 label eth0:vbird

[[email protected] ~]# ip address show eth0

2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:40:d0:13:c3:46 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0
    inet 192.168.50.50/24 brd 192.168.50.255 scope global eth0:vbird
    inet6 fe80::240:d0ff:fe13:c346/64 scope link

       valid_lft forever preferred_lft forever

#Look at the output above. A new line has been added. The name is eth0: vbird

#As for the broadcast +, it can also be written as broadcast 192.168.50.255.

[[email protected] ~]# ifconfig

eth0:vbir Link encap:Ethernet HWaddr 00:40:D0:13:C3:46
          inet addr:192.168.50.50 Bcast:192.168.50.255 Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
          Interrupt:5 Base address:0x3e00

#If you use ifconfig, you can see this strange thing!

Example 3: delete the device just now

[[email protected] ~]# ip address del 192.168.50.50/24 dev eth0

#It’s easy to delete.

——————————————————————————–

About the setting of route: IP route

This project is to view and set the route. In fact, the function of IP route is almost the same as route, but it can also set additional parameters, such as MTU planning, etc., which is quite powerful!

[root @ Linux ~] (IP route show < = = simply show route settings
[root @ Linux ~] ා IP route [add| del] [IP or domain] [via gateway] [dev device]

Parameters:

Show: simply display the routing table, or use list;

Add del: add or delete routes;

IP or domain: you can use a domain like 192.168.50.0/24 or a pure IP;
Via: it is not necessary to go out from that gateway;
Dev: it is connected by that device, and it is required;
MTU: the value of MTU can be set additionally;

Example 1: display the current routing data

[[email protected] ~]# ip route show
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.2
169.254.0.0/16 dev eth1 scope link
default via 192.168.1.254 dev eth1

As shown in the above table, the simplest function is to display the current routing information, which is actually the same as the route command. Some points should be paid attention to:

Proto: the routing protocol of this route mainly includes redirect, kernel, boot, static, RA, etc., in which kernel refers to the automatic setting directly determined by the core.

Scope: the scope of the route, mainly link, is the direct connection related to this device.

Let’s see how to add and delete routes:

Example 2: add a route, mainly a domain that can be directly communicated with the local computer

[[email protected] ~]# ip route add 192.168.5.0/24 dev eth0

#Set the route for the domain where the local computer communicates directly, and do not need to go through the external router

[[email protected] ~]# ip route show
192.168.5.0/24 dev eth0 scope link

… omitted below

Example 3: increase the route to the outside through router;

[[email protected] ~]# ip route add 192.168.10.0/24 via 192.168.5.100 dev eth0
[[email protected] ~]# ip route show

192.168.5.0/24 dev eth0 scope link

Other omissions

192.168.10.0/24 via 192.168.5.100 dev eth0

#Careful, because I have 192.168.5.0/24 routing (directly related to my network card),

#So we can throw the route of 192.168.10.0/24 to 192.168.5.100

#That host will help deliver it! The same restriction as the route command mentioned before!

Example 4: add default route

[[email protected] ~]# ip route add default via 192.168.1.2 dev eth0

#That 192.168.1.2 is my default router (Gateway);

#Remember, just one default route is OK;

Example 5: delete route

[[email protected] ~]# ip route del 192.168.10.0/24
[[email protected] ~]# ip route del 192.168.5.0/24 
 
In fact, this IP command is too broad and profound! Just contact Linux network friends, may see a little dizzy! You can use ifconfig, ifup, ifdown and route first. After you have experience in the future, you can continue to play the interesting command IP! If you are interested, you can also refer to the command ethtool!