Jump to content

How to Calculate Subnets with ipcalc

+ 5
  carlaschroder's Photo
Posted Oct 20 2009 02:43 PM

You often see documentation with instructions like "you must use different subnets for this to work," or "be sure your hosts are all on the same network." But, you're a bit hazy on what this means, and how to make the address calculations—is there a tool to help you?

There is indeed: ipcalc. This is a standard program available for any Linux. This command shows you everything you need to know for a single network:

$ ipcalc 192.168.10.0/24

Address:   192.168.10.0         11000000.10101000.00001010. 00000000
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.10.0/24      11000000.10101000.00001010. 00000000
HostMin:   192.168.10.1         11000000.10101000.00001010. 00000001
HostMax:   192.168.10.254       11000000.10101000.00001010. 11111110
Broadcast: 192.168.10.255       11000000.10101000.00001010. 11111111
Hosts/Net: 254                  Class C, Private Internet


So, here you see the old-fashioned dotted-quad notation, the newfangled CIDR notation, the available host address range, the number of hosts you can have on this network, and the binary addresses. ipcalc shows the network portion of the address, which is 192.168.10, and the host portion, which is 1–254. And it's a nice visual aid for understanding netmasks.

Warning
On Fedora, ipcalc is very different, and not nearly as helpful as the real ipcalc. You can install the real ipcalc from source, which you can download from http://freshmeat.net/projects/ipcalc/, or try whatmask. whatmask is similar to ipcalc, and it is in the Fedora repositories, so you can install it with yum install whatmask.

You need to specify the netmaskif it's not /24 (or 255.255.255.0). The more common CIDR netmasks are:

/8
/16
/24

Or, their dotted-quad equivalents:

255.0.0.0
255.255.0.0
255.255.255.0

Use netmasks to differentiate the network part of the address and the host address part. These are the private IPv4 private address ranges:

10.0.0.0–10.255.255.255
172.16.0.0–172.31.255.255
192.168.0.0–192.168.255.255

The first one, 10.0.0.0–10.255.255.255, gives you the most possible addresses. If you use the first quad for the network address, and the last three for host addresses, you'll have 16,777,214 addresses to play with, all in one giant network, which you can see for yourself:

$ ipcalc 10.0.0.0/8

Address:   10.0.0.0             00001010. 00000000.00000000.00000000
Netmask:   255.0.0.0 = 8        11111111. 00000000.00000000.00000000
Wildcard:  0.255.255.255        00000000. 11111111.11111111.11111111
=>
Network:   10.0.0.0/8           00001010. 00000000.00000000.00000000
HostMin:   10.0.0.1             00001010. 00000000.00000000.00000001
HostMax:   10.255.255.254       00001010. 11111111.11111111.11111110
Broadcast: 10.255.255.255       00001010. 11111111.11111111.11111111
Hosts/Net: 16777214             Class A, Private Internet


A 16,777,214-host network all in one subnet probably isn't what you want, so you can whittle it down into smaller subnets. This example show three subnets that use the first two quads (in bold) for the network portion of the address:

$ ipcalc 10.1.0.0/16
$ ipcalc 10.2.0.0/16
$ ipcalc 10.3.0.0/16

You could number these all the way up to 10.255.0.0/16. You can make even smaller subnets with a bigger netmask:

$ ipcalc 10.1.1.0/24
$ ipcalc 10.1.2.0/24
$ ipcalc 10.1.3.0/24

All the way up to 10.255.255.0/16.

The host address portions number from 1–254. Remember, the broadcast address is always the highest in the subnet.

ipcalc has one more excellent trick: calculating multiple subnets with one command. Suppose you want to divide a 10.150.0.0 network into three subnets for 100 total hosts. Just tell ipcalc your netmask, and how many hosts you want in each segment:

$ ipcalc 10.150.0.0/16 --s 25 25 50


ipcalc then spells it all out for you, and even shows your unused address ranges.

ipcalc has a few simple options, which you can see by running:

$ ipcalc --help


Classless Inter-Domain Routing (CIDR) notation is compact, and lets you slice and dice your networks finely, all the way down to a single host, which is /32. It is supposed to replace the old dotted-quad netmask notation, but you'll find you need to know both because there are applications that still don't support CIDR.

Linux Networking Cookbook

Learn more about this topic from Linux Networking Cookbook.

If you want a book that lays out the steps for specific Linux networking tasks, one that clearly explains the commands and configurations, this is the book for you. Linux Networking Cookbook is a soup-to-nuts collection of recipes that covers everything you need to know to perform your job as a Linux network administrator. You'll dive straight into the gnarly hands-on work of building and maintaining a computer network.

See what you'll learn


Tags:
1 Subscribe


5 Replies

 : Oct 20 2009 08:30 PM
Hey, I'm really glad you put up a thing about ipcalc. It is the best tool I've ever used for subnet calculations, CIDR, inverse masks (yuck), and decimal to binary conversion. Thanks a lot!
--
Luke Gartshore Sheppard, CISSP
 : Nov 06 2009 03:48 PM
Very informative Carla, thanks. I was missing what now appears to be the pretty obvious "You need to specify the netmaskif it's not /24 (or 255.255.255.0)"
 : Nov 08 2009 03:41 PM
Hi: I am a real novice when it comes to LINUX. I tried your ipcalc command on my OLPC XO terminal program which runs a peculiar Fedora OS called Sugar. (pardon my use of terms).

Apparently there are some fine points i missed because i just get the usage help (the unique way of telling me i didn't do it right :rolleyes:)

I'd like to paste in an image of the OLPC screen from my MAC but this Comment asks for a Image URL (http///). I expected it to open my desktop so i could pick it.

Well so much for inexperience. Anyway this looks like a promising site when i learn more about it.
WAIT A MINUTE: just saw Browse, below

I clicked on it and my desktop opened and there was the screen shot. So I "Add to Post" (be nice if forums would use the same terminology "Add to Comment" would be more accurate (picky :D)) I see it in the preview. Not quite WYSIWYG but close enough.


Attached Image

That is what i wanted: Regards.
 : Nov 08 2009 04:29 PM

Quote

Warning
On Fedora, ipcalc is very different, and not nearly as helpful as the real ipcalc. You can install the real ipcalc from source, which you can download from http://freshmeat.net/projects/ipcalc/, or try whatmask. whatmask is similar to ipcalc, and it is in the Fedora repositories, so you can install it with yum install whatmask.


Have you tried whatmask?
 : Apr 12 2010 12:15 AM

Quote

Warning
On Fedora, ipcalc is very different, and not nearly as helpful as the real ipcalc.


Well not entirely true.
Fedora in its official repos has the ipcalc program and its named ipcalulator.
yum install ipcalculator


Then just execute the same commands as the author made.e.g

[root@putin sanu01]# ipcalculator 192.168.10.0/24
Address:   192.168.10.0         11000000.10101000.00001010. 00000000
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.10.0/24      11000000.10101000.00001010. 00000000
HostMin:   192.168.10.1         11000000.10101000.00001010. 00000001
HostMax:   192.168.10.254       11000000.10101000.00001010. 11111110
Broadcast: 192.168.10.255       11000000.10101000.00001010. 11111111
Hosts/Net: 254                   Class C, Private Internet