• iptables命令参数

1、清理参数

[root@localhost~]# /etc/init.d/iptables start 启动iptables

[root@localhost~]# /etc/init.d/iptables status 查看iptables状态

[root@localhost~]# iptables -V 查看版本信息

[root@localhost~]# iptables -h 查看帮助信息

[root@localhost~]# iptables -L -n 查看默认filter表的规则

[root@localhost~]# iptables  -F 清除所有的规则

[root@localhost~]# iptables  -X 删除用户自定义的链

[root@localhost~]# iptables  -Z 把链的计数器清零

2      、禁止规则

  • 语法:

iptables -t[table] -[AD] chain rule-specification [options]

注:基本的处理行为:ACCEPT(接收)、DROP(丢弃)、REJECT(拒绝)。DROP好于REJECT

    命令行执行的规则只在内存临时生效。

具体命令:

关掉、开启22端口:

iptables -t filter-A INPUT -p tcp  --dport 22 -j DROP 关闭22端口

iptables -F 清楚自定义规则

禁止、开启80端口:

iptables -t filter-A INPUT -p tcp  --dport 80 -j DROP

iptables -L -n--line-numbers

Chain INPUT(policy ACCEPT)

num  target    prot opt source               destination        

1    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

iptables -t filter-D INPUT 1

iptables -L -n --line-numbers--line-numbers的好处是会在列表前面加序号,在使用iptables -t filter -D INPUT 1时直接添加序号1就可以清除此条规则。

2) 【注意】

-A-I的区别:

-A是添加规则到指定链的结尾

-I是添加规则到指定链的开头

例如:

-A添加如下

[root@localhost~]# iptables -t filter -A INPUT -p tcp --dport 80 -j DROP

[root@localhost~]# iptables -t filter -A INPUT -p tcp --dport 81 -j DROP

[root@localhost~]# iptables -t filter -A INPUT -p tcp --dport 82 -j DROP

[root@localhost~]# iptables -t filter -A INPUT -p tcp --dport 79 -j DROP

[root@localhost~]# iptables -L -n --line-numbers

ChainINPUT (policy ACCEPT)

num  target    prot opt source               destination        

1    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

2    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:81

3    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:82

4    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:79

[root@localhost~]# iptables -F

-I添加如下:

[root@localhost~]# iptables -t filter -I INPUT -p tcp --dport 80 -j DROP

[root@localhost~]# iptables -t filter -I INPUT -p tcp --dport 81 -j DROP

[root@localhost~]# iptables -t filter -I INPUT -p tcp --dport 82 -j DROP

[root@localhost~]# iptables -t filter -I INPUT -p tcp --dport 79 -j DROP

[root@localhost~]# iptables -L -n --line-numbers

ChainINPUT (policy ACCEPT)

num  target    prot opt source              destination        

1    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:79

2    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:82

3    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:81

4    DROP      tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

    以后封IP-I来封

    iptables -t filter -A INPUT 2 -p tcp --dport 79 -j DROP表示将此条记录插到第二条记录上面去。INPUT后面加数字就表示插到哪里。

3)

-i 接口

-s 源地址

iptables -t filter-A INPUT -i eth0 -s 10.0.0.1/24 -j DROP

或者iptables -t filter -A INPUT -i eth0 -s10.0.0.1 -j DROP

iptables -F

iptables -t filter-A INPUT -i eth0 ! -s 192.168.58.131 -j DROP

禁止除192.168.58.131以外的所有IP

iptables -F

4) 禁止ping功能:

iptables -A INPUT-p icmp --icmp-type 8 -s 0/0 -j DROP