CentOS7用firewall命令“替代”了iptables。在这里我们需要区分“iptables服务”和“iptables命令”。虽然firewalld是替代提供的防火墙管理iptables服务,但是它仍然使用iptables对内核命令动态通信包过滤。所以它只是iptables服务代替,而不是iptables命令。
systemctl stop firewalld
systemctl disable firewalld
yum install iptables-services
touch /etc/sysconfig/iptables
systemctl start iptables
systemctl enable iptables
touch /etc/sysconfig/ip6tables
systemctl start ip6tables
systemctl enable ip6table
同上
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
1、关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
2、安装iptables防火墙
yum install iptables-services #安装
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
安装firewalld和firewall-cmd请参考结尾link
检查防火墙状态
firewall-cmd --stat
临时开放ftp服务
firewall-cmd --add-service=ftp
永久开放ftp服务
firewall-cmd --add-service=ftp --permanent
关闭ftp服务
firewall-cmd --remove-service=ftp --permanent
配置防火墙在public区域永久开放http服务
firewall-cmd --permanent --zone=public --add-service=http
加入指定开放端口
firewall-cmd --add-port=1324/tcp
为了让之前的设定生效当然要重启服务咯
systemctl restart firewalld
或者使用下面的命令免去重启服务(防火墙策略配置后重新载入)
firewall-cmd --complete-reload
firewall-cmd --reload (这两句功能相同)
检查ftp服务的21端口是否开放
iptables -L -n | grep 21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21 ctstate NEW
查询ftp服务启用状态
firewall-cmd --query-service ftp
查看当前规则
firewall-cmd --list-all
仅允许部分IP访问本机服务配置
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" \
source address="192.168.0.4/24" service name="http" accept"
仅允许部分IP访问本机端口配置
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" \
source address="192.168.0.4/24" \
port protocol="tcp" port="8080" accept"
公共配置文件位置:/etc/firewalld/zones/public.xml
CentOS6关闭防火墙
service iptables status //查看防火墙状态
service iptables stop //关闭防火墙
chkconfig iptables –list //查看防火墙开机启动状态
chkconfig iptables off //关闭防火墙开机启动
一.Linux下开启/关闭防火墙命令
1) 永久性生效,重启后不会复原
开启: chkconfig iptables on
关闭: chkconfig iptables off
2) 即时生效,重启后复原
开启: service iptables start
关闭: service iptables stop
需要说明的是对于Linux下的其它服务都可以用以上命令执行开启和关闭操作。
在当开启了防火墙时,做如下设置,开启相关端口,
修改/etc/sysconfig/iptables
文件,添加以下内容:
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
二.UBuntu关闭防火墙
iptables -A INPUT -i ! PPP0 -j ACCEPT
三.CentOS Linux 防火墙配置及关闭
执行”setup”命令启动文字模式配置实用程序,在”选择一种工具”中选择”防火墙配置”,然后选择”运行工具”按钮,出现防火墙配置界面,将”安全级别”设为”禁用”,然后选择”确定”即可.
或者用命令:
/sbin/iptables -I INPUT -p tcp –dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp –dport 22 -j ACCEPT
/etc/rc.d/init.d/iptables save
这样重启计算机后,防火墙默认已经开放了80和22端口
这里应该也可以不重启计算机:
/etc/init.d/iptables restart
验证防火墙是否关闭
chkconfig --list | grep iptables