Take the 2-minute tour ×
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
add comment

3 Answers

up vote 12 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 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.

share|improve this answer
add comment

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
show 5 more comments

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.

share|improve this answer
    
@WarrenYoung Thanks for pointing this out. I clarified my answer. –  jofel Mar 18 '12 at 18:57
    
Sorry, 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) –  Lazy Badger Mar 19 '12 at 4:36
    
@LazyBadger corrected, thanks! –  jofel Mar 19 '12 at 7:33
add comment

Your Answer

 
discard

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.