linux Centos 配置iptables

这是小编通过在课堂上学习iptables来的一些基本总结。

(1)查看本机iptables的配置情况 

   iptables -L -n

[root@localhost ~]$ iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)

这是在没有开启iptables时看到的状态

(2)开启iptables服务

   service iptables start

 开启后在查看

[root@localhost ~]$ service iptables start
iptables:应用防火墙规则:[确定]
[root@localhost ~]$ iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

 (3)根据博主的学习内容:先查看filter 表的INPUT链

  iptables -t filter -L INPUT

[root@localhost ~]$ iptables -t filter -L INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp-data 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp 
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

这里的 -t 表示选择表,因为iptables有三张表,这里选择filter表,我这里只做一些基本操作,如果对iptables没什么基本概念的可以先去看一下iptables的基本理论

-L代表操作命令,后面再详细讲解,这里是列出filter表中INPUT链的所有规则

 

(3)iptables的常用的操作命令

-A 或 --append :将一条规则添加到链尾

-D 或 --delete :用-D 指定的规则或指定的规则在链表中的位置编号,该命令从链表中删去

-R :修改指令,修改链中指定的规则

-N 或 --new-chain :该命令中指定的名称创建一个新链

-F 或 --flush :如果指定链名,则该命令删除链中的所有规则 。如果为指定链名,则该命令删除所有链的所有规则(慎用)用于快速清除

-L 或 --list :列出指定链中的所有规则,我们前面有说到过嘛

-I :表示添加一条规则,但是可以指定添加的位置,在添加的链表后面可以加一个数字,表示将此规则添加到第几行,后面举例详解

–line-number :显示规则的序列号,这个参数在删除或修改规则时会用到

 

**注意一点,使用这些操作命令的时候,统统用大写**

**再注意一点,在各条命令中当要指定链名称的时候,一定要用全大写,例如我们上面查看的INPUT链就是全大写,不然会识别不了**

**对表的操作是大写,对链的操作是小写:这样子记住就行了

 

(4)两个重要的动作命令

   是对指定规则的接受还是不接受,其中都会用到-j这个指令,不好解释,后面实例说明

 DROP :表示阻止

 ACCEPT: 表示接受

为了好理解,后面会举例说明。

**还说明一点,各规则间也是有优先级的,因为我也是个初学者,所以深入的东西我还没接触什么,这里就不做多的介绍了。

 

(5)后面都是例子来解释说明了,这样好理解

  1、在filter表的INPUT链添加一条规则,内容是不接受其他主机ping它

iptables -t filter -A INPUT -p icmp -j DROP

[root@localhost ~]$ iptables -t filter -A INPUT -p icmp -j DROP
[root@localhost ~]$ iptables -t filter -L INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp-data 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp 
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 
DROP       icmp --  anywhere             anywhere 

 我添加一条规则后再查询filter的INPUT链,可以看到在链的末尾增添了一条刚添加的规则,这个时候再用其他主机来ping它就ping不通了

 

2、删除刚才添加的那条规则

iptables -t filter -D INPUT 

我们通过查找行号来删除

先查询一下iptables表的各规则的行号

[root@localhost ~]$ iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
4    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
8    DROP       icmp --  0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination  

 可以看到我们刚才插入的icmp规则在第8行,下面删除它

iptables -t filter -D INPUT 8

[root@localhost ~]$ iptables -t filter -D INPUT 8
[root@localhost ~]$ iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
4    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

 可以看到表尾刚插入的规则就已经删掉了

 

3、下面我们通过行号来插入规则,还是用之前那条禁止ping的规则 ,我们插入到链的第一行

 iptables -t filter -I INPUT 1 -p icmp -j DROP

[root@localhost ~]$ iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    DROP       icmp --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
5    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
8    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

 可以看到规则已经插入第一行

 

4、添加一条规则禁止指定的ip 来ping它

iptables -t filter -A INPUT -p icmp -s 192.168.128.213/24 -j DROP 

-s:指定主机

这里因为只有一台主机,不好演示

 

5、添加一条规则拒绝某台主机访问某个端口

iptables -t filter -A INPUT -p tcp --dport 80 -s 172.16.41.196/24 -j DROP

用的tcp协议,--dport后跟端口号。

命令都是变通的,基本的格式和基本的命令大概就这样,大家熟练这些简单命令后,变通一下,在再去看高级些的命令会更有帮助。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值