
From theory, each part of an IP address is a number between 0 to 255 (256 usable slots in total).
But why is 0
not used in any IP addresses in their 4th part and they always start from 1 in IP addresses calculations?
For example:
127.0.0.1 <=== logically this should start from 0 as the first usable IP address.
192.168.0.1 <=== for example even default router IP address is set to 1 rather than to be 0
10.10.0.1
.
.
Why is the 0
slot always skipped from the last part?
답변1
But why is 0 not used in any IP addresses in their 4th part and they always start from 1 in IP addresses calculations?
With respect to the title of your question, at least under Linux, 127.0.0.0
is a perfectly useful address (that like any other 127.*
address maps to the loopback interface).
For example, I can start a webserver on 127.0.0.0:8080
:
docker run -d --rm --hostname node1 --name node1 -p 127.0.0.0:8080:8080 alpine \
sh -c "apk add --update darkhttpd; mkdir /srv; hostname > /srv/index.html; darkhttpd /srv --port 8080"
And then start another one bound to 127.0.0.1:8080
:
docker run -d --rm --hostname node2 --name node2 -p 127.0.0.1:8080:8080 alpine \
sh -c "apk add --update darkhttpd; mkdir /srv; hostname > /srv/index.html; darkhttpd /srv --port 8080"
And now now I can access those servers on their respective addresses:
$ curl 127.0.0.0:8080
node1
$ curl 127.0.0.1:8080
node2
답변2
In IPv4, the first address of a subnet is reserved for...unclear reasons, really, but most likely because very long ago it used to be the "broadcast" address. (Later, the last address became the standard broadcast address.)
So nowadays it might be possible to use the first address, technically, but most existing network stacks still treat it as "reserved" (kind of like how the entire ex-"Class E" 240.0.0.0/4 space became accidentally unusable).
Note that it's specifically the first address of a subnet, and not always the 0
address. Those only match up in the case of a /24 subnet. But for example, a /16 subnet will have .0.0
reserved, but not .1.0
or .47.0
– those are in the middle of the /16 so they're perfectly valid host addresses.
(And on the other hand, a smaller /27 subnet could start not only at .0
but also at .32
or .192
– those would again be reserved in a /27, even if they're not reserved in a /24.)
Also note that this only applies to broadcast subnets (e.g. ethernet). Such addresses can still be used in point-to-point links or routed individually as /32's.
답변3
In the IPv4 specifications, the lowest address in any IPv4 network is reserved as the network address which has a distinct purpose from the broadcast address which is normally the highest address in the network (though that can be configured differently if you are looking for fun). The idea for a network address makes more sense in the days before class-less networks and subnet masks. The concept behind subnets and subnet masks was an add-on to IPv4 not present in the original specification.
Update: There is nothing magical about it ending in ".0", but simply that it's the lowest address in a subnet (which may not always be .0).
Originally, IPv4 was classful and divided into 5 classes; A, B, C, D, and E. Class A networks are all IPs that have the uppermost significant bit as a 0 or, in other words, IPs from 0.0.0.0 to 127.255.255.255. IPv4 addresses whose upper two bits are 10 are class B which corresponds to addresses 128.0.0.0 to 191.255.255.255. Class C begin 110xxxxx in their first octet or addresses 192.0.0.0 to 223.255.255.255. Class D have a first octet matching 1110xxxx giving them IPs 224.0.0.0 to 239.0.0.0. Finally, Class E is 1111xxxx or IPs 240.0.0.0 to 255.255.255.255. Class E addresses are considered reserved and have no defined use with the special exception of 255.255.255.255. Class D are multicast addresses used to send packets to groups of computers together. Classes A-C are your normal unicast IPs where individual devices get an IP on some network.
With that said, what defines the size of each network is it's class. Class A networks are defined to have the first 8-bits or first octet to be the network part of the the address and the remaining 24-bits are the host-specific part of the address on a single IPv4 network. Class B split it 16-bits and 16-bits or 2 octets each. Class C networks used the first 24-bits as the network part and the last 8-bits as the host part. In modern terms, a class A network has a /8 subnet, class B is a /16, and class C is a /24 subnet. The separation between the network and host part of the IPv4 address is important for routing packets and is how any device decides if it can send it directly to the system on the local network or if it has to send it to a router to forward on elsewhere. Before IPv4 network stacks and protocols implemented subnetting, this distinction was hard-coded from the IP itself.
You can see remnants of this in protocols like the RIPv1 routing protocol. It only advertises IPs, not subnet masks, so the distinction between a host address and a network address was key. If a RIPv1 address advertised an address of 192.0.0.0, that is the lowest address in a class B network so it is the network address for that entire network. It would tell other devices that all systems from 192.0.0.0 to 192.0.255.255 were available from that route (since class B uses the first two octets for network, 192.0 in this example). However, if it advertised 192.0.0.1, that is not the lowest address in that network so it is a host address. That means that RIPv1 is advertising a route for a single, specific host, not a whole network. Maybe it's a dial-up modem, or has some other reason to get a unique route, but that advertisement is only for that one host.
Now that subnets are common and newer protocols like RIPv2 send the subnet mask along with the IP, the whole concept of a network address is a bit redundant, but we are stuck with it for historical reasons. A host specific route can be advertised with a subnet mask of 255.255.255.255, but we still have to maintain compatibility with software that assumes a network address is present at the bottom of the network range.
Update: To further clarify, here are some examples. If you were to slice up the 10.x.x.x IP address range into /26 subnets, that gives each subnet 64 total addresses (62 usable for hosts) since only 6 bits of the 32-bit IP are remaining for the host portion. The first five subnet network addresses would be 10.0.0.0, 10.0.0.64, 10.0.0.128, 10.0.0.192, and 10.0.1.0. There default broadcast addresses would be the highest address in the subnet and correspond to 10.0.0.63, 10.0.0.127, 10.0.0.191, and 10.0.1.63, respectively. If instead, a /23 subnet was used, each network would have 512 addresses (510 usable). The first five subnets are 10.0.0.0, 10.0.2.0, 10.0.4.0, and 10.0.6.0. The corresponding broadcast addresses are 10.0.1.255, 10.0.3.255, 10.5.255, and 10.7.255. Note, in this latter example, 10.0.1.0, 10.0.3.0, and so on are not network addresses. They are perfectly usable as regular IPs for hosts because they are in the middle of the subnet range. Only the very first and last addresses are special. 10.0.0.255 is also similarly usable as a regular host.
답변4
In IPv4, whatever IP ends in .0 means it's a (default) network address when you use the prefix /24. You cannot use this address and assign it to a computer. The 'allowed' IPs to assign is from .1 to .254. You also can't use .255 because it is used as the 'broadcast' address for the network.