iptables

netfilter---->iptables(工具)vim


查看指定filter表的規則 -nvL(默認是filter表)bash

[root@wy ~]# iptables -t filter -nvL網絡

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)tcp

pkts bytes target     prot opt in     out     source               destinationide

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)工具

pkts bytes target     prot opt in     out     source               destinationspa

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)rest

pkts bytes target     prot opt in     out     source               destinationip


另外兩個表nat、mangle,但最經常使用的是filter。get

filter主要用來過濾包(進包、出包),做一些規則或限制。


添加一條規則

[root@wy ~]# iptables -t filter -I INPUT -p tcp  --dport 80 -s 12.12.12.12 -j REJECT

[root@wy ~]# iptables -nvL

Chain INPUT (policy ACCEPT 36 packets, 2592 bytes)

pkts bytes target     prot opt in     out     source               destination

0     0 REJECT     tcp  --  *      *       12.12.12.12          0.0.0.0/0           tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 19 packets, 1992 bytes)

pkts bytes target     prot opt in     out     source               destination

說明:添加規則也能夠用-A ,但它們的區別是-A是添加在全部規則的後面,-I是添加在最前面。

         -I插入的規則比-A增長的規則優先生效


刪除剛添加的規則

[root@wy ~]# iptables -t filter -D INPUT -p tcp  --dport 80 -s 12.12.12.12 -j REJECT


還有另一種刪除規則方法:

iptables -nvL --line-numbers先顯示規則序號,而後iptables -D INPUT/OUTPUT 序號就能夠了


清空計數器-Z

[root@wy ~]# iptables -Z

說明:也就是包和字節都變成了0


清空所有規則-F

[root@wy ~]# iptables -F


注:若指定port的時候要加上協議,不然會報錯。


保存規則

[root@wy ~]# service iptables save


查看保存的規則

[root@wy ~]# cat /etc/sysconfig/iptables


備份與恢復

[root@wy ~]# iptables-save > 1.ipt

[root@wy ~]# iptables -F         ###恢復前先清空

[root@wy ~]# iptables-restore < 1.ipt


filter                主要用來限制進入本機的包和出去的包

nat                  主要網絡地址轉換

mangle                主要給數據包打標記


修改策略

[root@wy ~]# iptables -P INPUT ACCEPT      #針對INPUT鏈


INPUT鏈實例:要求把預設策略設成DROP,其餘兩個是ACCEPT,針對192.168.0.0/24,22端口開放,對全部的Ip的80端口放行,全部的網段21端口放行。

[root@wy ~]  vim 1.ipt.sh

#! /bin/bash

ipt="/sbin/iptables"

$ipt -F

$ipt -P INPUT DROP

$ipt -P OUTPUT ACCEPT

$ipt -P FORWARD ACCEPT

$ipt -A INPUT -s 192.168.0.0./24 -p tcp --dport 22 -j ACCEPT

$ipt -A INPUT -p tcp --dport 80 -j ACCEPT

$ipt -A INPUT -p tcp --dport 21 -j ACCEPT

WEBRESOURCEe70e24c35d5b093719510957d4599

執行腳本

[root@wy ~]  sh  1.ipt.sh

相關文章
相關標籤/搜索