IP address, subnet mask, gateway (Linux view and configuration)

1.IP address

IP address = network address + host address, hosts with the same network address (network number) are hosts in the local network and can communicate with each other directly, while hosts with different network addresses are hosts in the remote network, and the mutual communication must be through the local Gateway (Gateway). Including A, B, C, D, E five categories.
IP address classification
Class A

The network number stipulates that the first digit is 0, so the first byte is 0-127, but due to the existence of special addresses, the actual first byte of class A is 1-126

If the network address is 10.0.0.0, the broadcast address is 10.255.255.255, and the effective host address is 10.0.0.1-10.255.255.254

Class B

The network number stipulates that the first two digits are 10, so the first byte is 128-191

If the network address is 172.16.0.0 and the broadcast address is 172.16.255.255, the effective host address is 172.16.0.1~172.16.255.254

Class C

The network number stipulates that the first 3 digits are 110, so the first byte is 192-223

If the network address is 192.168.100.0, the broadcast address is 192.168.100.255, and the effective host address is 192.168.100.1~192.168.100.254

Class D

The network number stipulates that the first 4 digits are 1110, so the first byte is 224-239

Class E

The network number stipulates that the first 4 digits are 1111, so the first byte is 240-255

1.1 Special IP

address description
network number all 0 The host address in the current subnet
The network number is all 1 All networks
The host number is all 0 The network address of the current host
The host number is all 1 All hosts on this network (broadcast address)
127.0.0.1 Loopback address, network transmission on the same host
0.0. 0.0 Local host of this network
255.255.255.255 Broadcast address of this network

1.2 Private IP

The address that can be used on a private network but cannot be accessed through Internet routing is for security reasons and to save IP address space. Through network address translation (NAT), the private IP can be converted to the Internet for use.

IP class Available address space
Class A 10.0.0.0~10.255.255.255
Class B 172.16.0.0~172.31.255.255
Class C 192.168.0.0~192.168.255.255

1.3 Network address

Identify which subnet it is in, indicating the network itself, the network number is fixed, and the host number is all 0, such as: 10.0.0.0, 172.16.0.0, 192.168.10.0.
Network address = IP address & amp; subnet mask

IP address: 192.168.43.166 (11000000 10101000 00101111 10100110)
Subnet mask: 255.255.255.0 (11111111 11111111 11111111 00000000)
Network address: 192.168.47.0 (11000000 10101000 00101111 00000000)

1.4 Host address

Identifies which host is within the subnet
host address = IP address & amp; (~subnet mask)

IP address: 192.168.43.166 (11000000 10101000 00101111 10100110)
Subnet mask: 255.255.255.0 (11111111 11111111 11111111 00000000)
`Subnet mask: 0.0.0.255 (00000000 00000000 0000000 11111111)
Host address: 0.0.0.166 (00000000 00000000 00000000 10100110)

1.5 Broadcast address

The address used to send information to all hosts on the network, the network number is fixed, and the host number is all 1. Such as: 255.255.255.255 (sending data packets to this address means sending to all hosts in any network), 172.16.255.255 (sending to the network at 172.16.0.0), 10.255.255.255 (sending to the network at 10.0.0.0).

1.6 Linux view IP information

#Linux
ifconfig
#Windows
ipconfig

ifconfig

identification description
eth0
lo
virbr0
eth0 is the network card address (MAC)
lo is the loopback address
virbr0 is a virtual network interface (caused by installing Linux on a virtual machine)
flags=4163 UP- network card has been start up
BROADCAST – NIC broadcast address is valid
RUNNING – the network card is linked to a device such as a router
MULTICAST-Supports multicast packets
mtu 1500 Maximum transmission unit 1500 btye, that is, the maximum load size of the link layer frame
inet 10.20.141.222
netmask 255.255.255.0
broadcast 10.20.141.255
IPv4 address
subnet mask
Broadcast address
inet6 fe80::250:56ff:fe8f:580b prefixlen 64 scopeid 0x20 IPv6 address, the first 64 bits of the address are Network prefix (subnet mask-like function), IPv6 Link-Local type address
ether 00:50:56:8f:58:0b
txqueuelen 1000 (Ethernet)
The MAC address of the network card, corresponding to the Ethernet protocol of the second link layer
The storage length of the buffer for transmitting data is 1000
RX bytes
TX bytes
Total received bytes

2. Subnet mask

The subnet mask determines the network address and host address in the IP address. Take A sending a data packet to B as an example. The IP addresses of A and B are ANDed with the subnet mask of A :
If the results are the same, it means that AB’s network address is the same, A can directly broadcast to B through ARP, and B can discover A’s MAC address direct communication.
If the results are different, then AB’s network address is different, ARP broadcast ends at the local gateway, A needs to send the data packet to the local gateway first, and the gateway queries the route according to B’s IP address The table is forwarded to B.

2.1 Subnet mask classification

2.1.1 The default subnet mask, the network number is 1, and the host number is 0

Class A: 255.0.0.0
Class B: 255.255.0.0
Class C: 255.255.255.0

2.1.2 Custom Subnet Mask

The host address can be used as the subnet address by subnetting
IP address = network address + subnet network address + subnet host address

Example: IP address 202.112.14.137/27 (11001010 01110000 00001110 10001001)
Network number: 27 digits
Subnet mask: 255.255.255.224 (11111111 11111111 11111111 11100000)
~Subnet mask: 0.0.0.31 (00000000 00000000 00000000 00011111)
Network address: 202.112.14.128 (11001010 01110000 00001110 10000000)
Host address: 0.0.0.9 (00000000 00000000 0000000 00001001)
Broadcast address: 202.112.14.159 (11001010 01110000 00001110 10011111)

3. Gateway

The target IP is in the same network segment (same network number) and can communicate directly without going through the gateway, otherwise it is required. As shown in the illustration, computer 1 and computer 2, computer 3 and computer 4 are all in the same network segment and directly communicate with each other, but computer 1 and computer 3 and 4 are in different network segments and are forwarded through the gateway.
gateway

3.1 Default Gateway

Default gateway address: the network number is fixed, and the host number is all set to 1 except the last one. If a host cannot find an available gateway, it will send the data packet to the default gateway, which will forward it.
The default gateway for 192.168.43.166/24 (11000000 10101000 00101011 10100110) is 192.168.43.254/24 (11000000 1010100 00101011 11111110)

3.2 Linux view routing information route-n

Search from top to bottom
1. The data packets to all destination addresses are forwarded by the gateway 10.98.92.250 through the network card eth0, and the Gateway is only used when the Flags has G
2. Packets destined for address 1.1.1.0 are forwarded through the virbr0 bridge device
3. The data packet destined for the 10.98.50.0 address is forwarded by the gateway 172.21.31.250 through the network card eth1
4. The data packets destined for 10.98.93.0 are forwarded through the network card eth0
5. The data packet destined for the 172.16.0.0 address is forwarded by the gateway 172.21.31.250 through the network card eth1
6. The data packet destined for the 172.19.8.0 address is forwarded by the gateway 172.19.136.250 through the network card eth2
7. The data packet to the address of 172.19.72.0 is forwarded by the gateway 172.19.136.250 through the network card eth2
8. The data packets destined for 172.19.136.0 are forwarded through the network card eth2
9. The data packets destined for 172.21.31.0 are forwarded through the network card eth1
10. The data packet destined for 192.168.0.0 is forwarded by the gateway 172.21.31.250 through the network card eth1

Identifier Description
Destination Destination address, which can be network address or host address, in the figure is the network address
Gateway Gateway address, “*” means the target address and the host In a network segment, no need
Genmask Destination address subnet mask
FLags U-routing is active
G-routing points to the gateway
H – target is a host
R-Restore entries generated by dynamic routing
D – dynamically installed by the router’s daemon
M – Modified by the routing daemon
! -Reject routing
Metric routing distance, the number of transfers required to reach the specified network
Ref Number of route references
Use Number of route lookups
Iface Interface

3.3 Linux configuration routing

#Using the route added by route, the machine restart or network card restart will fail (dynamic routing)
#Add routing (packets destined for address 10.98.50.0 are forwarded by gateway 172.21.31.250 through network card eth1)
route add -net 10.98.50.0 netmask 255.255.255.0 gw 172.21.31.250
#Add routing (packets destined for address 172.16.0.0 are forwarded by gateway 172.21.31.250 through network card eth1)
route add -net 172.16.0.0 netmask 255.240.0.0 gw 172.21.31.250

#delete route
route del -net 10.98.50.0 netmask 255.255.255.0

#Add static routing, write to /etc/sysconfig/static-routes
cat /etc/sysconfig/static-routes
any net 172.16.0.0/12 gw 172.21.19.250
any net 172.19.72.0/24 gw 172.19.200.250

[Network] The difference between route and IP route|route and IP route add routing
IP address virtual network and virtual machine network configuration
ip, subnet mask, gateway, default gateway
[Linux kernel 2] Introduction to common network packet loss and error packet debugging tools