보통 iptables는 /usr/sbin/iptables 혹은 /sbin/iptables에 설치된다. 또한 root 계정을 요구한다.
# iptables --help iptables v1.4.7 Usage: iptables -[AD] chain rule-specification [options] iptables -I chain [rulenum] rule-specification [options] iptables -R chain rulenum rule-specification [options] iptables -D chain rulenum [options] iptables -[LS] [chain [rulenum]] [options] iptables -[FZ] [chain] [options] iptables -[NX] chain iptables -E old-chain-name new-chain-name iptables -P chain target [options] iptables -h (print this help information) Commands: Either long or short options are allowed. --append -A chain Append to chain --delete -D chain Delete matching rule from chain --delete -D chain rulenum Delete rule rulenum (1 = first) from chain --insert -I chain [rulenum] Insert in chain as rulenum (default 1=first) --replace -R chain rulenum Replace rule rulenum (1 = first) in chain --list -L [chain [rulenum]] List the rules in a chain or all chains --list-rules -S [chain [rulenum]] Print the rules in a chain or all chains --flush -F [chain] Delete all rules in chain or all chains --zero -Z [chain [rulenum]] Zero counters in chain or all chains --new -N chain Create a new user-defined chain --delete-chain -X [chain] Delete a user-defined chain --policy -P chain target Change policy on chain to target --rename-chain -E old-chain new-chain Change chain name, (moving any references) Options: [!] --proto -p proto protocol: by number or name, eg. `tcp' [!] --source -s address[/mask][...] source specification [!] --destination -d address[/mask][...] destination specification [!] --in-interface -i input name[+] network interface name ([+] for wildcard) --jump -j target target for rule (may load target extension) --goto -g chain jump to chain with no return --match -m match extended match (may load extension) --numeric -n numeric output of addresses and ports [!] --out-interface -o output name[+] network interface name ([+] for wildcard) --table -t table table to manipulate (default: `filter') --verbose -v verbose mode --line-numbers print line numbers when listing --exact -x expand numbers (display exact values) [!] --fragment -f match second or further fragments only --modprobe=try to insert modules using this command --set-counters PKTS BYTES set the counter during insert/append [!] --version -V print package version.
Chain이란?
- INPUT : Inbound
- OUTPUT : Outbound
- FORWARD : Routing
iptables가 제공하는 INPUT, OUTPUT, FORWARD Chain은 삭제할 수 없다. 물론 -N 옵션을 통해 새로운 Chain을 생성할 수 있다.
Target이란?
- ACCEPT : 허용
- DROP : 차단
- REJECT : DROP과 유사하지만 응답 패킷을 전송
- LOG : syslog에 기록
- RETURN : Chain 내에서 패킷 처리
Rule 조회방법은 다음과 같다.
iptables -L
모든 Rule 삭제 방법은 다음과 같다.
iptables -F
-F(flush)는 초기화를 의미한다. 또한 -X는 모든 Chain을 제거한다.
예제
1. Inbound SSH 연결 허용
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
- INPUT Chain에 규칙 추가
- Inbound 인터페이스는 eth0
- TCP 프로토콜
- 목적지 포트는 22
- NEW, ESTABLISHED 상태만 허용
2. Outbound SSH 응답 허용
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
- OUTPUT Chain에 규칙 추가
- Outbount 인터페이스는 eth0
- TCP 프로토콜
- Outbound를 위한 소스 포트는 22
- ESTABLISHED 연결만 허용
3. Inbound HTTP 연결 허용
iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
- OUTPUT Chain에 규칙 추가
- Outbound 인터페이스는 eth0
- TCP 프로토콜
- 목적지 포트는 22
- NEW, ESTABLISHED 상태만 허용
4. Outbound HTTP 응답 허용
iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
- INPUT Chain에 규칙 추가
- Inbound 인터페이스는 eth0
- TCP 프로토콜
- Inbound시 소스 포트는 22
- ESTABLISED 연결만 허용
모든 차단
- -A : 규칙 추가 (--append), 기존 규칙에 새로운 규칙을 추가한다.
iptables -A INPUT -j DROP iptables -A OUTPUT -j DROP iptables -A FORWARD -j DROP
- -P : 기본 정책 변경 (--policy), 특정 정책을 대상으로 한다.
iptables -P INPUT -j DROP iptables -P OUTPUT -j DROP iptables -P FORWARD -j DROP
호스트가 기동될 때마다 iptables가 기본 실행되도록 설정하려면 chkconfig를 통해 호스트의 run level을 조정해야 한다.
# chkconfig --level 345 iptables on
iptables 규칙은 호스트가 기동 중인 상태에만 유효하다. 만약 규칙을 영구적으로 사용하고자 한다면 다음과 같이 규칙을 저장한다.
# service iptables save