Internet Protocol Address
Internet Protocol Address
An address that identifies devices on a network. IPv4 (32-bit), IPv6 (128-bit). Divided into public/private IPs.
Internet Protocol Address
An address that identifies devices on a network. IPv4 (32-bit), IPv6 (128-bit). Divided into public/private IPs.
An IP address (Internet Protocol Address) is a logical address that uniquely identifies every device connected to a network. Just as a home address is needed to deliver mail, an IP address allows data packets to reach the correct destination on the internet. It operates at the network layer (OSI Layer 3), and routers look at this address to forward packets.
An IP address consists of a network portion and a host portion. The network portion indicates which network the device belongs to, and the host portion indicates which device it is within that network. The subnet mask defines this boundary. For example, in 192.168.1.100/24, 192.168.1 is the network and 100 is the host.
The internet is currently transitioning from IPv4 to IPv6. As the roughly 4.3 billion IPv4 addresses have been exhausted, technologies such as NAT and DHCP have kept it going, but in the IoT era, IPv6's nearly unlimited address space (3.4×10^38 addresses) is essential. Most ISPs and cloud services already support dual stack (IPv4+IPv6).
Four 8-bit (0-255) octets, separated by dots (.). About 4.3 billion addresses.
Eight 16-bit hexadecimal blocks, separated by colons (:). Consecutive zeros can be omitted with ::.
| Characteristics | IPv4 | IPv6 |
|---|---|---|
| Address Length | 32 bits (4 bytes) | 128 bits (16 bytes) |
| Number of Addresses | About 4.3 billion | 3.4 × 10^38 |
| Notation | Decimal + dots (192.168.1.1) | Hexadecimal + colons (2001:db8::1) |
| Broadcast | Supported (255.255.255.255) | Not supported (uses multicast) |
| NAT Requirement | Required (address shortage) | Not required (sufficient addresses) |
| Header Size | 20-60 bytes (variable) | 40 bytes (fixed) |
A public IP (Public IP) is an address that is uniquely identified on the internet. It is assigned by an ISP and can be accessed directly from anywhere in the world. It is used for services that require external access, such as web servers and API servers.
A private IP (Private IP) is an address used only within an internal network. It is not routed on the internet, and multiple organizations can use the same private IP ranges without conflict. It is translated to a public IP through NAT to access the internet.
| Class | Private IP Ranges | Subnet Mask | Number of Hosts |
|---|---|---|---|
| Class A | 10.0.0.0 - 10.255.255.255 | /8 (255.0.0.0) | 16,777,214 |
| Class B | 172.16.0.0 - 172.31.255.255 | /12 (255.240.0.0) | 1,048,574 |
| Class C | 192.168.0.0 - 192.168.255.255 | /16 (255.255.0.0) | 65,534 |
| Loopback | 127.0.0.0 - 127.255.255.255 | /8 | Localhost Only |
A subnet (Subnet) is a technique for dividing a large network into smaller logical networks. It reduces broadcast domains, strengthens security, and allows IP addresses to be used efficiently. The subnet mask defines the boundary between the network portion and the host portion.
CIDR (Classless Inter-Domain Routing) resolves the inefficiency of traditional class-based IP allocation. It indicates the number of network bits using slash notation, as in /24. 192.168.1.0/24 means the first 24 bits are the network and the last 8 bits are the host (254 usable).
| CIDR | Subnet Mask | Usable IPs | Common Use |
|---|---|---|---|
| /32 | 255.255.255.255 | 1 | Single Host |
| /30 | 255.255.255.252 | 2 | Point-to-Point Link |
| /28 | 255.255.255.240 | 14 | Small Subnet |
| /24 | 255.255.255.0 | 254 | General LAN |
| /16 | 255.255.0.0 | 65,534 | Large Network |
DHCP (Dynamic Host Configuration Protocol) is a protocol that automatically assigns IP addresses to network devices. Without manual configuration, devices automatically receive the IP address, subnet mask, gateway, and DNS server information. It operates on a lease basis, and the default lease time is usually 24 hours.
NAT (Network Address Translation) performs translation between private IPs and public IPs. It allows multiple devices to access the internet through a single public IP, mitigating the IPv4 address exhaustion problem. Types include Static NAT (1:1), Dynamic NAT (N:M), and PAT/NAPT (N:1, port-based).
| Technology | Role | How It Works |
|---|---|---|
| DHCP | Automatic IP assignment | DISCOVER → OFFER → REQUEST → ACK |
| Static NAT | 1:1 address translation | Fixed mapping (for servers) |
| Dynamic NAT | N:M address translation | Dynamic assignment from a pool |
| PAT (NAPT) | N:1 port translation | Distinguished by port number (home routers) |
import socket
import ipaddress
# 내 IP 주소 확인
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
print(f"로컬 IP: {local_ip}")
# 공인 IP 확인 (외부 서비스 사용)
import urllib.request
public_ip = urllib.request.urlopen('https://api.ipify.org').read().decode()
print(f"공인 IP: {public_ip}")
# IP 주소 유효성 검사 및 분석
ip = ipaddress.ip_address('192.168.1.100')
print(f"버전: IPv{ip.version}")
print(f"사설 IP: {ip.is_private}")
print(f"루프백: {ip.is_loopback}")
# IPv6 주소 분석
ipv6 = ipaddress.ip_address('2001:db8::8a2e:370:7334')
print(f"IPv6 압축 표기: {ipv6.compressed}")
print(f"IPv6 전체 표기: {ipv6.exploded}")
# 서브넷 계산
network = ipaddress.ip_network('192.168.1.0/24', strict=False)
print(f"네트워크 주소: {network.network_address}")
print(f"브로드캐스트: {network.broadcast_address}")
print(f"호스트 범위: {list(network.hosts())[0]} - {list(network.hosts())[-1]}")
print(f"호스트 수: {network.num_addresses - 2}")
# CIDR 서브넷 분할
parent = ipaddress.ip_network('10.0.0.0/16')
subnets = list(parent.subnets(prefixlen_diff=8)) # /24로 분할
print(f"서브넷 256개 생성: {subnets[0]} ~ {subnets[-1]}")
# 현재 IP 설정 확인
ip addr show # Linux
ifconfig # macOS/Linux (deprecated)
ipconfig # Windows
# 공인 IP 확인
curl ifconfig.me
curl ipinfo.io/ip
curl api.ipify.org
# 상세 IP 정보 (지리 정보 포함)
curl ipinfo.io/8.8.8.8
# 네트워크 인터페이스별 IP
ip -4 addr show eth0 # IPv4만
ip -6 addr show eth0 # IPv6만
# 라우팅 테이블 확인
ip route show
netstat -rn
# 기본 게이트웨이 확인
ip route | grep default
# DHCP 임대 정보 확인 (Linux)
cat /var/lib/dhcp/dhclient.leases
# DHCP 갱신
sudo dhclient -r eth0 # 해제
sudo dhclient eth0 # 재할당
# NAT 테이블 확인 (Linux iptables)
sudo iptables -t nat -L -n -v
# 서브넷 계산 (ipcalc 도구)
ipcalc 192.168.1.0/24
# Network: 192.168.1.0/24
# Broadcast: 192.168.1.255
# HostMin: 192.168.1.1
# HostMax: 192.168.1.254
# Hosts/Net: 254
const os = require('os');
const dns = require('dns');
// 로컬 네트워크 인터페이스 정보
const interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(name => {
interfaces[name].forEach(iface => {
if (iface.family === 'IPv4' && !iface.internal) {
console.log(`${name}: ${iface.address}`);
console.log(` 넷마스크: ${iface.netmask}`);
console.log(` MAC: ${iface.mac}`);
}
});
});
// IP 주소 유효성 검사
const net = require('net');
console.log(net.isIP('192.168.1.1')); // 4 (IPv4)
console.log(net.isIP('2001:db8::1')); // 6 (IPv6)
console.log(net.isIP('invalid')); // 0 (유효하지 않음)
console.log(net.isIPv4('192.168.1.1')); // true
console.log(net.isIPv6('::1')); // true
// 공인 IP 확인 (fetch)
async function getPublicIP() {
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
console.log(`공인 IP: ${data.ip}`);
}
getPublicIP();
// CIDR 파싱 및 계산 (ip-cidr 패키지)
const IPCIDR = require('ip-cidr'); // npm install ip-cidr
const cidr = new IPCIDR('192.168.1.0/24');
console.log(`시작: ${cidr.start()}`); // 192.168.1.0
console.log(`끝: ${cidr.end()}`); // 192.168.1.255
console.log(`크기: ${cidr.size()}`); // 256
console.log(`포함: ${cidr.contains('192.168.1.100')}`); // true
"Set the VPC to 10.0.0.0/16 and allocate a /20 subnet per Availability Zone, and each subnet can use more than 4,000 IPs. It's common to keep public subnets small at /24 and private ones large at /20."
"If you see a 169.254.x.x address, DHCP assignment has failed. It was temporarily assigned by APIPA (Automatic Private IP Addressing), so you need to check the DHCP server status or the network connection."
"Allow only outbound through a NAT Gateway, and receive inbound only through the Load Balancer's static IP. EC2 instances in private subnets can access the internet without public IPs while still being protected from external exposure."
A /24 has 256 IPs, but only 254 are actually usable. The first (network address) and the last (broadcast) cannot be assigned to hosts. AWS reserves 3 additional addresses, so only 251 are usable.
When the network ranges on both sides overlap in a VPN or VPC Peering, routing conflicts occur. Plan IPs in advance and allocate the 10.x.x.x range separately for each organization.
If you put a public IP directly in code, an outage occurs when the IP changes. Use DNS domains or environment variables, and use the Elastic IP (static IP) service.
Centrally manage IP allocation with IPAM (IP Address Management) tools. Document the CIDR plan and separate ranges by production/staging/development environment. Review dual stack support for the IPv6 transition.