This may be a really dumb question, but I like to turn upside down what I already know sometimes, that way I can learn more. For example, given a network 192.168.0.0/24, I have a device inside with the IP 192.168.0.1. Ok, nothing strange here. But why couldn't I have this same IP in other networks like the same I have but /25, /26, /27.....If with the subnet mask, I could know inside which network is the given device? I could have the same 192.168.0.1 IP in more than one network only changing the subnet mask. I know that we can't repeat IPs, but what I want to understand is the WHY we can't, because as far as I can tell, we could repeat IPs because we have the subnet masks.
why couldn't I have this same IP in other networks like the same I have but /25, /26, /27... I could have the same 192.168.0.1 IP in more than one network only changing the subnet mask... We could repeat IPs because we have the subnet masks.
That idea ignores the fact that all routing is based on the longest prefix match. If you try this you will find that only one of the subnets will route to the .1 IP address (taking just one of the IP addresses as an example). What's more, some clients on the subnets would act like they could reach .1, but in reality it wouldn't be the same .1 that is responding elsewhere.
By using so many overlapping subnets, you will have a network that is rather difficult to operate and troubleshoot.
You don't need to believe me. Go ahead and try out what you're saying (using a subnet not critical to production); very soon, you'll want to hang up this duplicate addresses idea because it's so broken.
In all honesty, I have used overlapping subnets three or so times in my career to solve temporary problems when I was in a bind; the key here is that I never duplicated IP addresses even with overlapping subnets. Plus, I always built those overlapping subnets with the intention of tearing them down as soon as possible.
A network node routes each destination - it consults its local routing table and decides where the packet should go. There's no try-and-error, each routing decision is definitive. When that route fails to work the destination is unreachable.
Locally connected destinations - within the same subnet - are sent to directly (without a gateway). E.g. a source node with IP address/mask of 192.168.0.1/24 talks directly to all IP address from 192.168.0.0 to 192.168.0.255. If these addresses exist elsewhere in your network they cannot be reached.
Non-local destinations are contacted using a router/gateway. In the simplest case, that is the default gateway with the routing entry 0.0.0.0/0 - fits everything but is the last entry to check.
That gateway does the exact same thing - check if the destination is locally connected or requires a(nother) gateway, according to the gateway's routing table. Each forwarding decision on each router is only done once for a packet. There's no try-and-error and no checking if the destination is actually reachable on that route.
This forwarding process repeats until on the last hop, the destination must be locally connected. If it isn't the packet is dropped.
So, try to work out a way where you can have duplicate IP addresses without failing routes. It won't work.