centos7设置iptables

1. firewalld和iptables的区别

  • 在centos7下默认使用的是firewalld,但是在工作中,大多是时间用到的是iptables,所以推荐使用iptables

  • iptables默认每个服务都是开启的,需要拒绝才能限制;firewalld默认每个服务都是拒绝的,需要设置才能开放

  • iptables服务在 /etc/sysconfig/iptables 中储存配置;而 firewalld 将配置储存在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种 XML 文件里

  • 使用iptables 时,每一个单独更改意味着清除所有旧的规则,并从 /etc/sysconfig/iptables里读取所有新的规则;使用 firewalld 却不会再创建任何新的规则,仅仅运行规则中的不同。因此firewalld可以在运行时改变设置,而不丢失现行配置


2. centos7安装iptables

  • 关闭firewalld防火墙

CentOS 7.0默认使用firewalld作为防火墙,关闭firewalld:

// 停止firewalld
# systemctl stop firewalld.service 


// 禁止firewalld开机启动
# systemctl disable firewalld.service 


// 查看默认防火墙状态(关闭后显示not running,开启后显示running)
# firewall-cmd --state 
  • 安装iptables防火墙
// 安装iptables防火墙
# yum install -y iptables-services 

3. centos7配置iptables

我们可以通过以下两种方式来修改iptables防火墙规则

  • 直接修改/etc/sysconfig/iptables文件(注意:修改iptables文件后,需要重启iptables才能永久生效)
// 编辑防火墙配置文件
# vi /etc/sysconfig/iptables 

# sampleconfiguration for iptables service
# you can edit thismanually or use system-config-firewall
# please do not askus to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT[0:0]
:OUTPUT ACCEPT[0:0]
-A INPUT -m state--state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -jACCEPT
-A INPUT -i lo -jACCEPT

# 放开外部对本机22,80,8080端口的访问
-A INPUT -p tcp -mstate --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -jACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080-j ACCEPT

-A INPUT -j REJECT--reject-with icmp-host-prohibited
-A FORWARD -jREJECT --reject-with icmp-host-prohibited
COMMIT

注:这里使用80和8080端口为例,一般添加到“-A INPUT -p tcp -m state --state NEW -m tcp–dport 22 -j ACCEPT”行的上面或者下面,切记不要添加到最后一行,否则防火墙重启后不生效。

示例:只允许ip 10.0.200.19 访问本机5601端口

# vi /etc/sysconfig/iptables


# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

# 要注意顺序,自上而下,如果反了则全部会drop
-A INPUT -p tcp -s 10.0.200.19  --dport 5601  -j ACCEPT
-A INPUT -p tcp --dport 5601 -j DROP

COMMIT

最后重启防火墙,使配置生效

# systemctl restart iptables.service  


// 设置防火墙开机启动
# systemctl enable iptables.service  
  • 使用命令直接修改(注意:需要save生效)

iptables常用查看命令

# iptables -L -n    // 查看详细策略

# iptables -F       // 清除预设表中的所有规则链的规则

# iptables -X       // 清除预设表中使用者自定链中的规则

iptables命令行配置小结

// 同时开放本机的22和80端口
# iptables -I INPUT -p tcp -m multiport --dport 22,80 -j ACCEPT    


// 开放本机的5000-6000端口
# iptables -I INPUT -p tcp --dport 5000:6000 -j ACCEPT    


// 允许某个网段的ip访问
# iptables -I INPUT -p all -s 0.0.0.0/0 -j ACCEPT    


// 允许远程主机访问本机的8080端口
# iptables -I INPUT -s 0.0.0.0 -p tcp --dport 8080 -j ACCEPT    


// 禁止所有远程主机访问
# iptables -I INPUT -p tcp -s 0.0.0.0 -j DROP        


// 移除第七条规则
# iptables -D INPUT 7    

这里遇到了一个小问题,我一开始使用的是iptables -A来添加记录的,虽然写入到配置文件了,但是一直访问不到,最后通过使用iptables -I 添加配置,就能成功访问。问题总结:因为iptables -A是添加在最末尾的,-I是添加在第一条,所以是优先级的问题。

注:放行使用ACCEPT,禁止使用DROP。

iptables端口转发

// 将本机的80端口流量转发到8080端口
# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080    

# iptables -t nat -A OUTPUT -d localhost -p tcp --dport 80 -j REDIRECT --to-ports 8080


// 将访问本机的8282端口流量,转发到192.168.217.129的8181端口
# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8282 -j DNAT --to-destination 192.168.217.129:8181


// 如果访问hostIP 10.0.0.2的hostPort 19200端口,则把数据包转发到podIP 172.30.34.5的containerPort端口9200。
# iptables -t nat -A PREROUTING --dst 10.0.0.2 -p tcp --dport 19200 -j DNAT --to-destination 172.30.34.5:9200

3. 参考文章

https://www.jianshu.com/p/16b864d81cc8

https://www.jianshu.com/p/04e23d24e853

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值