CentOS
- 一、CentOS7以下版本
- 1.1 修改防火墙配置
- 1.2 关闭防火墙
- 二、CentOS7版本
- 2.1 查看默认防火墙状态
- 2.2 停止firewall
- 2.3 启动firewall
- 2.4 查看firewall服务是否开机启动
- 2.5 端口命令
- 2.6 重启防火墙
- 三、CentOS7版本下使用iptables防火墙
- 3.1 从yum源中下载iptables-services
- 3.2 关闭firewall防火墙
- 3.3 设置 iptables service
- 四、卸载最后
一、CentOS7以下版本
COS7以下版本默认用防火墙Iptables管理所有的端口,只启用默认远程连接22端口其他都关闭。
1.1 修改防火墙配置
将开启的端口加入防火墙白名单中
vim /etc/sysconfig/iptables
增加下面代码
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 8080 -j ACCEPT
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 3306 -j ACCEPT
保存退出,重启防火墙使配置生效:service iptables restart
或者也可以用命令行直接实现:
比如要开放80,22,8080 端口,输入以下命令即可:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
然后保存:
/etc/rc.d/init.d/iptables save
重启:service iptables restart
开启防火墙自动启动 chkconfig iptables on
状态:查看防火墙状态:service iptables status
- Active: inactive (dead) --表示防火墙已经关闭
查看打开的端口:/etc/init.d/iptables status
测试:
1.2 关闭防火墙
临时关闭防火墙 service iptables stop
(永久该关闭防火墙)关闭防火墙自动启动 chkconfig iptables off
二、CentOS7版本
在COS7版本开始,默认使用的是firewall作为防火墙,而不再是iptables,且在指令上弃用了service和chkconfig,改用systemctl来作为统一的替代,这是很多人在刚接触linux系统时会踩雷的点,一定要注意。
有些同学在使用COS7时候报错:service: command not found…,就是这个原因
2.1 查看默认防火墙状态
#查看默认防火墙状态(关闭后显示not running,开启后显示running)
firewall-cmd --state
或
systemctl status firewalld.service
2.2 停止firewall
临时关闭防火墙 systemctl stop firewalld.service
永久禁止firewall开机自动启动 systemctl disable firewalld.service
2.3 启动firewall
启动防火墙服务 systemctl start firewalld.service
开机时自动启用防火墙服务:systemctl enable firewalld.service
2.4 查看firewall服务是否开机启动
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
2.5 端口命令
查看已经开放的端口:firewall-cmd --list-ports
开启端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
2.6 重启防火墙
systemctl restart firewalld.service
或者:
firewall-cmd --reload
三、CentOS7版本下使用iptables防火墙
那如果我硬是要在COS7下用iptables防火墙呢?
也是可以的。
在使用iptables必须重新设置一下:
3.1 从yum源中下载iptables-services
yum install iptables-services
3.2 关闭firewall防火墙
#停止firewall
systemctl stop firewalld.service
#禁止firewall开机启动
systemctl disable firewalld.service
3.3 设置 iptables service
- systemctl start iptables.service #打开防火墙
- systemctl restart iptables.service #重启防火墙
- systemctl stop iptables.service #临时关闭防火墙
- systemctl disable iptables.service #永久关闭防火墙
- systemctl enable iptables.service #开机自启动防火墙
简而言之二句话:
1、下载iptables;
2、防火墙名由firewalld -> iptables,语法规则不变。
是不是特别简单易学?
四、卸载最后
以上所有操作,请在root权限下运行!
以上所有操作,请在root权限下运行!
以上所有操作,请在root权限下运行!
希望能帮助到您,避免走我的踩的坑。如确实有帮助到您,点个赞吧。