CIDR
Classless Inter-Domain Routing
An IP address allocation scheme. Subnets are written like 10.0.0.0/16. Essential for cloud VPC setup.
Classless Inter-Domain Routing
An IP address allocation scheme. Subnets are written like 10.0.0.0/16. Essential for cloud VPC setup.
CIDR (Classless Inter-Domain Routing) is a method for efficiently allocating and routing IP addresses. A network range is expressed by appending a slash (/) and a number to an IP address, as in 192.168.1.0/24. The number after the slash indicates the number of network bits; /24 means that the upper 24 bits are the network address and the remaining 8 bits (256 addresses) are the host address.
CIDR was introduced in 1993 in RFC 1517–1520. The previous class-based (Class A/B/C) addressing scheme wasted IP addresses. For example, a whole Class B (65,536 IPs) had to be allocated even to small and medium-sized businesses that did not need it. CIDR allows allocating flexibly only as much as is needed, which delayed IPv4 address exhaustion by more than 10 years.
In CIDR notation, the larger the number, the smaller the network. /8 provides 16,777,214 addresses (large enterprise), /16 provides 65,534 (medium-sized enterprise), /24 provides 254 (small network), and /32 is a single IP. Converted to subnet masks, /24 is 255.255.255.0 and /16 is 255.255.0.0. Understanding this relationship makes VPC design and writing security group rules easier.
In cloud environments, CIDR is an essential concept. When creating an AWS VPC, you specify a CIDR block such as 10.0.0.0/16 and divide it into /24 subnets. In a security group, 0.0.0.0/0 allows all IPs, and 10.0.1.0/24 allows only a specific subnet. Kubernetes Pod CIDR and Service CIDR separate networks on the same principle.
import ipaddress
# CIDR 기본 정보 확인
network = ipaddress.ip_network("192.168.1.0/24")
print(f"네트워크 주소: {network.network_address}") # 192.168.1.0
print(f"브로드캐스트: {network.broadcast_address}") # 192.168.1.255
print(f"서브넷 마스크: {network.netmask}") # 255.255.255.0
print(f"호스트 수: {network.num_addresses - 2}") # 254 (사용 가능)
print(f"IP 범위: {list(network.hosts())[0]} ~ {list(network.hosts())[-1]}")
# IP가 특정 CIDR에 포함되는지 확인
ip = ipaddress.ip_address("192.168.1.100")
cidr = ipaddress.ip_network("192.168.1.0/24")
print(f"{ip} in {cidr}: {ip in cidr}") # True
# VPC 서브넷 분할 예제 (/16 → /24로 분할)
vpc_cidr = ipaddress.ip_network("10.0.0.0/16")
subnets = list(vpc_cidr.subnets(new_prefix=24))
print(f"생성 가능한 /24 서브넷 수: {len(subnets)}") # 256개
# 실제 AWS 서브넷 설계 예시
print("Public Subnet 1:", subnets[0]) # 10.0.0.0/24
print("Public Subnet 2:", subnets[1]) # 10.0.1.0/24
print("Private Subnet 1:", subnets[10]) # 10.0.10.0/24
print("Private Subnet 2:", subnets[11]) # 10.0.11.0/24
# CIDR 간 겹침 여부 확인
net1 = ipaddress.ip_network("10.0.0.0/16")
net2 = ipaddress.ip_network("10.0.5.0/24")
print(f"겹침 여부: {net1.overlaps(net2)}") # True
# 보안그룹 규칙 검증
allowed_cidr = ipaddress.ip_network("10.0.0.0/8", strict=False)
client_ip = ipaddress.ip_address("10.50.100.25")
print(f"접근 허용: {client_ip in allowed_cidr}") # True
# AWS VPC 생성 (CIDR: 10.0.0.0/16)
aws ec2 create-vpc \
--cidr-block 10.0.0.0/16 \
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=MyVPC}]'
# 서브넷 생성 (/24 단위)
aws ec2 create-subnet \
--vpc-id vpc-12345678 \
--cidr-block 10.0.1.0/24 \
--availability-zone ap-northeast-2a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=PublicSubnet1}]'
# 보안그룹 인바운드 규칙 추가
# 특정 CIDR에서만 SSH 허용
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp \
--port 22 \
--cidr 10.0.0.0/8
# 모든 IP에서 HTTPS 허용 (0.0.0.0/0 = 전체)
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0
# VPC CIDR 블록 추가 (Secondary CIDR)
aws ec2 associate-vpc-cidr-block \
--vpc-id vpc-12345678 \
--cidr-block 10.1.0.0/16
# 라우팅 테이블에 CIDR 기반 경로 추가
aws ec2 create-route \
--route-table-id rtb-12345678 \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id igw-12345678
# ipcalc로 CIDR 계산 (Linux)
ipcalc 192.168.1.0/24
# Network: 192.168.1.0/24
# Netmask: 255.255.255.0
# HostMin: 192.168.1.1
# HostMax: 192.168.1.254
# Hosts/Net: 254
# sipcalc로 상세 정보 확인
sipcalc 10.0.0.0/16
# VPC 및 서브넷 설계 (Terraform)
variable "vpc_cidr" {
default = "10.0.0.0/16" # 65,536개 IP
}
# VPC 생성
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "production-vpc"
}
}
# 퍼블릭 서브넷 (AZ별 /24 할당)
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, count.index)
# count=0: 10.0.0.0/24, count=1: 10.0.1.0/24
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
tags = { Name = "public-subnet-${count.index + 1}" }
}
# 프라이빗 서브넷 (/24 할당, 오프셋 10부터)
resource "aws_subnet" "private" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, count.index + 10)
# count=0: 10.0.10.0/24, count=1: 10.0.11.0/24
availability_zone = data.aws_availability_zones.available.names[count.index]
tags = { Name = "private-subnet-${count.index + 1}" }
}
# 보안그룹 - CIDR 기반 접근 제어
resource "aws_security_group" "web" {
name = "web-sg"
vpc_id = aws_vpc.main.id
# 전체 IP에서 HTTPS 허용
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# VPC 내부에서만 SSH 허용
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.vpc_cidr] # 10.0.0.0/16
}
# 특정 사무실 IP만 DB 접근 허용
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["203.0.113.0/24"] # 사무실 IP 대역
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
"If we set the VPC to 10.0.0.0/16, we can use 65,000 IPs. If we split it into /24 units, we can create 2 public subnets and 2 private subnets and still have room to add more than 250 subnets. When we adopt EKS later, we'll need a Pod CIDR as well, so it's better to allocate generously."
"Right now the SSH port in the security group is open to 0.0.0.0/0, which means it is accessible from every IP in the world. We should restrict it to the office IP range, 203.0.113.0/24, or at the very least narrow it to 10.0.0.0/8 (the internal network)."
"When connecting a VPN, overlapping CIDRs cause routing conflicts. The on-premises network uses 192.168.0.0/16, so we should set the AWS VPC to 10.0.0.0/16 and specify both CIDRs in the Direct Connect routing table."
If you create a VPC with /24 (254 addresses), you cannot divide it into subnets, and you will run out of IPs when expanding EKS or services. Start a VPC with at least /16. You can expand a CIDR later, but you cannot shrink it.
With VPC Peering or a VPN connection, the connection will not work if the CIDRs on both sides overlap. In a multi-account environment, plan in advance to separate IP ranges, such as 10.0.0.0/16, 10.1.0.0/16, and 10.2.0.0/16.
In a security group, 0.0.0.0/0 means "all IPs in the world." Using this setting for SSH (22), RDP (3389), or DB ports immediately makes you a target for hacking. Restrict access to only the CIDRs that are strictly necessary.
Use a VPC /16 → subnet /24 structure as the baseline. Separating ranges by purpose makes management easier: public at 10.0.0-9.x, private at 10.0.10-19.x, and DB at 10.0.20-29.x. Use only RFC 1918 private IPs (10.x, 172.16-31.x, 192.168.x).