iptables的规则介绍

Linux 的防火墙

SELinux

临时生效
setenforce 0
html

[root@jinkai ~]#
[root@jinkai ~]# getenforce
Enforcing
[root@jinkai ~]# setenforce 0
[root@jinkai ~]# getenforce
Permissivelinux

永久生效
修改配置文件:/etc/selinux/config 重启后生效
bash

[root@jinkai ~]# cat /etc/selinux/config 服务器

.# This file controls the state of SELinux on the system.
.# SELINUX= can take one of these three values:
.# enforcing - SELinux security policy is enforced.
.# permissive - SELinux prints warnings instead of enforcing.
.# disabled - No SELinux policy is loaded.
SELINUX=disabled
.# SELINUXTYPE= can take one of three values:
.# targeted - Targeted processes are protected,
.# minimum - Modification of targeted policy. Only selected processes are protected.
.# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@jinkai ~]# getenforce
Disabled网络

netfilter防火墙

netfilter和Firewalld,iptables三者之间的关系
Iptables和firewalld 都只是防火墙的管理工具,一样都是基于iptables的命令管理,真正实现防火墙功能的是netfilter
tcp

Iptables 防火墙管理工具

关闭firewalld防火墙工具,安装iptables工具ide

[root@jinkai ~]# systemctl disable firewalld //禁止firewalld 开机自启动
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@jinkai ~]# systemctl stop firewalld //关闭firewalld 服务器
[root@jinkai ~]# yum install -y iptables-services //安装iptables服务
[root@jinkai ~]# systemctl enable iptables // 开启开机自启动
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@jinkai ~]# systemctl start iptables //打开iptables服务工具

查询iptables默认规则
iptables -nvL
.net

[root@jinkai ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
29 1700 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo 0.0.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
0 0 REJECT all --
* 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibitedrest

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 17 packets, 1260 bytes)
pkts bytes target prot opt in out source destination
[root@jinkai ~]#

-nvL选项表示查看规则,-F表示临时清除当前规则,-n表示不针对ip反解析主机名,-L表示列出,-v表示列出信息更加详细。
必须使用service iptables save 保存才行,防火墙规则保存在/etc/sysconfig/iptables中;

netfilter的5个表

filter:用于过滤包,是系统预设表,最经常使用的表;有INPUT、OUTPUT、FORWARD等三个链;
nat:主要用于网络地址转换;有PREROUTING、OUTPUT、POSTROUTING等三个链;
mangle:用来给数据包作标记,而后根据标记来操做相应的包;
raw:能够实现不追踪某些数据包,默认系统的数据包都会被追踪;
security:强制访问控制(MAC)的网络规则;

netfilter的5个链

PREROUTING:数据包进入路由表以前;
INPUT:经过路由表后目的地为本机;
FORWARD:经过路由表,目的地部位本机;
OUTPUT:有本机产生,向外转发;
POSTROUTING:发送到网卡接口以前;
详细能够参考:https://www.cnblogs.com/metoy/p/4320813.html

iptables基本语法

-A/-D:增长或删除一条规则;
-I:插入一条规则;
-F:清空规则;
-Z:清空计数,从新开始计数;
-t:指定表,后面必须带参数表名,-t nat;
-n:不针对ip反解析主机名;
-v:更加详细的信息;
-L:列出,与-v一块儿使用;
-p:表示指定协议,能够是tcp、udp、icmp;
--dport:跟-p一块儿使用,表示指定目标端口;
--sport:跟-p一块儿使用,表示指定源端口;
-s:表示指定源ip(能够是一个网段)
-d:表示指定目的ip(能够是一个网段)
-j:后面跟动做,其中ACCEPT表示容许包、DROP表示丢掉包、REJECT表示拒绝包;
-i:表示指定网卡(不经常使用);

清空和查看防火墙规则
iptables -F 把全部规则删除,不加-t 指定表,默认删除filter表;
service iptables save 保存到文件,重启生效;

[root@jinkai ~]# iptables -F
[root@jinkai ~]# iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 364 bytes)
pkts bytes target prot opt in out source destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 4 packets, 368 bytes)
pkts bytes target prot opt in out source destination
[root@jinkai ~]#

查看指定表

iptables -t nat 

查看指定nat表,-t 参数就是指定表;

iptables -t nat -nvL 查看net表规则;
[root@jinkai ~]# iptables -t nat -nvL

清空包以及流量计数器归零

iptables -Z

增长规则
-A:增长规则,增长的规则通常在最后面
增长指定源ip以及端口拒绝访问目标ip的某端口

iptables -A INPUT -s 192.168.111.136 -p tcp --sport 1234 -d 192.168.111.137 --dport 80 -j DROP
未来源ip 192.168.111.136 的1234端口 访问192.168.111.137 的80端口 拒绝掉
[root@jinkai ~]# iptables -A INPUT -s 192.168.111.136 -p tcp --sport 1234 -d 192.168.111.137 --dport 80 -j DROP
[root@jinkai ~]# iptables -nvL
Chain INPUT (policy ACCEPT 40 packets, 2320 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- 192.168.111.136 192.168.111.137 tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 21 packets, 1660 bytes)
pkts bytes target prot opt in out source destination
[root@jinkai ~]#

插入规则
-I:插入规则,插入的规则通常在最前面
iptables -I INPUT -p tcp --dport 80 -j DROP

将拒绝全部的ip访问本机的80端口
[root@jinkai ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@jinkai ~]# iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 364 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 DROP tcp -- 192.168.111.136 192.168.111.137 tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 4 packets, 480 bytes)
pkts bytes target prot opt in out source destination
[root@jinkai ~]#

删除规则
-D:删除

iptables -D INPUT -p tcp --dport 80 -j DROP

删除掉已知道命令的规则

[root@jinkai ~]# iptables -D INPUT -p tcp --dport 80 -j DROP

删除未知命令的规则
iptables -nvL --line-number
显示规则的序列号num

[root@jinkai ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 121 packets, 7052 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- 192.168.111.136 192.168.111.137 tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 64 packets, 5632 bytes)
num pkts bytes target prot opt in out source destination
[root@jinkai ~]#

iptables -D INPUT 1
删除INPUT链中序列号为1的规则,
[root@jinkai ~]# iptables -D INPUT 1
修改链的默认规则
-P 后面跟链的名称,策略DROP
[root@jinkai ~]# iptables -P INPUT DROP

保存与备份规则

保存:

service iptables save
将规则保存到/etc/sysconfig/iptables

备份:

iptables-save > my.ipt
将iptables的规则备份到my.ipt文件中

恢复备份规则

iptables-restore < my.ipt
将文件中的规则恢复到iptables中

案例需求:只针对filter表,预设策略INPUT链DROP,其余两个链ACCEPT,而后针对192.168.188.0/24开通22端口,对全部网段开放80端口,对全部网段开放21端口
多条规则,写成脚本的形式执行

.#cat /usr/local/sbin/iptables.sh
#! /bin/bash
ipt="/usr/sbin/iptables"
$ipt -F
$ipt -P INPUT DROP
$ipt -P OUTPUT ACCEPT
$ipt -P FORWARD ACCEPT
$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$ipt -A INPUT -s 192.168.188.0/24 -p tcp --dport 22 -j ACCEPT
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT

完成脚本的编写后,直接运行/bin/sh /usr/local/sbin/iptables.sh便可。若是想开机启动时初始化防火墙规则,则须要在/etc/rc.d/rc.local中添加一行/bin/sh /usr/local/sbin/iptables.sh。

[root@jinkai ~]# sh /usr/local/sbin/iptables.sh
[root@jinkai ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
28 1624 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT tcp -- 192.168.188.0/24 0.0.0.0/0 tcp dpt:22
0 0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 15 packets, 1180 bytes)
pkts bytes target prot opt in out source destination
[root@jinkai ~]#
了解更详细能够参考:https://blog.csdn.net/weixin_47151717/article/details/107736865

相关文章
相关标签/搜索