네트워크 설정

Linux 네트워크 인터페이스, 라우팅, 방화벽, DNS 설정 완벽 가이드


Linux 네트워크 인터페이스, 라우팅, 방화벽, DNS 설정 완벽 가이드

목표: 복잡한 네트워크 토폴로지를 구성하고 문제를 진단할 수 있는 수준


🌐 네트워크 기초

OSI 7계층 vs TCP/IP

OSI 계층TCP/IP프로토콜/장비Linux 명령어
7. 응용응용HTTP, FTP, SSHcurl, ssh
6. 표현응용SSL/TLS, ASCIIopenssl
5. 세션응용NetBIOS, RPC-
4. 전송전송TCP, UDPss, netstat
3. 네트워크인터넷IP, ICMP, ARPip, ping
2. 데이터링크네트워크Ethernet, PPPip link
1. 물리네트워크케이블, 허브ethtool

IP 주소 체계

IPv4

32비트 주소 = 4개의 8비트 옥텟
예: 192.168.1.1

클리스 | 범위 | 서브넷 마스크 | 호스트 수
A | 1.0.0.0 ~ 126.0.0.0 | 255.0.0.0 (/8) | 16,777,214
B | 128.0.0.0 ~ 191.255.0.0 | 255.255.0.0 (/16) | 65,534
C | 192.0.0.0 ~ 223.255.255.0 | 255.255.255.0 (/24) | 254
D | 224.0.0.0 ~ 239.255.255.255 | 멀티캐스트
E | 240.0.0.0 ~ 255.255.255.255 | 연구/예약

사설 IP 범위 (RFC 1918)

10.0.0.0/8      (10.0.0.0 ~ 10.255.255.255)
172.16.0.0/12   (172.16.0.0 ~ 172.31.255.255)
192.168.0.0/16  (192.168.0.0 ~ 192.168.255.255)

CIDR 표기법

/24 = 255.255.255.0   = 256개 주소 (254개 사용 가능)
/16 = 255.255.0.0     = 65,536개 주소
/8  = 255.0.0.0       = 16,777,216개 주소
/32 = 255.255.255.255 = 1개 주소 (호스트)

🔧 네트워크 인터페이스 관리

ip 명령어 (현대적)

# 인터페이스 목록
ip link show
ip link show eth0

# IP 주소 확인
ip addr show
ip addr show eth0
ip -4 addr show          # IPv4만
ip -6 addr show          # IPv6만

# IP 주소 추가
sudo ip addr add 192.168.1.100/24 dev eth0

# IP 주소 삭제
sudo ip addr del 192.168.1.100/24 dev eth0

# 인터페이스 활성화/비활성화
sudo ip link set eth0 up
sudo ip link set eth0 down

# MTU 변경
sudo ip link set eth0 mtu 9000

# MAC 주소 변경
sudo ip link set eth0 address 00:11:22:33:44:55

ifconfig (레거시)

# 인터페이스 정보
ifconfig
ifconfig eth0

# IP 주소 설정
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# 인터페이스 활성화/비활성화
sudo ifconfig eth0 up
sudo ifconfig eth0 down

네트워크 설정 파일

Debian/Ubuntu (/etc/network/interfaces)

# 정적 IP
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4
    dns-search example.com

# DHCP
auto eth0
iface eth0 inet dhcp

# 루프백
auto lo
iface lo inet loopback

Netplan (Ubuntu 18.04+)

# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
        search:
          - example.com

# 적용
sudo netplan apply
sudo netplan try          # 테스트 (120초 후 자동 롤백)

RHEL/CentOS (/etc/sysconfig/network-scripts/)

# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
NAME=eth0
DEVICE=eth0
ONBOOT=yes

# NetworkManager 사용 시
# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=eth0
DEVICE=eth0
ONBOOT=yes

systemd-networkd

# /etc/systemd/network/10-eth0.network
[Match]
Name=eth0

[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
Domains=example.com

# DHCP
[Network]
DHCP=yes

[DHCPv4]
RouteMetric=100

🛣️ 라우팅

라우팅 테이블

# 라우팅 테이블 확인
ip route show
ip route show table main
ip route show table local

# 상세 정보
ip route get 8.8.8.8       # 특정 목적지 경로
ip route show dev eth0     # 특정 인터페이스

# 기본 게이트웨이
ip route show default

라우트 추가/삭제

# 기본 게이트웨이 추가
sudo ip route add default via 192.168.1.1

# 정적 라우트 추가
sudo ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0

# 라우트 삭제
sudo ip route del 10.0.0.0/8
sudo ip route del default

# 메트릭 지정
sudo ip route add 172.16.0.0/16 via 192.168.1.2 metric 100

정적 라우트 영구 설정

Debian/Ubuntu

# /etc/network/interfaces
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    up ip route add 10.0.0.0/8 via 192.168.1.254
    down ip route del 10.0.0.0/8

RHEL/CentOS

# /etc/sysconfig/network-scripts/route-eth0
10.0.0.0/8 via 192.168.1.254
172.16.0.0/16 via 192.168.1.253 dev eth0

정책 기반 라우팅 (PBR)

# 라우팅 테이블 추가
echo "200 custom_table" | sudo tee -a /etc/iproute2/rt_tables

# 규칙 추가
sudo ip rule add from 192.168.1.100 table custom_table
sudo ip rule add to 10.0.0.0/8 table custom_table
sudo ip rule add iif eth1 table custom_table

# 테이블에 라우트 추가
sudo ip route add default via 10.0.0.1 table custom_table
sudo ip route add 192.168.2.0/24 dev eth1 table custom_table

# 규칙 확인
ip rule show
ip route show table custom_table

🧱 방화벽

iptables (레거시)

기본 구조

테이블 (Table)
├── filter (기본) - 패킷 필터링
│   ├── INPUT       - 들어오는 패킷
│   ├── FORWARD     - 라우팅되는 패킷
│   └── OUTPUT      - 나가는 패킷
├── nat - 주소 변환
│   ├── PREROUTING  - 라우팅 전
│   ├── POSTROUTING - 라우팅 후
│   └── OUTPUT
├── mangle - 패킷 수정
└── raw - 연결 추적 제외

기본 명령어

# 규칙 목록
sudo iptables -L
sudo iptables -L -n -v           # 상세, 숫자, 상세 통계
sudo iptables -L INPUT -n --line-numbers  # 줄 번호 포함

# 기본 정책 설정
sudo iptables -P INPUT DROP      # 기본: 들어오는 패킷 거부
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

# 규칙 추가
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 규칙 삭제
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -D INPUT 3         # 3번째 규칙 삭제

# 규칙 삽입
sudo iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT  # 첫 번째에 삽입

# 규칙 교체
sudo iptables -R INPUT 1 -p tcp --dport 22 -j DROP

# 모든 규칙 삭제
sudo iptables -F
sudo iptables -X                 # 사용자 정의 체인 삭제
sudo iptables -Z                 # 카운터 초기화

일반적인 규칙

#!/bin/bash
# firewall.sh

# 초기화
iptables -F
iptables -X

# 기본 정책
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 기본 허용
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# SSH (제한)
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT

# HTTP/HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# ICMP (ping)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

# 로깅
iptables -A INPUT -j LOG --log-prefix "DROP: "

# 저장
iptables-save > /etc/iptables/rules.v4

nftables (현대적)

# 테이블/체인/규칙 목록
sudo nft list ruleset
sudo nft list table inet filter

# 테이블 생성
sudo nft add table inet filter

# 체인 생성
sudo nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }

# 규칙 추가
sudo nft add rule inet filter input iif lo accept
sudo nft add rule inet filter input ct state established,related accept
sudo nft add rule inet filter input tcp dport 22 accept
sudo nft add rule inet filter input tcp dport { 80, 443 } accept

# 설정 저장
sudo nft list ruleset > /etc/nftables.conf

firewalld (RHEL/CentOS)

# 상태 확인
sudo firewall-cmd --state

# 영구/임시 규칙
sudo firewall-cmd --add-service=http        # 임시
sudo firewall-cmd --add-service=http --permanent  # 영구
sudo firewall-cmd --reload                  # 재로드

# 서비스 허용
sudo firewall-cmd --add-service=ssh
sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=https

# 포트 허용
sudo firewall-cmd --add-port=8080/tcp
sudo firewall-cmd --add-port=10000-20000/tcp

# 영역(zone) 변경
sudo firewall-cmd --set-default-zone=public
sudo firewall-cmd --zone=public --add-interface=eth0

# 규칙 목록
sudo firewall-cmd --list-all
sudo firewall-cmd --list-all --zone=public

# 서비스 정의
sudo firewall-cmd --get-services
sudo firewall-cmd --info-service=http

🌐 DNS 설정

/etc/resolv.conf

# 기본 DNS 설정
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1
search example.com local
options rotate timeout:1 attempts:3

systemd-resolved

# 상태 확인
systemctl status systemd-resolved
resolvectl status

# DNS 설정
sudo resolvectl dns eth0 8.8.8.8 8.8.4.4
sudo resolvectl domain eth0 example.com

# 캐시 확인
resolvectl statistics

# 캐시 플러시
sudo resolvectl flush-caches

/etc/systemd/resolved.conf

[Resolve]
DNS=8.8.8.8 8.8.4.4
FallbackDNS=1.1.1.1
Domains=example.com
DNSStubListener=yes
Cache=yes
DNSSEC=yes

DNS 도구

# dig
 dig google.com
dig @8.8.8.8 google.com
dig +trace google.com
dig -x 8.8.8.8              # 역방향 조회

# nslookup
nslookup google.com
nslookup -type=mx google.com
nslookup -type=ns google.com

# host
host google.com
host -t mx google.com
host -t ns google.com

# systemd-resolve
systemd-resolve google.com
systemd-resolve --status

🔒 SSH 설정

기본 설정 (/etc/ssh/sshd_config)

# 포트 변경
Port 22

# 루트 로그인 금지
PermitRootLogin no

# 비밀번호 인증 비활성화 (키 인증만)
PasswordAuthentication no
PubkeyAuthentication yes

# 허용 사용자/그룹
AllowUsers john alice
AllowGroups sshusers

# 세션 타임아웃
ClientAliveInterval 300
ClientAliveCountMax 2

# 최대 인증 시도
MaxAuthTries 3

# 버전 2만 사용
Protocol 2

# 호스트키 알고리즘
HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256

# 암호화 알고리즘
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com

SSH 키 관리

# 키 생성
ssh-keygen -t ed25519 -C "comment"
ssh-keygen -t rsa -b 4096 -C "comment"

# 키 복사
ssh-copy-id user@host

# 에이전트
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_ed25519

# 설정 파일 (~/.ssh/config)
Host myserver
    HostName 192.168.1.100
    User john
    Port 2222
    IdentityFile ~/.ssh/id_ed25519
    ServerAliveInterval 60

📊 네트워크 모니터링

ss (socket statistics)

# 모든 소켓
ss -a

# TCP 소켓
ss -t
ss -tl                      # listening
ss -tln                     # listening, 숫자
ss -tlnp                    # listening, 숫자, 프로세스

# UDP 소켓
ss -u
ss -ul

# 연결 상태별
ss -t state established
ss -t state listening
ss -t state time-wait

# 프로세스별
ss -tp

# 요약
ss -s

tcpdump

# 기본 사용
sudo tcpdump -i eth0

# 특정 포트
sudo tcpdump -i eth0 port 80
sudo tcpdump -i eth0 port 22

# 특정 호스트
sudo tcpdump -i eth0 host 192.168.1.1

# 특정 네트워크
sudo tcpdump -i eth0 net 192.168.1.0/24

# 패킷 내용
sudo tcpdump -i eth0 -A port 80    # ASCII
sudo tcpdump -i eth0 -X port 80    # HEX

# 파일 저장/읽기
sudo tcpdump -i eth0 -w capture.pcap
sudo tcpdump -r capture.pcap

# 제한
sudo tcpdump -i eth0 -c 100        # 100개만
sudo tcpdump -i eth0 -W 5 -C 100   # 100MB 로테이션

nmap

# 기본 스캔
nmap 192.168.1.1

# 포트 스캔
nmap -p 22,80,443 192.168.1.1
nmap -p- 192.168.1.1              # 전체 포트
nmap -sV 192.168.1.1              # 서비스 버전
nmap -sS 192.168.1.1              # SYN 스캔 (root)
nmap -sT 192.168.1.1              # TCP 스캔
nmap -sU 192.168.1.1              # UDP 스캔

# 네트워크 스캔
nmap -sn 192.168.1.0/24           # 호스트 발견
nmap -O 192.168.1.1               # OS 탐지

🐛 네트워크 트러블슈팅

연결 문제 진단

# 1. 물리적 연결 확인
ethtool eth0
mii-tool eth0

# 2. IP 주소 확인
ip addr show

# 3. 게이트웨이 확인
ip route show

# 4. 로컬 연결 테스트
ping 127.0.0.1

# 5. 게이트웨이 연결 테스트
ping 192.168.1.1

# 6. 외부 연결 테스트
ping 8.8.8.8

# 7. DNS 테스트
ping google.com
dig google.com

# 8. 포트 연결 테스트
curl -v http://example.com
nc -zv example.com 80
telnet example.com 80

# 9. 경로 추적
traceroute google.com
mtr google.com

# 10. 패킷 캡처
sudo tcpdump -i eth0 host google.com

일반적인 문제

IP 충돌

# ARP 테이블 확인
ip neigh show
arp -a

# 중복 IP 탐지
sudo arping -D -I eth0 192.168.1.100

DNS 문제

# DNS 서버 직접 쿼리
dig @8.8.8.8 google.com

# 로컬 DNS 캐시 확인
systemd-resolve --status

# 캐시 플러시
sudo systemd-resolve --flush-caches

# /etc/resolv.conf 확인
cat /etc/resolv.conf

라우팅 문제

# 라우팅 테이블 확인
ip route show

# 특정 목적지 경로
ip route get 8.8.8.8

# 패킷 경로 추적
traceroute -I 8.8.8.8
mtr --report 8.8.8.8

📋 체크리스트

  • IP 주소 설정 (정적/DHCP)
  • 라우팅 테이블 구성
  • DNS 설정
  • 방화벽 규칙 설정
  • SSH 보안 설정
  • 네트워크 모니터링
  • 연결 문제 진단

🔗 관련 노트

  • 01-01-리눅스-기본-명령어-모음 — 기본 명령어
  • 02-04-네트워크-설정 — 방화벽 심화
  • 02-04-네트워크-설정 — SSH 심화
  • 02-04-네트워크-설정 — 네트워크 심화
  • 03-02-성능-모니터링 — 네트워크 성능

마지막 업데이트: 2026-06-01