Skip to main content
  1. Posts/

IPtables Cheatsheet

·9 mins·
iptables linux iptables
Table of Contents

IPtables adalah utilitas pada Linux yang digunakan untuk mengonfigurasi aturan firewall di tingkat sistem operasi (kernel). Dengan IPtables, Anda dapat mengontrol lalu lintas jaringan, seperti memblokir atau mengizinkan koneksi berdasarkan alamat IP, port, dan protokol.

Berikut beberapa perintah IPtables yang umum digunakan untuk mengelola dan mengamankan server Linux.

Best Practice Rules
#

Contoh urutan rule yang aman.

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp -s 203.0.113.10 --dport 22 -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -j DROP

Listing Rules (Melihat Aturan Firewall)
#

Melihat seluruh aturan firewall pada tabel filter (default):

iptables -S

Melihat aturan firewall pada tabel NAT:

iptables -S -t nat

Melihat seluruh aturan firewall IPtables dalam format lengkap:

iptables-save

Melihat seluruh aturan firewall IPtables beserta counter paket dan byte:

iptables-save -c | column -t -l4 -o ""

Melihat aturan firewall berdasarkan chain tertentu (contoh: INPUT):

iptables -L INPUT

Menampilkan aturan firewall beserta nomor baris:

iptables -L --line-numbers

Menampilkan aturan firewall dengan jumlah paket dan total ukuran data (aggregate size):

iptables -L INPUT -v

Mereset counter paket dan byte pada seluruh aturan:

iptables -Z

Deleting Rules (Menghapus Aturan Firewall)
#

Menghapus aturan firewall berdasarkan nomor baris pada chain tertentu:

iptables -D INPUT 3

Menghapus seluruh aturan firewall pada chain tertentu:

iptables -F INPUT

Menghapus seluruh aturan firewall pada semua chain:

iptables -F

Menghapus custom chain (contoh: LOGDROPIN):

iptables -X LOGDROPIN

Logging
#

Anda dapat mengaktifkan logging pada suatu aturan iptables dengan membuat custom chain khusus untuk log, lalu menerapkannya pada chain utama.

Contoh konfigurasi logging untuk trafik HTTP/HTTPS.

iptables -N LOGDROPIN
iptables -A INPUT  -p tcp -m multiport --dports 80,443 -j LOGDROPIN
iptables -A OUTPUT -p tcp -m multiport --dports 80,443 -j LOGDROPIN
iptables -A LOGDROPIN -p tcp -m limit --limit 30/min -j LOG --log-prefix "*Blokir Trafik HTTP* " --log-uid
iptables -A LOGDROPIN -j DROP

Dari aturan di atas, sistem akan mencatat log setiap ada trafik masuk (INPUT) maupun keluar (OUTPUT) yang mengarah ke port 80 (HTTP) dan 443 (HTTPS), sebelum paket tersebut diblokir.

Contoh log yang dihasilkan

Jun  3 23:48:20 lamp kernel: *Blokir Trafik HTTP* IN=eth0 OUT= MAC=00:0c:29:03:8f:2d:00:0c:29:fa:93:f6:08:00 SRC=192.168.160.145 DST=192.168.160.30 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=28829 DF PROTO=TCP SPT=45996 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0
Jun  3 23:48:24 lamp kernel: *Blokir Trafik HTTP* IN= OUT=eth0 SRC=192.168.160.30 DST=104.21.30.34 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=8322 DF PROTO=TCP SPT=52702 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0 UID=1002 GID=1002

Melihat log iptables menggunakan journalctl.

journalctl -k

Atau untuk memfilter prefix tertentu:

journalctl -k | grep "Blokir Trafik HTTP"

Melihat log iptables menggunakan dmesg.

dmesg | grep "Blokir Trafik HTTP"

Anda juga dapat menerapkan aturan firewall berdasarkan UID user Linux menggunakan modul owner (hanya berlaku untuk chain OUTPUT).

iptables -N LOGDROPIN
iptables -A INPUT  -p tcp -m multiport --dports 80,443 -j LOGDROPIN
iptables -A OUTPUT -p tcp -m multiport --dports 80,443 -m owner --uid-owner 1002 -j LOGDROPIN
iptables -A LOGDROPIN -j LOG --log-prefix "*Blokir Trafik HTTP* " --log-uid
iptables -A LOGDROPIN -j DROP

Contoh berikut digunakan hanya untuk mencatat log, tanpa memblokir trafik.

iptables -N LOGACCIN
iptables -A OUTPUT -p tcp -m multiport --dports 80,443 -m owner --uid-owner 1002 -j LOGACCIN
iptables -A LOGACCIN -j LOG --log-prefix "*INFO TRAFIK* " --log-uid
iptables -A LOGACCIN -j ACCEPT

Contoh log yang dihasilkan.

Jun  3 23:56:53 lamp kernel: *INFO TRAFIK* IN= OUT=eth0 SRC=192.168.160.30 DST=172.67.150.119 LEN=52 TOS=0x00 PREC=0x00 TTL=64 ID=21276 DF PROTO=TCP SPT=34368 DPT=80 WINDOW=239 RES=0x00 ACK FIN URGP=0 UID=1002 GID=1002

Untuk melakukan trace paket dari IP tertentu di seluruh chain iptables, gunakan target TRACE pada tabel raw.

iptables -t raw -A PREROUTING -s 192.168.160.41 -p tcp \
  -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j TRACE

Melihat hasil trace dengan xtables-monitor.

xtables-monitor -t

Contoh output:

PACKET: 2 5faa8904 IN=eth0 MACSRC=52:54:0:30:67:b6 MACDST=52:54:0:9c:52:a4 MACPROTO=0800 SRC=192.168.160.41 DST=192.168.160.30 LEN=60 TOS=0x0 TTL=64 ID=43395 DF SPORT=51172 DPORT=22 SYN
 TRACE: 2 5faa8904 raw:PREROUTING:rule:0x3:CONTINUE  -4 -t raw -A PREROUTING -s 192.168.160.41/32 -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j TRACE
 TRACE: 2 5faa8904 raw:PREROUTING:return:
 TRACE: 2 5faa8904 raw:PREROUTING:policy:ACCEPT

Example Blocking Rules
#

emblokir IP tertentu yang mengakses port 80 (HTTP) dan 443 (HTTPS).

iptables -A INPUT -s 192.168.0.0/16 -p tcp -m multiport --dports 80,443 -j DROP

Menambahkan komentar ke aturan firewall.

iptables -A INPUT -s 192.168.0.0/16 -j DROP -m comment --comment "Private IP Block"

Membatasi maksimal 2 koneksi SSH per host klien

iptables -I INPUT -p tcp --dport 22 --syn \
  -m connlimit --connlimit-above 2 --connlimit-mask 32 \
  -j REJECT --reject-with tcp-reset
Opsi --syn memastikan hanya koneksi baru yang dihitung, sehingga koneksi aktif tidak terputus.

Membatasi koneksi SSH menggunakan modul limit (rate limit).

iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW \
  -m limit --limit 3/minute --limit-burst 3 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

Membatasi koneksi dan memblokir IP sementara (port flood protection).

iptables -N PORTFLOOD
iptables -A INPUT -p tcp --dport 80 -m conntrack --ctstate NEW -j PORTFLOOD
iptables -A PORTFLOOD -m recent --set --name portflood --rsource
iptables -A PORTFLOOD -m recent --update --seconds 300 --hitcount 20 \
  --name portflood --rsource -j LOG --log-prefix "*BLOCK FLOOD* "
iptables -A PORTFLOOD -m recent --update --seconds 300 --hitcount 20 \
  --name portflood --rsource -j DROP
--seconds 300 berarti IP akan diblokir selama 5 menit jika melebihi hitcount.

Memblokir semua permintaan DNS MX record.

iptables -A OUTPUT -p udp --dport 53 \
  -m string --algo kmp --hex-string '|00000f0001|' -j DROP

Memblokir permintaan MX khusus domain google.com.

iptables -A OUTPUT -p udp --dport 53 \
  -m string --algo kmp --hex-string '|06|google|03|com|00|000F|' -j DROP

Penjelasan:

  • 06 = jumlah byte kata google
  • 03 = jumlah byte TLD com
  • 000F = type record MX (15)

Memblokir trafik HTTP berdasarkan MAC address.

iptables -A INPUT -p tcp --dport 80 \
  -m mac --mac-source 00:0C:29:DB:B5:67 -j DROP
Berlaku hanya untuk trafik LAN, dan efektif pada chain PREROUTING, FORWARD, atau INPUT.

Memblokir trafik HTTP berdasarkan IPset.

iptables -A INPUT -p tcp --dport 80 \
  -m set --match-set dame_judol src -j DROP

Memblokir paket dengan status INVALID (stateful firewall).

iptables -A INPUT  -m conntrack --ctstate INVALID -j DROP
iptables -A INPUT  -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT  -p tcp -m conntrack --ctstate NEW --dport 80 -j ACCEPT

iptables -A OUTPUT -m conntrack --ctstate INVALID -j DROP
iptables -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -m conntrack --ctstate NEW --dport 80 -j ACCEPT

Mencegah serangan ping flood (ICMP).

iptables -A INPUT -p icmp --icmp-type echo-request \
  -m limit --limit 1/second -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

Melindungi server dari serangan TCP SYN Flood.

iptables -N SYNFLOOD
iptables -A INPUT ! -i lo -p tcp \
  --tcp-flags FIN,SYN,RST,ACK SYN -j SYNFLOOD

iptables -A SYNFLOOD -m limit --limit 100/sec --limit-burst 150 -j RETURN
iptables -A SYNFLOOD -m limit --limit 30/min \
  -j LOG --log-prefix "*BLOCK SYNFLOOD* "
iptables -A SYNFLOOD -j DROP

Default Drop. WAJIB diletakkan di bagian paling akhir

iptables -A INPUT -j DROP

Example Allowing Rules
#

Mengizinkan koneksi dari localhost.

iptables -A INPUT -i lo -j ACCEPT

Mengizinkan konesi yang sudah Established / Related.

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

Mengizinkan akses SSH (Port 22) dari semua IP.

iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT

Mengizinkan SSH hanya dari IP tertentu.

iptables -A INPUT -p tcp -s 203.0.113.10 --dport 22 -m conntrack --ctstate NEW -j ACCEPT

Mengizinkan beberapa port sekaligus (HTTP & HTTPS).

iptables -A INPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate NEW -j ACCEPT

Mengizinkan akses dari IP tertentu ke semua port.

iptables -A INPUT -s 203.0.113.10 -j ACCEPT

Example Load Balancer Rule
#

Server Load Balancer

  • IP: 192.168.160.30
  • Interface publik: eth0

Web Server 1

  • IP: 192.168.160.42

Web Server 2

  • IP: 192.168.160.43

Mengaktifkan IP Forwarding
#

Sebelum menerapkan rule load balancer, pastikan IP forwarding aktif pada server load balancer.

sysctl -w net.ipv4.ip_forward=1

Agar permanen, tambahkan ke /etc/sysctl.conf:

net.ipv4.ip_forward = 1

Load Balancing metode Random Balancing.

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -m statistic --mode random --probability 0.33 \
  -j DNAT --to-destination 192.168.160.42

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -j DNAT --to-destination 192.168.160.43

iptables -t nat -A POSTROUTING -o eth0 -p tcp \
  -j SNAT --to-source 192.168.160.30

Penjelasan:

  • Sekitar 33% trafik diarahkan ke 192.168.160.42
  • Sisanya otomatis jatuh ke 192.168.160.43
  • Rule terakhir POSTROUTING memastikan source IP konsisten (SNAT)

Load Balancing metode Round Robin (statistic nth).

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -m statistic --mode nth --every 2 --packet 0 \
  -j DNAT --to-destination 192.168.160.42

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -j DNAT --to-destination 192.168.160.43

iptables -t nat -A POSTROUTING -o eth0 -p tcp \
  -j SNAT --to-source 192.168.160.30

Cara kerja:

  • Setiap koneksi ke-2 diarahkan ke Web Server 1
  • Koneksi lainnya diarahkan ke Web Server 2

Jika terdapat 4 web server, nilai --every disusun bertingkat.

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -m statistic --mode nth --every 3 --packet 0 \
  -j DNAT --to-destination 192.168.160.42

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -m statistic --mode nth --every 2 --packet 0 \
  -j DNAT --to-destination 192.168.160.43

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -m statistic --mode nth --every 1 --packet 0 \
  -j DNAT --to-destination 192.168.160.44

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
  -j DNAT --to-destination 192.168.160.45

Menggunakan Conntrack Persistence (Session Stickiness) agar session login tidak rusak.

iptables -t mangle -A PREROUTING -p tcp --dport 80 \
  -m conntrack --ctstate NEW \
  -m statistic --mode nth --every 2 --packet 0 \
  -j CONNMARK --set-mark 1

iptables -t mangle -A PREROUTING -p tcp --dport 80 \
  -m conntrack --ctstate NEW \
  -j CONNMARK --set-mark 2

iptables -t mangle -A PREROUTING -j CONNMARK --save-mark

iptables -t nat -A PREROUTING -p tcp --dport 80 \
  -m connmark --mark 1 \
  -j DNAT --to-destination 192.168.160.42

iptables -t nat -A PREROUTING -p tcp --dport 80 \
  -m connmark --mark 2 \
  -j DNAT --to-destination 192.168.160.43

iptables -t nat -A POSTROUTING -o eth0 -p tcp \
  -j SNAT --to-source 192.168.160.30

Migrasi iptables ke nftables
#

Untuk menerjemahkan (translate) aturan iptables lama ke format aturan nftables

iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT

Output (contoh):

nft add rule ip filter INPUT tcp dport 22 accept

Artinya:

  • Aturan iptables untuk mengizinkan koneksi SSH (port 22)
  • Diterjemahkan menjadi aturan nftables yang setara

Untuk export aturan nftables.

nft list ruleset > /etc/nftables.conf

Untuk restore aturan nftables.

nft -f /etc/nftables.conf

Menyimpan Rule Agar Persist Setelah Reboot
#

Secara default, rule IPtables hilang setelah reboot. Berikut beberapa cara umum.

Metode 1: iptables-persistent (Debian / Ubuntu)
#

Install:

apt install iptables-persistent

Simpan rule:

iptables-save > /etc/iptables/rules.v4

Rule akan otomatis dimuat saat boot.

Metode 2: service iptables (CentOS / RHEL)
#

Simpan rule:

iptables-save > /etc/sysconfig/iptables

Enable service:

systemctl enable iptables
systemctl restart iptables

Metode 3: Script Manual (Universal)
#

Buat script:

nano /usr/local/bin/iptables-restore.sh

Isi:

#!/bin/sh
iptables-restore < /etc/iptables.rules

Simpan rule:

iptables-save > /etc/iptables.rules
chmod +x /usr/local/bin/iptables-restore.sh

Tambahkan ke startup (cron):

crontab -e
@reboot /usr/local/bin/iptables-restore.sh

Referensi:

Related

Install Hugo on Linux
·1 min
hugo hugo linux
How to Block IP Targeting Specific Domains
·2 mins
linux linux
Create Bridges on Bonds with and without VLANs using NetworkManager
·1 min
centos linux centos
Compiling Custom Kernel Modules
·3 mins
linux linux centos
Reinstalling GRUB Bootloader
·1 min
linux linux centos
Troubleshoot grub2-install: error: unknown filesystem
·2 mins
linux linux