防火墙firewall和iptables的使用 —— Linux系统

防火墙是整个数据包进入主机前的第一道关卡。
Linux中有两种防火墙软件,ConterOS 7.0以上使用的是 firewallConterOS 7.0以下使用的是 iptables,本文将分别介绍两种防火墙软件的使用。

1.验证本机系统使用的是哪个防火墙

​​systemctl status iptables.service       #查看 iptables 防火墙的状态
systemctl status firewalld.service      #查看 firewall 防火墙的状态

​

2.使用 防火墙——firewalld

2.1 安装 firewalld

	yum install firewalld         #安装 firewalld 防火墙

	systemctl unmask firewalld    #解除屏蔽

	systemctl stop iptables       #停止 iptables 防火墙

2.2 基本操作

	systemctl status firewalld      #查看防火墙状态

	systemctl start firewalld       #启动防火墙

	systemctl stop firewalld        #关闭防火墙

	systemctl restart firewalld     #重启防火墙

	systemctl enable firewalld      #设置开机自启

	systemctl disable firewalld     #禁用开机启动

2.3 对端口的操作

	firewall-cmd --zone=public --add-port=8080/tcp --permanent      #开放8080端口

	firewall-cmd --zone=public --remove-port=8080/tcp --permanent   #关闭8080端口

	firewall-cmd --reload     #更新配置信息(这样就不需要重启防火墙了)

2.4 查看操作

​
	firewall-cmd --list-ports     #查看开放的端口

	systemctl list-unit-files | grep enabled    #查看所有开机自启的服务

​

3.使用 防火墙——iptables

3.1 安装 iptables

​
	systemctl stop firewalld       #停止 firewalld 防火墙

	systemctl mask firewalld       #屏蔽 firewalld 防火墙

	yum -y install iptables        #安装 iptables

	yum -y install iptables-services      #安装 iptables-services

​

3.2 基本操作

	systemctl start iptables.service       #启动 iptables 防火墙

	systemctl status iptables.service      #查看 iptables 防火墙状态

	systemctl enable iptables.service      #设置开机自启

	systemctl disable iptables.service     #禁用开机自启

3.3 对端口的操作

  • 3.3.1 放行端口(两种方式)
方式一:
   1.编辑 /etc/sysconfig/iptables 文件。执行命令:vim /etc/sysconfig/iptables
   2.复制 22号端口那一行,并粘贴到下一行,修改22为8080
   3.写入保存并退出
   4.systemctl restart iptables 重启iptables服务(这样8080端口就被开放了)
方式二:
    #开启8080端口
    iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT      
    service iptables save  #保存配置信息(如果不执行保存,系统重启后8080端口将失效)  
    systemctl restart iptables #重启iptables服务
  • 3.3.2 删除端口
删除对应的规则,就要知道对应的编号
     iptables -L -n --line-number   #显示规则和相对应的编号
 -- 
如果 8080 端口规则编号为6
     iptables -D INPUT 6      #删除规则编号为6的端口
     service iptables save    #保存配置信息(如果不执行保存,重启后端口还是会被开放)

3.4 查看操作

iptables -L -n       #查看 iptables 开放的端口

本次分享到此结束,感谢大家的阅读! 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小蜜蜂vs码农

你的鼓励将是我创作的最大动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值