How to convert an IP range to CIDR
A CIDR block must start on an address that its size divides evenly and must contain a power of two addresses. Most ranges do not fit that shape. 10.0.0.10 to 10.0.0.20 is eleven addresses that begin in the middle of a block, so no single prefix describes it. The ip range to cidr conversion splits it into the fewest blocks that cover exactly those addresses and nothing else.
The tool works from the start of the range. At each step it takes the largest block that is aligned at the current address and still fits before the end, then moves on. The result is the minimal exact set, never an over-wide block that would include addresses you did not ask for.
10.0.0.10 - 10.0.0.20
10.0.0.10/31
10.0.0.12/30
10.0.0.16/30
10.0.0.20/32That is why one range becomes four blocks here. The conversion runs in your browser, as the Privacy Policy describes.
Input the way your data comes
Ranges rarely arrive in one clean format, so the reader accepts the common ones on the same list. Start and end can be joined by a hyphen, an en dash, an em dash, the word to, a comma, a semicolon, a tab or a space. A short form such as 192.168.1.10-20 repeats the first three numbers for you.
- A line with a single address becomes a /32 or /128 block.
- A line that is already a CIDR block passes through. A block with host bits set, such as 10.0.0.5/24, is rounded to 10.0.0.0/24.
- A header line such as start,end is skipped, and extra columns after the end address, like a site name, are ignored and counted in the summary.
- Text after a hash sign or two slashes is a comment.
Mixed IPv4 and IPv6 lists work. A single range cannot mix them, so a line such as 10.0.0.1 - ::1 is reported with its line number.
Aggregate ranges into fewer CIDR blocks
Vendor lists often hold ranges that overlap or sit next to each other. Setting Mode to Aggregate list joins them before converting, so the output is as short as it can be. This is CIDR aggregation, also called route summarization or supernetting when it is done to routes.
34.64.0.0 - 34.64.255.255
34.65.0.0 - 34.65.255.255
34.66.0.0 - 34.66.127.255
34.66.64.0 - 34.66.255.255
35.190.0.0 - 35.190.63.255
34.64.0.0/15
34.66.0.0/16
35.190.0.0/18Five ranges became three blocks. The first two ranges touch, so they form a /15. The next two overlap, so they form one /16. The summary reports how many ranges the aggregation joined. In Each range mode the same list would give one set of blocks per line, and a note suggests Aggregate when it sees overlaps.
Exclude a subnet from a range
To allow a whole network except one part, give the whole network as the input and put the part in Exclude. The tool subtracts it and writes the blocks that remain. Exclude accepts blocks, single addresses and ranges, separated by spaces or commas, and works in both modes.
10.0.0.0/8 excluding 10.1.0.0/16
10.0.0.0/16
10.2.0.0/15
10.4.0.0/14
10.8.0.0/13
10.16.0.0/12
10.32.0.0/11
10.64.0.0/10
10.128.0.0/9A hole in the middle of a large block costs several blocks, one for each side at every size. If everything in the list falls inside the excluded space, the tool says so instead of returning nothing.
Output for nginx, Apache and iptables
Output turns the blocks into text you can paste into a configuration. The templates are plain text, so nothing is fetched or checked against a server. Rule type chooses allow or deny. IPv6 blocks use ip6tables for iptables.
- nginx writes allow or deny lines and ends with deny all or allow all, which is how nginx access rules are normally closed.
- Apache writes Require ip lines inside RequireAny for allow, and Require not ip lines inside RequireAll with Require all granted for deny.
- iptables writes one append rule per block with ACCEPT or DROP.
- CSV adds the first and last address, the number of addresses and the source line. JSON gives an array of strings.
allow 10.0.0.0/30;
allow 2001:db8::/126;
deny all;Test any generated rule on a non-production server first. A wrong allow list can lock you out.
Reversed ranges and other mistakes
A range whose start is above its end is a common slip in spreadsheets. With Reversed ranges set to Swap, the two addresses are exchanged and the line is listed in the notes. Set it to Error to leave such lines out and list them instead. An octet above 255, a leading zero, a mix of IPv4 and IPv6 in one range, and a netmask with gaps are always reported with the line number.
To go the other way, from blocks to first and last addresses, use CIDR to IP Range. To tidy a list first, Sort Lines orders it, Compare Two Lists shows what changed between two versions, and List to Comma Separated joins a column into one line.
Limits
- The tool does arithmetic only. It does not look up who owns a range or where it is.
- Rules for nginx, Apache and iptables are templates for the common case. Check them against your own configuration.
- IPv6 zone identifiers such as %eth0 are rejected.
- A line with more than two addresses and no slash uses the first two as the range and ignores the rest.