iptables使用の詳細

3484 ワード

1.iptablesのインストール
#       iptables
service iptables status
#  iptables
yum install -y iptables
#  iptables-services
yum install iptables-services

iptablesを使用した後、システムをfirewalldで無効にしてください.
#   firewalld  
systemctl stop firewalld
#   firewalld  
systemctl mask firewalld

1.1ルール
iptablesには複数のRules(1つの条件と1つの目標値)が含まれます.条件が満たされると、ルールの目標値が実行されます.条件を満たさなければ次のRulesを判断します.
ターゲット値
iptablesは次の目標値をサポートします.
  • ACCEPT–パケットを許可する
  • DROP–パケットを破棄
  • QUEUE–パケットはユーザ空間
  • に移行する.
  • RETURN–現在のチェーンの後続Rules
  • の実行を停止
    1.2規則表
    ルール・テーブル間の優先順位:Raw->mangle->nat->filter
  • filter表、3つのチェーン:INPUT、FOrWARD、OUTPUT作用:パケットカーネルモジュールのフィルタリング:iptables_filter.
  • Nat表、三つのチェーン:PREROUTING、POSTROOUTING、OUTPUT作用:ネットワークアドレス変換(IP、ポート)カーネルモジュール:iptable_nat
  • Mangle表、五つのチェーン:PREROUTING、POSTROOUTING、INPUT、OUTPUT、FOrWARD作用:パケットのサービスタイプ、TTLを修正し、ルートを配置してQOSカーネルモジュールを実現することができる:iptable_mangle(この時計を見ないでください.私たちは戦略を設定するときはほとんど使いません)
  • Raw表、2つのチェーン:OUTPUT、PREROUTING作用:パケットがステータストラッキングメカニズムでカーネルモジュールを処理するかどうかを決定する:iptable_raw

  • 2.基本コマンド
    #   iptables    
    iptables -nvL
    
    #   22  
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
    #     IP,        
    iptables -I INPUT -s 106.54.212.96 -j DROP
    
    #   mangle/nat/raw  
    iptables -t mangle/nat/raw --list
    

    3.高度な機能
    ルールの追加
    iptables -t    -A INPUT/OUTPUT -p    -d   ip --dport      -j    
    
  • -A尾部
  • に規則を追加
  • -I挿入規則
  • -D削除規則
  • 3.1宛先アドレス変換(DNAT)
    192.168.1.1.101:8080ポートへのアクセス、192.168.1.1.22:80ポートへのアクセスへの変換
    iptables -t nat -A OUTPUT -p tcp -d 192.168.1.101 --dport 8080 -j DNAT --to-destination 192.168.1.102:80
    

    4.一般的な基本設定
    #!/bin/sh
    #    
    iptables -P INPUT ACCEPT
    #        
    iptables -F
    #         
    iptables -X
    #      0
    iptables -Z
    iptables -A INPUT -i lo -j ACCEPT
    #  22,21,80,443  
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    #  ping
    iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
    #                RELATED,  FTP   
    iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    #        
    iptables -P INPUT DROP
    #        
    iptables -P OUTPUT ACCEPT
    #        
    iptables -P FORWARD DROP
    #    
    service iptables save
    #  iptables
    systemctl restart iptables