How do I get a netmask from the IP address? Is there a command to get it?
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
192.168.0.1 - 192.168.0.254.
With a netmask of
/25 in cidr notation) the IP range would be
192.168.0.129 - 192.168.0.254.
You cannot in most cases, because of classless networks. A given IPv4 address can have 30 different netmasks.
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.
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
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.