Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

How do I get a netmask from the IP address? Is there a command to get it?

share|improve this question

3 Answers 3

up vote 13 down vote accepted

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
With the netmask of (/24 in cidr notation) the IP range would be -
With a netmask of (/25 in cidr notation) the IP range would be -

share|improve this answer

You cannot in most cases, because of classless networks. A given IPv4 address can have 30 different netmasks.

share|improve this answer
In which case in modern systems can you do this? – Karlson Mar 18 '12 at 18:39
I don't fully understand the question, but basically all systems and devices nowadays use CIDR. – SquareRootOfTwentyThree Mar 18 '12 at 18:45
@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. – Warren Young Mar 18 '12 at 18:46
@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 Mar 18 '12 at 18:49
@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. – Karlson Mar 18 '12 at 18:52

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 and should be in the same sub-network, netmask (equals to the ip range or in cidr notation works.

share|improve this answer
@WarrenYoung Thanks for pointing this out. I clarified my answer. – jofel Mar 18 '12 at 18:57
Sorry, but "netmask" means not, but (if we'll include network and broadcast) or (if we'll not include) – Lazy Badger Mar 19 '12 at 4:36
@LazyBadger corrected, thanks! – jofel Mar 19 '12 at 7:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.