8

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

13

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.

3

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

  • 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
1

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.

  • @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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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