第一步:先檢查是否安裝了iptables
[root@localhost ~]#
service iptables status
結(jié)果一:需要安裝
iptable
iptable-service
(執(zhí)行1.1啊央、1.2蹋半、1.3命令)image.png
結(jié)果二:不需要安裝iptable
iptable-service
image.png
1.1 安裝iptable
[root@localhost ~]#
yum install -y iptables
1.2 升級(jí)iptables
[root@localhost ~]#
yum update -y iptables
1.3 安裝iptable-service
[root@localhost ~]#
yum install -y iptables-services
第二步: 停止/禁用 自帶的firewalld服務(wù)
2.1 停止firewalld服務(wù)
[root@localhost ~]#
systemctl stop firewalld
2.2 禁用firewalld服務(wù)
[root@localhost ~]#
systemctl mask firewalld
第三步: 設(shè)置現(xiàn)有規(guī)則
3.1 查看iptables現(xiàn)有規(guī)則
[root@localhost ~]#
iptables -L -n
3.2 先允許所有(不然有可能會(huì)杯具)
[root@localhost ~]#
iptables -P INPUT ACCEPT
3.3 清空所有默認(rèn)規(guī)則
[root@localhost ~]#
iptables -F
3.3 清空所有自定義規(guī)則
[root@localhost ~]#
iptables -X
3.4 所有計(jì)數(shù)器歸0
[root@localhost ~]#
iptables -Z
3.5 允許來(lái)自于lo接口的數(shù)據(jù)包(本地訪(fǎng)問(wèn))
[root@localhost ~]#
iptables -A INPUT -i lo -j ACCEPT
3.6 開(kāi)放22端口
[root@localhost ~]#
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
3.7 允許ping唾那,開(kāi)放8端口
[root@localhost ~]#
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
第四步: 按照實(shí)際要求,添加規(guī)則
4.1 允許接受本機(jī)請(qǐng)求之后的返回?cái)?shù)據(jù) RELATED,是為FTP設(shè)置的
[root@localhost ~]#
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
4.2 其他入站一律丟棄
[root@localhost ~]#
iptables -P INPUT DROP
4.3 所有出站一律綠燈
[root@localhost ~]#
iptables -P OUTPUT ACCEPT
4.4 所有轉(zhuǎn)發(fā)一律丟棄
[root@localhost ~]#
iptables -P FORWARD DROP
第五步:其他規(guī)則設(shè)定
5.1 如果要添加內(nèi)網(wǎng)ip信任(接受其所有TCP請(qǐng)求)
[root@localhost ~]#
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
5.2 過(guò)濾所有非以上規(guī)則的請(qǐng)求
[root@localhost ~]#
iptables -P INPUT DROP
5.3 要封停一個(gè)IP哪替,使用下面這條命令:
[root@localhost ~]#
iptables -I INPUT -s ***.***.***.*** -j DROP
5.4 要解封一個(gè)IP耗帕,使用下面這條命令:
[root@localhost ~]#
iptables -D INPUT -s ***.***.***.*** -j DROP
第六步 保存規(guī)則設(shè)定
[root@localhost ~]#
service iptables save
第七步 開(kāi)啟iptables服務(wù)
7.1 注冊(cè)iptables服務(wù)
相當(dāng)于以前的chkconfig iptables on
[root@localhost ~]#
systemctl enable iptables.service
7.2 開(kāi)啟服務(wù)
[root@localhost ~]#
systemctl start iptables.service
7.3 查看狀態(tài)
[root@localhost ~]#
systemctl status iptables.service
查看
[root@localhost ~]#
vim /etc/sysconfig/iptables