Linux防火墙Firewall和Iptables的使用
Linux中有两种防火墙软件,ConterOS7.0以上使用的是firewall,ConterOS7.0以下使用的是iptables,本文将分别介绍两种防火墙软件的使用。
Firewall
- 开启防火墙:
systemctl start firewalld
- 关闭防火墙:
systemctl stop firewalld
- 查看防火墙状态:
systemctl status firewalld
- 设置开机启动:
systemctl enable firewalld
- 禁用开机启动:
systemctl disable firewalld
- 重启防火墙:
firewall-cmd --reload
- 开放端口(修改后需要重启防火墙方可生效):
firewall-cmd --zone=public --add-port=8080/tcp --permanent
![](http://upload-images.jianshu.io/upload_images/1939592-03ce74c31a3c3800.png?imageMogr2/auto-orient/strip|imageView2/2/w/859/format/webp)
展示图片
- 查看开放的端口:
firewall-cmd --list-ports
![](http://upload-images.jianshu.io/upload_images/1939592-37126aac7c92a308.png?imageMogr2/auto-orient/strip|imageView2/2/w/532/format/webp)
展示图片
- 关闭端口:
firewall-cmd --zone=public --remove-port=8080/tcp --permanent
![](http://upload-images.jianshu.io/upload_images/1939592-777a090350564b8a.png?imageMogr2/auto-orient/strip|imageView2/2/w/889/format/webp)
展示图片
Iptables
安装
由于CenterOS7.0以上版本并没有预装Iptables,我们需要自行装。
-
安装前先关闭firewall防火墙
展示图片 - 安装iptables:
yum install iptables
- 安装iptables-services:
yum install iptables-services
使用
- 开启防火墙:
systemctl start iptables.service
![](http://upload-images.jianshu.io/upload_images/1939592-f45c313f15cb70db.png?imageMogr2/auto-orient/strip|imageView2/2/w/994/format/webp)
展示图片
- 关闭防火墙:
systemctl stop iptables.service
- 查看防火墙状态:
systemctl status iptables.service
- 设置开机启动:
systemctl enable iptables.service
- 禁用开机启动:
systemctl disable iptables.service
- 查看filter表的几条链规则(INPUT链可以看出开放了哪些端口):
iptables -L -n
![](http://upload-images.jianshu.io/upload_images/1939592-0b61e997db286b3c.png?imageMogr2/auto-orient/strip|imageView2/2/w/952/format/webp)
展示图片
- 查看NAT表的链规则:
iptables -t nat -L -n
![](http://upload-images.jianshu.io/upload_images/1939592-2adb0eda53c8e483.png?imageMogr2/auto-orient/strip|imageView2/2/w/538/format/webp)
展示图片
- 清除防火墙所有规则:
iptables -F
iptables -X
iptables -Z
- 给INPUT链添加规则(开放8080端口):
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
![](http://upload-images.jianshu.io/upload_images/1939592-d4a6a815f661becf.png?imageMogr2/auto-orient/strip|imageView2/2/w/958/format/webp)
展示图片
- 查找规则所在行号:
iptables -L INPUT --line-numbers -n
![](http://upload-images.jianshu.io/upload_images/1939592-594b4b72994ea676.png?imageMogr2/auto-orient/strip|imageView2/2/w/1011/format/webp)
展示图片
- 根据行号删除过滤规则(关闭8080端口):
iptables -D INPUT 1
![](http://upload-images.jianshu.io/upload_images/1939592-27ac7d8dd351a921.png?imageMogr2/auto-orient/strip|imageView2/2/w/956/format/webp)
展示图片
作者:梦想de星空
链接:https://www.jianshu.com/p/2d6e53924546
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
作者:朱祁林
出处:http://zhuqil.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。