wiki:linux/iptable

Version 1 (modified by yuna, 6 years ago) (diff)

--

iptables

ルールの追加方法

追加したい場所を確認

# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    cali-INPUT  all  --  anywhere             anywhere             /* cali:Cz_u1IQiXIMmKD4c */
2    KUBE-FIREWALL  all  --  anywhere             anywhere
3    KUBE-EXTERNAL-SERVICES  all  --  anywhere             anywhere             ctstate NEW /* kubernetes externally-visible service portals */
4    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
5    ACCEPT     icmp --  anywhere             anywhere
6    ACCEPT     all  --  anywhere             anywhere
7    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
8    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

INPUTチェインの8行目でリジェクトされる前にルールを追加する。

# iptables -I INPUT 8 -p tcp --dport 8080 -j ACCEPT

下記のコマンドで追加すると、ルールが追加されている。

# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    cali-INPUT  all  --  anywhere             anywhere             /* cali:Cz_u1IQiXIMmKD4c */
2    KUBE-FIREWALL  all  --  anywhere             anywhere
3    KUBE-EXTERNAL-SERVICES  all  --  anywhere             anywhere             ctstate NEW /* kubernetes externally-visible service portals */
4    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
5    ACCEPT     icmp --  anywhere             anywhere
6    ACCEPT     all  --  anywhere             anywhere
7    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
8    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:webcache
9    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

デバッグ

上記のルールの追加を利用して、ドロップする前にログを出力するルールを追加する。

# iptables -I INPUT 8 -j LOG --log-prefix "IPTABLES DROP:" --log-level=info
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    cali-INPUT  all  --  anywhere             anywhere             /* cali:Cz_u1IQiXIMmKD4c */
2    KUBE-FIREWALL  all  --  anywhere             anywhere
3    KUBE-EXTERNAL-SERVICES  all  --  anywhere             anywhere             ctstate NEW /* kubernetes externally-visible service portals */
4    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
5    ACCEPT     icmp --  anywhere             anywhere
6    ACCEPT     all  --  anywhere             anywhere
7    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
8    LOG        all  --  anywhere             anywhere             LOG level info prefix "IPTABLES DROP:"
9    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

ドロップされたパケットがあると、/var/log/messagesに表示される。

Jul 13 09:54:40 master1 kernel: IPTABLES DROP:IN=eth0 OUT= MAC=00:0d:3a:4d:fc:d2:74:83:ef:85:23:fc:08:00 SRC=10.0.0.8 DST=10.0.0.7 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=46332 DF PROTO=TCP SPT=39658 DPT=2379 WINDOW=29200 RES=0x00 SYN URGP=0

上記の例では、2379にアクセスされているのが分かる。iptablesで2379を空けてやると通信がうまくいくようになる。