iptables

#####1.火墙介绍#######################

1.netfilter

2.iptables

3.iptables | firewalld

#####2.火墙管理工具切换#################

在rhel8中默认使用的是firewalld

[root@rhel8_node1 ~]# dnf install iptables-services.x86_64 -y

firewalld --- iptables

[root@rhel8_node1 ~]# systemctl stop firewalld

[root@rhel8_node1 ~]# systemctl disable firewalld 

[root@rhel8_node1 ~]# systemctl mask firewalld

[root@rhel8_node1 ~]# systemctl enable iptables.service

注意:

iptables --- firewalld

systemctl stop iptables

systemctl disable iptables

systemctl mask iptables

systemctl enable firewalld

#####3.iptables的使用###############

[root@rhel8_node1 ~]# iptables -L    ##查看

[root@rhel8_node1 ~]# vim /etc/sysconfig/iptables    ##iptables策略记录文件

永久保存策略:

[root@rhel8_node1 ~]# iptables -F   ##刷新

[root@rhel8_node1 ~]# iptables -nL

[root@rhel8_node1 ~]# systemctl restart iptables.service    ##重启之后 策略恢复

[root@rhel8_node1 iptables -L

方法:

[root@rhel8_node1 ~]# iptables -F

[root@rhel8_node1 ~]# iptables-save > /etc/sysconfig/iptables    ##将火墙信息导入

或者:

[root@rhel8_node1 ~]# service iptables save

测试:

[root@rhel8_node1 ~]# systemctl restart iptables.service

[root@rhel8_node1 ~]# iptables -nL

#####4.火墙默认策略#####################

默认策略中的五条链:

input输入
output输出
forward转发
postrouting路由之后
prerouting路由之前

 

 

 

 

 

 

默认三张表:

filter经过本机内核的数据(input output forward)
nat不经过内核的数据(input output postrouting prerouting)
mangle当filter和nat表不够用时使用(input output postrouting prerouting forward)

 

 

 

 

iptables命令:

iptables

-t指定表名称
-n不做解析
-L查看
-A添加策略
-p协议
--dport目的地端口
-s来源
-j(后可跟以下五条)动作
        ACCEPT允许
        DROP丢弃
        REJECT拒绝
        SNAT源地址转换
        DNAT目的地地址转换
-N新建链
-E更改链名称
-X删除链
-D删除规则
-I插入规则
-R更改规则
-P更改默认规则

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

例子:

[root@rhel8_node1 ~]# iptables -t filter -L

[root@rhel8_node1 Desktop]# watch -n 1 iptables -t filter -nL   ##监控面板

[root@rhel8_node1 Desktop]# iptables -A INPUT -j REJECT

[root@rhel8_node1 Desktop]# iptables -D INPUT 1

[root@rhel8_node1 Desktop]# iptables -A INPUT -j DROP

[root@rhel8_node1 Desktop]# iptables -A INPUT -s 192.168.3.109 -j ACCEPT    ##按照顺序执行

[root@rhel8_node1 Desktop]# iptables -I INPUT -s 192.168.3.109 -j ACCEPT    ##插入默认第一条

[root@rhel8_node1 Desktop]# iptables -I INPUT -s 192.168.3.109 -p tcp --dport 22 -j ACCEPT

[root@rhel8_node1 Desktop]# iptables -R INPUT 1 -s 192.168.3.109 -p tcp --dport 80 -j ACCEPT

[root@rhel8_node1 Desktop]# iptables -P INPUT DROP

[root@rhel8_node1 Desktop]# iptables -N redhat

[root@rhel8_node1 Desktop]# iptables -E redhat  REDHAT

[root@rhel8_node1 Desktop]# iptables -X REDHAT

数据包状态:

RELATED建立过连接的
ESTABLISHED正在连接的
NEW新的

 

 

 

 

[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -i lo -j ACCEPT

[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT

[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW ! -s 192.168.3.109 -p tcp --dport 22 -j ACCEPT  ##除了109之外的

[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -j REJECT

测试:

[root@rhel7 ~]# ping 192.168.3.110   ##无法ping通

[root@rhel7 ~]# ssh root@192.168.3.110    ##无法连接

[root@test Desktop]# ssh root@192.168.3.110   ##可连接

[root@rhel8_node1 Desktop]# service iptables save    ##保存火墙策略

 

数据转发:

[root@rhel8_node1 ~]# nmcli connection add con-name westos1 ifname ens224 type ethernet ip4 172.25.254.110/24  ##添加网卡

[root@test Desktop]# nmcli connection modify uuid ea74cf24-c2a2-ecee-3747-a2d76d46f93b ip4 172.25.254.120/24

[root@test Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-ens160

[root@test Desktop]# systemctl restart NetworkManager

[root@test Desktop]# nmcli connection down ens160

[root@test Desktop]# nmcli connection up ens160

[root@test Desktop]# route -n

nat表中的dnat snat

[root@rhel8_node1 Desktop]# watch -n 1 iptables -t nat -nL

snat:

[root@rhel8_node1 Desktop]# iptables -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.3.110

测试:

[root@test Desktop]# ping 192.168.3.109    ##可ping通

[root@rhel7 ~]# w -i

dnat:

[root@rhel8_node1 Desktop]# iptables -t nat -A PREROUTING -i ens160 -p tcp --dport 22 -j DNAT --to-dest 172.25.254.120

测试:

[root@rhel7 ~]# ssh root@192.168.3.110

[root@test ~]# ifconfig

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值