How can I get the netmask from an IP address?
Solution 1
This is not possible. If it were possible there would be no reason to have a netmask at all as it could be automatically determined.
The netmask is used to specify the IP range a 'network' starts and stops at. This range is arbitrary.
For example, with the IP address 192.168.0.140
:
With the netmask of 255.255.255.0
(/24
in cidr notation) the IP range would be 192.168.0.1 - 192.168.0.254
.
With a netmask of 255.255.255.128
(/25
in cidr notation) the IP range would be 192.168.0.129 - 192.168.0.254
.
Solution 2
You cannot in most cases, because of classless networks. A given IPv4 address can have 30 different netmasks.
Solution 3
netmask are mainly used to distinguish local addresses in the same sub-network from addresses outside which are reachable only over a gateway.
So if you know all (= currently or in the future used) IP addresses in the same sub-network, you can construct a netmask which contains them all. For example if 192.168.2.4 and 192.168.2.253 should be in the same sub-network, netmask 255.255.255.0 (equals to the ip range 192.168.2.0-255 or in cidr notation 192.168.2.0/24) works.
Solution 4
Albeit you cant determine the netmask from the ip address directly in centos 7 you can type "ip ad" which will return the ip address with its corresponding subnet number.
-bash-4.2$ ip ad 1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:16:3e:77:ca:e9 brd ff:ff:ff:ff:ff:ff
**inet 10.1.0.11/24** brd 10.1.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::216:3eff:fe77:cae9/64 scope link
valid_lft forever preferred_lft forever
The subnet number is also stored in the following file
/etc/sysconfig/network-scripts/ifcfg-eth0 (where eth0 is dependent on your network card type)
Look for the PREFIX setting. The number it provides is the subnet mask
eg PREFIX=24
If there is no PREFIX number in the above file then your server network setting will default to a subnet mask of 8 which again will be shown from your "ip ad" command.
If you wish to change the PREFIX number remember to restart the network service using systemctl restart network.
Related videos on Youtube
George Eracleous
Updated on September 18, 2022Comments
-
George Eracleous almost 2 years
How do I get a netmask from the IP address? Is there a command to get it?
-
Karlson over 12 yearsIn which case in modern systems can you do this?
-
SquareRootOfTwentyThree over 12 yearsI don't fully understand the question, but basically all systems and devices nowadays use CIDR.
-
Warren Young over 12 years@Karlson: As Patrick said, there is no way to automatically determine when you have a case that falls into an exception to this rule. You can look at the first octet of the address and blindly guess from its top few bits that it is part of a class A, B, C... or whatever network but you cannot know for a fact that the address is of that class, hence that you know the net mask. In fact, with some addresses, it's a smart bet that the old-style "class" of an address is irrelevant: addresses beginning with 10 are almost never class A (/8) because they're almost always subnetted.
-
Karlson over 12 years@SquareRootOfTwentyThree The question is whether or not there is a way based on an IP of the destination system to determine the netmask used.
-
Karlson over 12 years@WarrenYoung I am aware of the IP address classes but you can guess the class IP mask has (and I am guessing here) an over 99% probability of being wrong.
-
Warren Young over 12 yearsNo, @Karlson, the odds are not as strong as you say. If the first octet is "10", I'd bet the odds are better than 50% that the mask is not 255.0.0.0. If it is a public IP, there's a much better chance than 1% that it is associated with a netmask other than you'd guess from the class. The only time I'd expect a 1% chance of being wrong is with 192.168.x.y addresses; there you can be pretty sure it's a class C netmask. And in that case, it's mostly because it means you're dealing with an unmanaged network, not because there's a particularly good reason to use class C masks.
-
YoloTats.com over 12 years@WarrenYoung Thanks for pointing this out. I clarified my answer.
-
SquareRootOfTwentyThree over 12 years@Karlson, I see now. Warren explained it already then, even though in my experience class A private networks with netmask /8 are fairly common.
-
Warren Young over 12 years@SquareRootOfTwentyThree: In my day job, I deal with a lot of customers using 10.x.y.z schemes on their private networks, and I rarely see
/8
masks. I think it's because our customers typically employ professional network admins, and their training makes subnetting irresistible; they've been given a hammer so they go around banging things with it. We've seen customers with a few hundred machines on a 10.x.y.z network use Byzantine routing and masking schemes. Why? Because they can. Because Cicso tells them it's a good idea. Irritating... -
Lazy Badger over 12 yearsSorry, but "netmask 255.255.255.0" means not 192.168.2.1-255, but 192.168.2.0-255 (if we'll include network and broadcast) or 192.168.2.1-254 (if we'll not include)
-
Warren Young over 12 yearsVLANs can usually solve the same problem with less complexity.
-
x-yuri almost 3 yearsDoes that mean that there could be 32 or 30 nodes that have the same IP but different netmasks?
-
phemmer almost 3 yearsNo. IP must be unique. I can't provide a good explanation of why in a short comment, and without more knowledge of what needs to be explained. If there's some confusion a new question might be appropriate.