cebtos 7 iptable

1、配置防火墙,开启80端口、3306端口
CentOS 7.0默认使用的是firewall做为防火墙,这里改成iptables防火墙。
一、关闭firewall:
#中止firewall服务
systemctl stop firewalld.service
#禁止firewall开机启动
systemctl disable firewalld.servicelinux


二、安装iptables防火墙
#安装
yum install iptables-services
#编辑防火墙配置文件
vi /etc/sysconfig/iptables网络


# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited 默认拒绝全部
COMMIT
:wq! #保存退出tcp

#最后重启防火墙使配置生效
systemctl restart iptables.service
#设置防火墙开机启动
systemctl enable iptables.serviceide


2、关闭SELINUX
#修改配置文件
vi /etc/selinux/configthis

#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增长
:wq! #保存退出
#使配置当即生效
setenforce 0rest

iptables -nL --line-number 查看iptablecode

service iptables status  查看防火墙状态orm

一、查看
iptables -nvL –line-number接口

-L 查看当前表的全部规则,默认查看的是filter表,若是要查看NAT表,能够加上-t NAT参数
-n 不对ip地址进行反查,加上这个参数显示速度会快不少
-v 输出详细信息,包含经过该规则的数据包数量,总字节数及相应的网络接口
–line-number 显示规则的序列号,这个参数在删除或修改规则时会用到
二、添加
添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I能够插入到指定位置,没有指定位置的话默认插入到规则的首部。ip

当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.4          0.0.0.0/0

添加一条规则到尾部:

[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

再插入一条规则到第三行,将行数直接写到规则链的后面:

[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

查看:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.3          0.0.0.0/0
4    DROP       all  --  192.168.1.4          0.0.0.0/0
5    DROP       all  --  192.168.1.5          0.0.0.0/0

能够看到192.168.1.3插入到第三行,而原来的第三行192.168.1.4变成了第四行。

三、删除
删除用-D参数

删除以前添加的规则(iptables -A INPUT -s 192.168.1.5 -j DROP):

[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP

有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时咱们能够先使用–line-number找出该条规则的行号,再经过行号删除规则。

[root@test ~]# iptables -nv --line-number
iptables v1.4.7: no command specified
Try `iptables -h' or 'iptables --help' for more information.
[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.3          0.0.0.0/0

删除第二行规则

[root@test ~]# iptables -D INPUT 2

四、修改
修改使用-R参数

先看下当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.5          0.0.0.0/0

将第三条规则改成ACCEPT:

[root@test ~]# iptables -R INPUT 3 -j ACCEPT

再查看下:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

第三条规则的target已改成ACCEPT。

相关文章
相关标签/搜索