cebtos 7 iptable

一、配置防火牆,開啓80端口、3306端口
CentOS 7.0默認使用的是firewall作爲防火牆,這裏改爲iptables防火牆。
1、關閉firewall:
#停止firewall服務
systemctl stop firewalld.service
#禁止firewall開機啓動
systemctl disable firewalld.service


2、安裝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! #保存退出

#最後重啓防火牆使配置生效
systemctl restart iptables.service
#設置防火牆開機啓動
systemctl enable iptables.service


二、關閉SELINUX
#修改配置文件
vi /etc/selinux/config

#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增加
:wq! #保存退出
#使配置立即生效
setenforce 0

iptables -nL --line-number 查看iptable

service iptables status  查看防火牆狀態

1、查看
iptables -nvL –line-number

-L 查看當前表的所有規則,默認查看的是filter表,如果要查看NAT表,可以加上-t NAT參數
-n 不對ip地址進行反查,加上這個參數顯示速度會快很多
-v 輸出詳細信息,包含通過該規則的數據包數量,總字節數及相應的網絡接口
–line-number 顯示規則的序列號,這個參數在刪除或修改規則時會用到
2、添加
添加規則有兩個參數:-A和-I。其中-A是添加到規則的末尾;-I可以插入到指定位置,沒有指定位置的話默認插入到規則的首部。

當前規則:

[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變成了第四行。

3、刪除
刪除用-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

4、修改
修改使用-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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章