How to check the IP address of this machine? I think this problem will certainly not trouble programmers. Everyone must know that if it is a Windows system, then in the cmd window, enter ipconfig. If you want to see more detailed information, enter ipconfig /all.
If it is a Linux system, if you have installed the net-tools tool, you can use the ifconfig command to view it. But if you have not installed this toolkit, you can also view it through ip addr.
Let’s look at the information displayed by typing ip addr on a host.
# ip addr 1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:16:3e:14:a2:5b brd ff:ff:ff:ff:ff:ff inet 172.16.1.196/20 brd 172.16.15.255 scope global dynamic eth0 valid_lft 291113559sec preferred_lft 291113559sec
This command can display all the network card information on the host. It can be seen that there are currently two network cards on the host, lo and eth0.
IP address
172.16.1.196 is the eth0 network card. IP address, which consists of four parts, each part occupies 8 bits (1 byte), a total of 32 bits. The IP address is the communication address of a network card in the online world, which is equivalent to our house number in the real world. Since it is a house number, everyone is different. If it is the same, an IP address conflict will occur and the Internet will not be able to be accessed.
Classification of IP addresses
Theoretically, there are at most 2 to the 32nd power of IP addresses, which is 4294967296. In fact, there are far more Not nearly so much. IP addresses are divided into 5 categories, and the first three categories A/B/C are actually available.
The following table shows in detail the number of hosts that can be included in the three types of addresses A, B, and C.
Category |
##IP Address Range |
Private IP address range |
Subnet mask |
Number of networks |
Maximum number of hosts in a network segment |
A |
##1.0.0.1-127.255.255.254 | ##10.0. 0.0-10.255.255.255
##255.0.0.0 |
##126 (2^ 7- 2) |
##16777214 (2^ 24-2) |
B |
##128.0.0.1-191.255.255.254 |
##172.16.0.0-172.31 .255.255 | ##255.255.0.0
##16383 (2^ 14-1) |
65534 (2^16-2) |
||
##192.168.0.0-192.168.255.255 |
255.255.255.0 |
2097152 (2^ 21-1) |
254 (2^8-2) |
无类型域间选路(CIDR)
看上面表格会发现一个问题,就是C类地址能包含的主机数太少了,只有254个,不够一个大一点的企业使用。而B类又太多了,很少有这么大的单位。所以,就有了一个折中的办法CIDR。
CIDR通过子网掩码将ip地址一分为二,前面的部分为网络号,后面的部分为主机号。下面通过例子看子网掩码是如何划分网络号和主机号的:
172.16.1.196/20
这个地址表示形式就是CIDR。斜杠后面的20即子网掩码,它是由前面连续的20个1组成的,即11111111.11111111.11110000.00000000。表示前20位为网络号,后12位为主机号。所以该网络可用主机数为2的12次方再减去2(一个网络地址、一个广播地址)
将子网掩码和 IP 地址按位AND计算,就可得到网络号。我们来计算下网络号以及第一个可用地址最最后一个可用地址
*.*.00000001.* *.*.11110000.* -------------- 172.16.0.0 <===网络号
获得了网络号,那么第一个可用地址为:172.16.0.1,最后一个可用地址为:172.16.15.254。
伴随着CIDR还有广播地址,172.16.15.255,它是主机号的最后一个。如果发送这个地址,那么172.16.0.0这个网络里的主机都能收到。
公有IP和私有IP
在工作中,基本上不用划分A类、B类还是C类,所以时间长了,大家都忘记了这个分类,只记得CIDR。但是有一点还是要注意的,就是公有 IP 地址和私有 IP 地址。关于私有IP范围,在之前的表格已经给出。当你看到10.x.x.x或172.x.x.x或192.x.x.x时,就要明白,这是个私有ip地址。
那么私有ip和公有ip都是用来干嘛的呢?
公有IP地址是广域网的范畴,通过它能直接访问互联网。如果你想搭建一个网站,让全世界的人都能访问,那么就需要使用公有IP。
私有IP地址:我们企业或家庭内部组建局域网用的IP,一般都会用私有IP。私有地址是局域网范畴内的,私有IP禁止出现在Internet中。
MAC地址
link/ether 00:16:3e:14:a2:5b
这一行显示的是MAC地址,它有12位16进制数组成,用6个字节表示。它是网卡的物理地址,号称全球唯一,不会有两个相同的MAC地址。既然是全球唯一,那么用它来替代ip不是非常好的。
这样是不行的,因为网络中的数据包传递,除了要有确定的地址外,还要有定位功能。MAC地址是不具备远程定位功能的,而IP具有远程定位功能。
MAC地址类似身份证号,每个人身份证号都是唯一的,但是你不能通过这个号去找到这个人的住址。而IP则类似身份证上面的家庭住址信息。
MAC地址虽不具备远程定位功能,但还是有一定的定位功能的(在局域网内)。比如在同一间办公室,你吼一声身份证号xxxxxx是谁,办公室人听到了,有人站起来说是我。但如果这个人在外地(不在同一个网段),你吼破嗓子也没人回应。
总结
IP地址具有定位功能;MAC地址唯一,但不能远程定位
CRID用来划分子网
IP分为公有IP、私有IP。
更多相关技术文章,请访问linux系统教程栏目!
The above is the detailed content of Detailed explanation of the information displayed by ip addr - IP, MAC. For more information, please follow other related articles on the PHP Chinese website!