Linux: Subnet (CIDR) Calculator

by on July 7, 2010 · 5 comments· LAST UPDATED July 7, 2010

in

I'm new to networking and need help with network settings. I'm looking for a tool for calculating available host address ranges with CIDR using Linux command prompt. How do I use subnet calculator under Linux or UNIX?

Linux comes with various IP subnet calculator that will help you with network settings. Once such program is Sipcalc which is an ip subnet calculator. You can install it as follows under Debian or Ubuntu Linux:
$ sudo apt-get update
$ sudo apt-get install sipcalc

Sample outputs:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  sipcalc
0 upgraded, 1 newly installed, 0 to remove and 51 not upgraded.
Need to get 30.6kB of archives.
After this operation, 123kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ lucid/universe sipcalc 1.1.4-2 [30.6kB]
Fetched 30.6kB in 2s (15.1kB/s)
Selecting previously deselected package sipcalc.
(Reading database ... 203411 files and directories currently installed.)
Unpacking sipcalc (from .../sipcalc_1.1.4-2_i386.deb) ...
Processing triggers for man-db ...
Processing triggers for doc-base ...
Processing 1 added doc-base file(s)...
Registering documents with scrollkeeper...
Setting up sipcalc (1.1.4-2) ...

How Do I Calculate Subnets?

$ sipcalc 192.168.1.0/24
Sample outputs:

-[ipv4 : 192.168.1.0/24] - 0
[CIDR]
Host address		- 192.168.1.0
Host address (decimal)	- 3232235776
Host address (hex)	- C0A80100
Network address		- 192.168.1.0
Network mask		- 255.255.255.0
Network mask (bits)	- 24
Network mask (hex)	- FFFFFF00
Broadcast address	- 192.168.1.255
Cisco wildcard		- 0.0.0.255
Addresses in network	- 256
Network range		- 192.168.1.0 - 192.168.1.255
Usable range		- 192.168.1.1 - 192.168.1.254

The above will provide network start & stop range, wildcard, mask and other information. You can calculate 192.168.1.0/24 subnet as follows
$ sipcalc 192.168.1.5/24

Interface Specific Calculation

Instead of taking address information from the shell command line arg sipcalc can obtain relevant information by looking at a specified interface on the system. In this example, get information for eth0 interface:
$ sipcalc eth0
Sample outputs:

-[int-ipv4 : eth0] - 0
[CIDR]
Host address		- 192.168.3.254
Host address (decimal)	- 3232236542
Host address (hex)	- C0A803FE
Network address		- 192.168.3.0
Network mask		- 255.255.255.0
Network mask (bits)	- 24
Network mask (hex)	- FFFFFF00
Broadcast address	- 192.168.3.255
Cisco wildcard		- 0.0.0.255
Addresses in network	- 256
Network range		- 192.168.3.0 - 192.168.3.255
Usable range		- 192.168.3.1 - 192.168.3.254

whatmask Program

Whatmask is a small C program that will help you with network settings. Download and compile it as follows:
$ cd /tmp
$ wget http://downloads.laffeycomputer.com/current_builds/whatmask/whatmask-1.2.tar.gz
$ tar -zxvf whatmask-1.2.tar.gz
$ cd whatmask-1.2/
$ ./configure
$ make
$ sudo make install

You can use it as follows to find out usable ip address with /29:
$ whatmask /29
Sample outputs:

---------------------------------------------
       TCP/IP SUBNET MASK EQUIVALENTS
---------------------------------------------
CIDR = .....................: /29
Netmask = ..................: 255.255.255.248
Netmask (hex) = ............: 0xfffffff8
Wildcard Bits = ............: 0.0.0.7
Usable IP Addresses = ......: 6

OR
$ whatmask 202.54.1.2/27
Sample outputs:

------------------------------------------------
           TCP/IP NETWORK INFORMATION
------------------------------------------------
IP Entered = ..................: 202.54.1.2
CIDR = ........................: /27
Netmask = .....................: 255.255.255.224
Netmask (hex) = ...............: 0xffffffe0
Wildcard Bits = ...............: 0.0.0.31
------------------------------------------------
Network Address = .............: 202.54.1.0
Broadcast Address = ...........: 202.54.1.31
Usable IP Addresses = .........: 30
First Usable IP Address = .....: 202.54.1.1
Last Usable IP Address = ......: 202.54.1.30

Download software featured in this faq:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 init July 7, 2010 at 8:37 am

Hello,

For Gentoo, the package is ipcalc : http://gentoo-portage.com/net-misc/ipcalc

Reply

2 nixCraft July 8, 2010 at 7:45 pm

Thanks for pointing out Gentoo specific info.

Reply

3 Jason July 11, 2010 at 3:32 pm

ipcalc is available in debian/ubuntu as well, and more/less combines the features of sipcalc and whatmask:

$ ipcalc 202.54.1.2/27
Address: 202.54.1.2 11001010.00110110.00000001.000 00010
Netmask: 255.255.255.224 = 27 11111111.11111111.11111111.111 00000
Wildcard: 0.0.0.31 00000000.00000000.00000000.000 11111
=>
Network: 202.54.1.0/27 11001010.00110110.00000001.000 00000
HostMin: 202.54.1.1 11001010.00110110.00000001.000 00001
HostMax: 202.54.1.30 11001010.00110110.00000001.000 11110
Broadcast: 202.54.1.31 11001010.00110110.00000001.000 11111
Hosts/Net: 30 Class C

$ ipcalc /29
Address: 255.255.255.248 11111111.11111111.11111111. 11111000
Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111
=>
Network: 255.255.255.0/24 11111111.11111111.11111111. 00000000
HostMin: 255.255.255.1 11111111.11111111.11111111. 00000001
HostMax: 255.255.255.254 11111111.11111111.11111111. 11111110
Broadcast: 255.255.255.255 11111111.11111111.11111111. 11111111
Hosts/Net: 254 Class invalid

Reply

4 nixCraft July 20, 2010 at 4:59 pm

Thanks for info, I appreciate your post.

Reply

5 Raz Goren April 3, 2014 at 3:58 am

Both commands install using yum on CentOS 6.5 too .
yum install whatmask

yum install gip

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: