16

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?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Mojtaba Rezaeian
  • 623
  • 8
  • 20
  • 2
    The IPs you have included are just common IPs. That they have an 1 as last byte does not mean the `0` can not be used. May be this is just psychological because numbering for non-computer scientists starts with 1 not with 0. – Robert Jun 08 '22 at 12:54
  • 5
    Less psychological more intentional following the applicable standard – Ramhound Jun 08 '22 at 12:56
  • 6
    The first address in a subnet is used to identify the subnet. Addressed in [this](https://superuser.com/questions/1111437/why-cant-i-use-the-first-or-last-address-in-a-subnet) post. – doneal24 Jun 08 '22 at 13:09
  • I always feel wrong using an address that ends in .0 even though I know its valid if your subnet is large enough. – IT_User Jun 09 '22 at 17:13
  • 2
    Related: https://www.theregister.com/2022/06/01/ipv4_proposed_changes/ - it's been proposed to free up the 240.0.0.0/4 subnet, and much of the `127.0.0.0/8` subnet , for use on the Internet (keeping only the low /16 for localhost usage, since in practice that's all most systems use). **Also the `.0` addresses in subnets.** This would free up millions of new IPv4 addresses. Of course that would require software changes in routers, so is highly problematic. (And firewall and other rule changes if `127.1.0.1` could now be a remote address.) For now it's just one (influential) guy's proposal. – Peter Cordes Jun 10 '22 at 00:03
  • @PeterCordes: Of those I can see the 240/4 change mostly succeeding, as it already worked on Linux and some organizations already do use it internally as an extension of RFC1918 space (so even if it's un-reserved, it probably won't become global...) – u1686_grawity Jun 10 '22 at 07:19
  • I got my main router using the IP `192.168.1.1`, then I connect my Wifi to it and my Wifi address becomes `192.168.0.1`. That's a real story at my home network :) – raspiduino Jun 10 '22 at 15:47
  • Hypothetically, of the subnet mask was `254.0.0.0` (`/7`) then `127.0.0.0` would not be the first address in that block since the subnet would be `126.0.0.1 - 127.255.255.254`. Obviously the subnet mask is key to the context for the answer. – 640KB Jun 11 '22 at 14:21

6 Answers6

34

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.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 8
    The first address was never the broadcast but identifies the subnet. – doneal24 Jun 08 '22 at 13:10
  • 11
    @doneal24: It was, very long ago (there are documented traces of it – e.g. Cisco IOS had `ip broadcast-zero` as an option). But nowadays it could still be usable as a host address while _also_ identifying the subnet, as those happen in different contexts – for example, `2001:db8::/64` in IPv6 is a subnet address _and_ a valid host address at the same time. – u1686_grawity Jun 08 '22 at 13:14
  • 1
    I think Cisco's option violates [RFC 919](https://datatracker.ietf.org/doc/html/rfc919). They've had other practices that don't always play well with others. Addressing in IPv6 is very different and your observation is not surprising. – doneal24 Jun 08 '22 at 13:20
  • 13
    @doneal24: The point was that it seems to have been a practice that _predates_ RFC 919 and many other RFCs. Addressing and routing in IPv6 is still the same, only the methods for address distribution are different. – u1686_grawity Jun 08 '22 at 13:22
  • Got it. I wasn't involved in IP networks before the early '80s so I've missed practices pre-dating then. – doneal24 Jun 08 '22 at 13:30
  • 4
    I think this broadcast compatibility dates back from BSD 4.2: https://datatracker.ietf.org/doc/html/rfc1122#page-66 see page foot note. reading the SHOULD etc, systems might not follow this RFC, but it's also possible there are errata to this rfc. – A.B Jun 08 '22 at 16:04
  • One more detail about the network ID: If you were to compute the logical AND between the IP address and the subnet mask's bits, it would get you the network ID. – user654123 Jun 09 '22 at 06:16
  • 5
    @doneal24 why would identifying the subnet mean the address couldn't also be used for a host? What does identifying the subnet actually mean, anyway? It's not like datagrams are ever sent to subnets (they may be sent to broadcast addresses) – user253751 Jun 09 '22 at 09:22
  • @user253751 see my comment and link about rfc 1122 above. While there are recommendations in RFC, de facto behavior might differ. – A.B Jun 09 '22 at 09:38
  • 1
    @user253751 for example on Linux behavior might differ depending on kernel version. on kernel 5.10 (and any earlier kernel) `ip route show table all type broadcast` *will* include network address as broadcast, on kernel 5.17 the same command will not have network address as broadcast (opening the way to be able to use this address as host/unicast). – A.B Jun 09 '22 at 09:56
  • 3
    The original IP specification (before any implementations) had the broadcast address as all host bits the same -- this could be interpreted as either all zeros or all ones. This was refined (after interoperability testing) for all ones being the broadcast and all zeros being the "subnet address." That is, an address for unspecified network admin. Now, routers have regular addresses to support multiple routers per subnet. – mpez0 Jun 09 '22 at 12:39
  • @doneal24 The TCP/IP-based Internet didn't come online until 1983, so it would be surprising if you had experience before the early 80's. Until then it was still just a research project. – Barmar Jun 09 '22 at 14:14
  • @mpez0: Ah, this reminded me, in IPv6 the all-zeros interface ID *is* actually explicitly reserved for "all subnet routers" anycast... – u1686_grawity Jun 09 '22 at 14:18
  • @user1686 That's a good point wrt IPv6 routers (and IPv4 legacy). That lets one address "all routers on the subnet" even if one doesn't know which hosts are routers. OTOH, I don't believe any IPv4 routers actually respond to all-zeroes by default. Do IPv6 routers? Or is it just reserved for possible future use? – mpez0 Jun 10 '22 at 11:57
  • @mpez0: Some IPv6 routers actually implement it, though not all (and the ones which do, sometimes implement it partially and don't respond to local ND queries for it). Still, it is actually [listed as required to implement](https://datatracker.ietf.org/doc/html/rfc4291#section-2.6.1) in the current version of the spec. Meanwhile I have never seen IPv4 routers doing the same, and this is actually the first time I've heard that it _was_ a possibility in IPv4. (And really it doesn't seem all that useful in practice, as not all routers on a subnet are necessarily equal.) – u1686_grawity Jun 10 '22 at 12:21
  • @user1686 Looking at the v6 link you provided, it's Anycast, not multicast, so it's any one of the routers, not all of the routers. Very interesting. – mpez0 Jun 10 '22 at 13:21
6

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.

penguin359
  • 202
  • 2
  • 5
  • Despite the (relative) lack of upvotes, this is the correct answer. Early protocols (and config files) used `.0` to represent the whole network (i.e. what we would now call`/24`, `/16`, `/8` etc), and anything else to represent an individual IP or the broadcast address. As the slash notation had yet to be invented (CIDR) this was a way to represent the network address, as opposed to unicast or broadcast using the same 4-tuple format. – abligh Jun 11 '22 at 17:56
  • I should clarify that it's not the .0, but the lowest address in that subnet. For a /16 or anything larger than a /24, there will be some addresses that end in a .0 that are not a network address. Also, on networks smaller than a /24, some suffixes will be network addresses that are not .0 like a .128 on a /25 subnet. – penguin359 Jun 12 '22 at 18:17
  • @penguing359 - you are correct up to "Also" I think, and apologies for my sloppiness :-) Re the sentence beginning "also", in the era we are talking about (classful addressing) there were no subnets shorter than what we now call a /24; network addresses were preserved as reserved in CIDR mainly for back compatibility and AIUI served no purpose beyond that (and indeed /31 and /32 do not have them). – abligh Jun 12 '22 at 20:54
  • Indeed, /32 is a special case for a host-specific route, but I have never seen /31 used which I presumed was because it had no usable address. I have seen /29 which comprises 8 addresses in total. When my ISP granted us a /29, we were told we had 5 addresses we could use. The lowest address was the network address and unusable. The highest address was a broadcast address, and the second-highest was the default gateway for this subnet. They, at least, claimed that the network address in the subnet was unusable. – penguin359 Jun 13 '22 at 00:31
  • @abligh While most of the use of the network address is gone in classless networking, I believe it has one use remaining, I think it can be used to bind to the corresponding address on that interface in a similar fashion to using 0.0.0.0. So, binding to the network address of a DHCP address would bind to that interface and follow it if it changes addresses. However, I have not yet tested this or confirmed that this should be the case. – penguin359 Jun 13 '22 at 00:32
3

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
larsks
  • 4,053
  • 28
  • 36
2

Many decades ago the 127.0.0.0/8 address was designated as a internal loopback address. Only suitable for local traffic with the local PC.

Since the decision was made years ago, every router manufacturer every has designated this address as non-routable. Many old routers it maybe hard coded and not removable. Some would require new firmware, and the manufacturer has designated them end of line so they won't ever get new firmware. This would require many hundreds if not thousand of routers to be replaced. Some do exist that you could reprogram.

Therefore every single router on the internet will discard any traffic destine for the 127.0.0.0/8 address. The traffic will never make it through the internet due to this.

Literally tens of thousand of routers would have to be reprogrammed to allow this address to be routed. A single non-compliant router between you and your destination and the packet will never reach you.

Even if the change was made now, it would take years before packets would make it through reliably.

cybernard
  • 13,380
  • 3
  • 29
  • 33
  • Concur - once I tried to set the Loopback interface on a linux host from a /8 to a /24, which worked. Then I tried making 127.0.1.0/24 an "internal local LAN" in my virtual environment. Everything worked fine, linux, freebsd, but the windows XP machine would baulk as soon as I typed "127" into its IP config screen. Hardcoded it was, and from memory it also said NO to any first octet higher than 239, because it was reserved. – Criggie Jun 11 '22 at 09:31
1

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.

  • Yes, but only if the netmask is exactly a /24. With classless subnets, a /23 is ~512 IPs and in the middle, the .255 and its adjacent .0 IP are definitely valid. IE, you could configure a network to use 10.0.0.0/23 and 10.0.0.0 and 10.0.1.255 are reserved, you could have 10.0.0.255 and 10.0.1.0 as valid IPs allocated to devices. A /8 would have a lot of valid IPs that end in either 0 or 255. Subnetting is fun. – Criggie Jun 11 '22 at 09:36
  • Why you saying its not allowed to use `0`? This "WHY" was my exact question at first because that `0` is not reserved for broadcasting or anything special that I know. Or maybe I could not understand what you mean by default network address? Do you mean if some packets which not have any defined destination IP should be send to this destination? but why? how it will be proccessed? But if you only saying this is not allowd, there should be a logical reason for that. – Mojtaba Rezaeian Jun 12 '22 at 12:52
-2

The question is loaded - IPv4 addresses are not what you claim, but they can be represented as four numbers between 0 and 255 (but many other representations are possible, used, and valid, such as 10.5, 0x7f.0 and so on).

What you are wondering about is why the first address in a subnet is often not used. But that has nothing to do with an octet being zero, as the following examples show that are all first addressess within their subnet: 10.0.0.248, 127.1.0.160, 192.168.101.2.

The reason why the first address is often reserved has historical and compatibility reasons - if you can waste the first address, it might be prudent to enhance compatibility with old and broken devices. Since IPv4 addresses are premium nowadays, this becomes less common.

Remember Monica
  • 668
  • 6
  • 9
  • 1
    The distinction between "zero" and "first address" is discussed in the existing answer, which also goes into more details about the "historical and compatibility reasons", so this answer doesn't really add anything. – IMSoP Jun 10 '22 at 12:06