Ana içeriğe geç
Kategoriye Dön

Port Forwarding ve NAT Yapılandırma: iptables Rehberi

NAT kavramları, DNAT/SNAT kuralları, iptables ve nftables ile port yönlendirme, masquerade ve kullanım senaryoları: oyun sunucuları, NAT arkasındaki web sunucuları.

Okuma süresi: 13 dk Güvenlik
port forwardingnatiptablesdnatsnatmasqueradelinuxağ yapılandırma

İçindekiler

Port Forwarding ve NAT Yapılandırma: iptables Rehberi

Port forwarding ve NAT (Network Address Translation), ağ trafiğini yönetmek için temel araçlardır. Bir sunucunun arkasındaki servisleri dışarıya açmak, oyun sunucusu kurmak veya birden fazla servisi tek IP üzerinden yönetmek için bu teknikleri kullanırsınız. Bu rehberde iptables ve nftables ile NAT ve port forwarding yapılandırmayı öğreneceksiniz.

NAT Nedir?

NAT (Network Address Translation), IP paketlerinin kaynak veya hedef adreslerini değiştiren bir ağ tekniğidir. İki ana türü vardır:

  • SNAT (Source NAT): Kaynak IP adresini değiştirir — iç ağdan dışarıya çıkan trafikte kullanılır
  • DNAT (Destination NAT): Hedef IP adresini değiştirir — dışarıdan iç ağa gelen trafiği yönlendirmek için kullanılır
  • Masquerade: Dinamik IP'ler için özel SNAT türü

Temel Kavramlar

İnternet → [Sunucu: 203.0.113.1] → [İç Ağ: 192.168.1.0/24]
                                         ↓
                                   [Web: 192.168.1.10:80]
                                   [DB: 192.168.1.20:3306]
                                   [Oyun: 192.168.1.30:27015]

IP Yönlendirmeyi Etkinleştirme

Port forwarding için önce IP yönlendirme etkinleştirilmelidir:

hljs bash
# Geçici olarak etkinleştir
sudo sysctl -w net.ipv4.ip_forward=1

# Kalıcı olarak etkinleştir
sudo nano /etc/sysctl.conf
# Şu satırı ekle:
# net.ipv4.ip_forward=1

# Değişiklikleri uygula
sudo sysctl -p

# Doğrula
cat /proc/sys/net/ipv4/ip_forward
# Çıktı: 1

iptables ile Port Forwarding (DNAT)

Temel Port Yönlendirme

hljs bash
# Dış port 80'i iç sunucuya yönlendir
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.10:80

# Dış port 443'ü iç sunucuya yönlendir
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 192.168.1.10:443

# Farklı port numarasına yönlendir (dış 8080 → iç 80)
sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80

# Yönlendirilen trafiğe FORWARD izni ver
sudo iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 80 -j ACCEPT
sudo iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 443 -j ACCEPT

Belirli IP'den Gelen Trafiği Yönlendir

hljs bash
# Yalnızca belirli IP'den gelen trafiği yönlendir
sudo iptables -t nat -A PREROUTING -s 203.0.113.5 -p tcp --dport 22 -j DNAT --to-destination 192.168.1.5:22

# Belirli ağ arayüzünden gelen trafiği yönlendir
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 3306 -j DNAT --to-destination 192.168.1.20:3306

UDP Port Yönlendirme

hljs bash
# Oyun sunucusu için UDP port yönlendirme
sudo iptables -t nat -A PREROUTING -p udp --dport 27015 -j DNAT --to-destination 192.168.1.30:27015
sudo iptables -A FORWARD -p udp -d 192.168.1.30 --dport 27015 -j ACCEPT

# DNS yönlendirme
sudo iptables -t nat -A PREROUTING -p udp --dport 53 -j DNAT --to-destination 192.168.1.5:53
sudo iptables -t nat -A PREROUTING -p tcp --dport 53 -j DNAT --to-destination 192.168.1.5:53

SNAT ve Masquerade

hljs bash
# Masquerade — dinamik IP için (internet bağlantısı paylaşımı)
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# SNAT — statik IP için (daha verimli)
sudo iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 203.0.113.1

# Belirli ağdan çıkan trafiği masquerade et
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

Tam NAT Yapılandırması

hljs bash
# Tam bir NAT + port forwarding yapılandırması

# 1. Mevcut kuralları temizle
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t mangle -F

# 2. Varsayılan politikalar
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

# 3. Loopback ve mevcut bağlantılar
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

# 4. SSH erişimi
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 5. Port forwarding kuralları
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.10:80
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 192.168.1.10:443
sudo iptables -t nat -A PREROUTING -p udp --dport 27015 -j DNAT --to-destination 192.168.1.30:27015

# 6. Forward kuralları
sudo iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 80 -j ACCEPT
sudo iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 443 -j ACCEPT
sudo iptables -A FORWARD -p udp -d 192.168.1.30 --dport 27015 -j ACCEPT

# 7. Masquerade
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# 8. Kuralları kaydet
sudo apt install iptables-persistent -y
sudo netfilter-persistent save

nftables ile Port Forwarding

hljs bash
sudo nano /etc/nftables.conf
#!/usr/sbin/nft -f

flush ruleset

table ip nat {
    chain prerouting {
        type nat hook prerouting priority -100;
        
        # Web sunucusu port yönlendirme
        tcp dport 80 dnat to 192.168.1.10:80
        tcp dport 443 dnat to 192.168.1.10:443
        
        # Oyun sunucusu UDP yönlendirme
        udp dport 27015 dnat to 192.168.1.30:27015
        
        # Farklı port numarasına yönlendirme
        tcp dport 8080 dnat to 192.168.1.10:80
    }
    
    chain postrouting {
        type nat hook postrouting priority 100;
        
        # Masquerade
        oif eth0 masquerade
    }
}

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        iif lo accept
        ct state established,related accept
        tcp dport 22 accept
    }
    
    chain forward {
        type filter hook forward priority 0; policy drop;
        ct state established,related accept
        
        # Yönlendirilen trafiğe izin ver
        ip daddr 192.168.1.10 tcp dport { 80, 443 } accept
        ip daddr 192.168.1.30 udp dport 27015 accept
    }
    
    chain output {
        type filter hook output priority 0; policy accept;
    }
}

Kullanım Senaryoları

Senaryo 1: Oyun Sunucusu (CS2, Minecraft)

hljs bash
# CS2 sunucusu
sudo iptables -t nat -A PREROUTING -p udp --dport 27015 -j DNAT --to-destination 192.168.1.30:27015
sudo iptables -t nat -A PREROUTING -p tcp --dport 27015 -j DNAT --to-destination 192.168.1.30:27015
sudo iptables -A FORWARD -d 192.168.1.30 -p udp --dport 27015 -j ACCEPT
sudo iptables -A FORWARD -d 192.168.1.30 -p tcp --dport 27015 -j ACCEPT

# Minecraft sunucusu
sudo iptables -t nat -A PREROUTING -p tcp --dport 25565 -j DNAT --to-destination 192.168.1.31:25565
sudo iptables -A FORWARD -d 192.168.1.31 -p tcp --dport 25565 -j ACCEPT

Senaryo 2: NAT Arkasındaki Web Sunucusu

hljs bash
# HTTP ve HTTPS yönlendirme
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.10:80
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j DNAT --to-destination 192.168.1.10:443
sudo iptables -A FORWARD -i eth0 -d 192.168.1.10 -p tcp -m multiport --dports 80,443 -j ACCEPT

Senaryo 3: SSH Port Değiştirme

hljs bash
# Dış port 2222'yi iç port 22'ye yönlendir
sudo iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to-destination 192.168.1.5:22
sudo iptables -A FORWARD -d 192.168.1.5 -p tcp --dport 22 -j ACCEPT

Kuralları Kalıcı Yapma

hljs bash
# iptables-persistent ile
sudo apt install iptables-persistent -y
sudo netfilter-persistent save

# Manuel olarak
sudo iptables-save > /etc/iptables/rules.v4
sudo ip6tables-save > /etc/iptables/rules.v6

# Yeniden yükleme scripti
sudo nano /etc/network/if-pre-up.d/iptables
hljs bash
#!/bin/bash
iptables-restore < /etc/iptables/rules.v4
ip6tables-restore < /etc/iptables/rules.v6
hljs bash
sudo chmod +x /etc/network/if-pre-up.d/iptables

Sorun Giderme

hljs bash
# NAT tablosunu görüntüle
sudo iptables -t nat -L -n -v

# Bağlantı izleme tablosunu görüntüle
sudo conntrack -L

# Paket sayaçlarını sıfırla
sudo iptables -Z

# Belirli porta gelen trafiği izle
sudo tcpdump -i eth0 port 80

# IP yönlendirme kontrolü
cat /proc/sys/net/ipv4/ip_forward

# Kural eşleşmelerini say
sudo iptables -t nat -L -n -v | grep -v "0     0"

Port forwarding kuralları ekledikten sonra hem PREROUTING (DNAT) hem de FORWARD kurallarını eklemeyi unutmayın. Sadece DNAT kuralı eklemek yeterli değildir — FORWARD zincirinde de trafiğe izin verilmesi gerekir.

Sonuç

Port forwarding ve NAT, Linux ağ yönetiminin temel bileşenleridir. iptables veya nftables ile DNAT, SNAT ve masquerade kurallarını doğru yapılandırarak oyun sunucuları, web servisleri ve diğer uygulamaları NAT arkasından dışarıya açabilirsiniz.