Linux 服务管理-iptables

网络拓扑图

安装

(要么firewall,要么iptables,不用删除,选其一即可)

systemctl   disable --now  firewalld  

yum install  iptables-services -y

systemctl    enable --now iptables

systemctl    status            iptables

清除查看规则

(默认为filter表)

(清除所有规则)

iptables -F

(清空其它表所有规则)

iptables  -t  nat         -F
iptables  -t  raw        -F
iptables  -t  mangle  -F 

(查看默认规则)

(-t   指定表,默认filter表)

iptables -nL

iptables  -t   nat   -nL

备份还原规则

iptables-save > /etc/sysconfig/iptables
iptables-save > firewall.bak
iptables-restore < firewall.bak

设置默认策略

(防止把自己锁在外面,先执行,切记! )

iptables -I INPUT -p tcp --dport 22 -j ACCEPT

(默认为filter表)

(默认链策略是ACCEPT)

iptables  -P  INPUT                 DROP
iptables  -P  OUTPUT             ACCEPT
iptables  -P  FORWARDING   ACCEPT

追加/删除/插入规则

追加

cat  /etc/services | grep ssh

iptables -I INPUT   -p   tcp --dport 80        -j ACCEPT 
iptables -I INPUT   -p   tcp --dport 22        -j ACCEPT

iptables -I INPUT   -p   tcp --dport 80,22   -j ACCEPT

iptables -I INPUT   -p   icmp   -j   REJECT
iptables -I INPUT   -p   icmp   -j   DROP 

REJECT:Destination Host Prohibited

[root@wai ~]# ping 192.168.170.170
PING 192.168.170.170 (192.168.170.170) 56(84) bytes of data.
From 192.168.170.170 icmp_seq=1 Destination Port Unreachable

DROP:无提示卡住

[root@wai ~]# ping 192.168.170.170
PING 192.168.170.170 (192.168.170.170) 56(84) bytes of data.


 

添加一条规则, 不允许所有,必要允许,其它拒绝,放在最后一条,慎用!
iptables -A INPUT  -j  REJECT

iptables -A INPUT  -j  DROP

删除

查看规则并显示序号

iptables -nL  --line-numbers

删除INPUT链序号3规则

iptables -D INPUT 3

插入

查看规则并显示序号

iptables -nL  --line-numbers

在INPUT链序号4规则后面插入一条规则
iptables -I INPUT 4 -p icmp -j ACCEPT 

limit

限制单位时间内连接的数据包个数,500个/秒,超过拒绝连接

iptables -I INPUT -m limit --limit 500/sec -j ACCEPT
iptables -P INPUT DROP

拒绝转发关键词、敏感信息数据包

iptables -I FORWARD -m string --algo bm--string "/etc/passwd" -j REJECT
iptables -I FORWARD -m string --algo bm--string "qq" -j REJECT

state

状态检测,放行所有出站数据包,放行入站的回应数据包。拒绝入站的新连接请求与无效连接

状态检测:NEW、ESTABLISHED、RELATED、INVALID

除了从本机出去的数据包由NAT表的OUTPUT链处理外,其它所有的状态检测都在NAT表中的PREROUTING链中处理,具体状态信息如下:

1、NEW状态:由外主动发起连接的第一个数据包

2、ESTABLISHED状态:由内主动发起并收到对方响应返回的第一个数据包,从NEW变为ESTABLISHED,并且该状态会继续匹配这个连接后继数据包

3、RELATED状态:当一个数据包的状态处于ESTABLISHED状态的连接有关系的时候,就会被认为是RELATED,也就是说一个连接想要是RELATED状态,首先要有一个ESTABLISHED的连接(承接ESTABLISHED)

4、INVALID状态:不能被识别属于哪个连接状态或没有任何关系的状态,都是被拒绝的

iptables -A INPUT -m state --state NEW         -j DROP
iptables -A INPUT -m state --state INVALID    -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED  -j  ACCEPT

禁止网络嗅探

iptables -A INPUT -p tcp --tcp-flags hosts FIN,URG,PSH    -j    DROP

iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST     -j    DROP

iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN        -j    DROP

iptables -A INPUT -p tcp --tcp-flags SYN,SYN --dport 80     -j    DROP

snat

iptables --table nat -F

开启路由转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p

POSTROUTING:路由选择后处理数据包

$ 固定ip
iptables -t nat -I  POSTROUTING  -s 192.168.180.0/24  -j  SNAT  --to-source 20.20.20.21

or
iptables -t nat -A POSTROUTING  -o    ens190               -j  SNAT  --to-source 20.20.20.21

$ 动态ip

iptables -t nat -I  POSTROUTING  -s 192.168.180.0/24 -o ens160   -j   MASQUERADE

$ 查看
iptables --table nat  -nL

dnat

PREROUTING:路由选择前处理数据包

iptables -t nat -I PREROUTING -d 192.168.170.170  -p tcp  --dport 80 -j DNAT --to-destination 192.168.180.181

查看
iptables --table nat -nL

  • 35
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lylaotang

你的鼓励就是我创作的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值