linux firewalld防火墙设置

1、开启防火墙

systemctl start firewalld

2、开放指定端口
命令说明:
–zone 作用域
–add-port=8080/tcp 添加端口,格式为:端口/通讯协议
–permanent 永久生效,没有此参数重启后失效

firewall-cmd --zone=public --add-port=8080/tcp --permanent

3、重启防火墙

firewall-cmd --reload

查看状态

systemctl status firewalld

停止

systemctl disable firewalld

禁用

systemctl stop firewald

iptables 设置方式

安装

yum install iptables-services

查看状态

service iptables status

停止防火墙

service iptables stop

启动防火墙

service iptables start

重启防火墙

service iptables restart

查看通过的端口

iptables -L -n

iptables
参数 说明

-A 参数就看成是添加一条 INPUT OUTPUT 的规则
-p 指定是什么协议,tcp、udp 协议
--dport 就是目标端口 服务器的端口
--sport 就是数据源端口 访问者的端口
-j 就是指定是 ACCEPT 接收 或者 DROP 不接收
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

或者

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

iptables -A OUTPUT -p tcp --sport 8080 -j ACCEPT

重启

service iptables restart

其他知识
过滤无效的数据包iptables 提供了一个参数 是检查状态的,配置下 22 和 80 端口,防止无效的数据包
-m state --state <状态>

INVALID 无效的封包,例如数据破损的封包状态

ESTABLISHED:已经联机成功的联机状态

NEW:想要新建立联机的封包状态

RELATED:表示这个封包是与我们主机发送出去的封包有关

iptables -AINPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

多个规则 可以使用逗号隔开

iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT


iptables -A OUTPUT -p tcp --sport 80-m state --state RELATED,ESTABLISHED -j ACCEPT

禁止某个IP访问

 iptables -A INPUT -p tcp -s 192.168.1.100 -j DROP

查看规则

iptables -L -n

OR

iptables -L -n --line-number 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
3    INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
4    INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
5    INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
6    DROP       all  --  0.0.0.0/0            0.0.0.0/0            ctstate INVALID
7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
8    DROP       tcp  --  192.168.1.2          0.0.0.0/0     

删除规则,按照ID(1 2 3 …) 删除

iptables -D OUTPUT 1 

设置好规则 或者删除规则后 记得保存

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值