Linux系统如何关闭防火墙
在linux系统上打开和关闭防火墙与centos7和更早版本略有不同。
另一方面,centos7以前的版本可以通过iptables相关命令实现防火墙的打开/关闭
1 .首先,在打开的终端上可以使用iptables --help查看帮助使用命令;
2 .检查防火墙状态: service iptables status (可以使用此命令检查防火墙是打开还是关闭);
3 .服务启用常接参数:
(1)服务启用停止关闭防火墙;
)2)服务启用启动打开防火墙;
)3)重新加载服务可移植加载防火墙;
(4)重新启动服务可移植重启防火墙;
4 .永久关闭/打开防火墙
chkconfig iptables off防火墙永久关闭;
永久打开chkconfig iptables on防火墙;
5 .通过5.linux图形界面打开和关闭防火墙
在终端上键入setup,返回图形界面并进行相应的选择。 关闭防火墙:选择工具——防火墙配置——运行工具——安全级别——禁用——确定;
6 .启用和禁用防火墙端口
)使用上述命令打开防火墙后,必须修改/etc/sysconfig/iptables文件,并添加两个打开80、22端口的命令
- arh-firewall-1-input-mstate-- statenew-mtcp-ptcp-- dport 80-j accept
- arh-firewall-1-input-mstate-- statenew-mtcp-ptcp-- dport 22-j accept
)2) centos linux防火墙可以按上一章5所述的方法关闭,也可以通过命令关闭。
# sbin/iptables-l输入- ptcp-dport 80-j accept
# sbin/iptables-l输入- ptcp-dport 22-j accept
#/etc/init.d/iptables stop
)3) UBuntu要关闭防火墙,只需键入以下命令
iptables-a输入- I! PPP0 -j ACCEPT
二.对于centos7系统
在centos7中,使用防火墙命令打开和关闭防火墙。
1 .系统命令
(1) systemctlstatusfirewalld.service查看防火墙状态;
)2) systemctlstartfirewalld.service启动防火墙;
)3)关闭系统停止防火墙d.service防火墙;
(4)重新启动systemctlrestartfirewalld.service防火墙;
)5)打开systemctlenablefirewalld.service启动防火墙;
(6)打开systemctldisablefirewalld.service以禁用防火墙;
)7)检查systemctlis-enabledfirewalld.service防火墙是否已通电;
2 .防火墙区域管理
firewall引入了zone的概念。 firewall可以将不同的网络连接分为不同的信任级别。 为了便于理解,在这里附上水平的说明。
阻止(块区域)拒绝所有外部连接,返回icmp-host-prohibited,允许内部连接;
隔离区域(dmz )允许有限的接入连接;
放弃区(DOP )在没有任何响应的情况下放弃所有进入的数据包。
外部区域(external )只接受指定的连接,通常用于路由转发;
家庭区域(home )允许受信任计算机受到限制的访问连接
内部区域信任网络上的其他计算机,不会损坏计算机。 只能选择接受传入的网络连接;
公共区域(public )不信任网络上的任何计算机,只是选择接受传入的网络连接
信任区域信任所有连接;
工作区(work )允许受信任计算机限制连接。
显示区域管理命令示例:
)1)查看防火墙- cmd--列表-所有区域详细列表;
)2)防火墙- cmd-- get-zones显示支持的区域列表;
)3)显示防火墙- cmd----获取-默认区域默认区域列表;
(4)firewall-cmd --set-default-zone=home设置默认的区域;
(5)firewall-cmd --zone=home --list-all查看home区域l;
(6)firewall-cmd --get-active-zone查看活动的区域;
(7)firewall-cmd --permanent-new-zone=myself创建自己的区域;
(8)firewall-cmd --permanent-delete-zone=myself删除区域;
3.Firewall服务端口管理
在下面提到的命令示例中,带"--permanent"永久生效的策略记录(除了查看)执行后,必须执行"--reload"参数后才能立即生效,否则需要重启后再生效。不带"--permanent"的命令立即生效,但是reload或者restart后失效。
(1)firewall-cmd --state查看防火墙状态;
(2)firewall-cmd --get services查看已被firewall提供的一些常用服务;
(3)firewall-cmd --zone=public --permanent --list-services查看某域的服务(注:加了--permanent表示永久服务,不加显示所有服务,包含临时服务;不加--zone表示默认区域);
(4)firewall-cmd --zone=public--permanent --add-service=http添加某域的服务;
(5)firewall-cmd --zone=public--permanent --remove-service=http移除某域的服务(注:加了--permanent表示永久服务,不加显示所有服务,包含临时服务;不加--zone表示默认区域);
(6)firewall-cmd --zone=home--permanent --add-por=5000/tcp添加端口;
(7)firewall-cmd --zone=home--permanent --remove-por=5000/tcp删除端口;
(8)firewall-cmd --zone=home--permanent --add-por=5000-5005/tcp添加多端口(注:加了--permanent表示永久服务,不加显示所有服务,包含临时服务;不加--zone表示默认区域;端口后面的tcp和udp表示协议类型);
(9)firewall-cmd --zone=public --permanent --list-ports查看端口情况(注:加了--permanent表示永久服务,不加显示所有服务,包含临时服务;不加--zone表示默认区域);
(10)firewall-cmd --zone=public --query-service=ssh查询服务是否被允许(不加--zone表示默认区域);
(11)firewall-cmd --permanent --zone=public --add-forward-port=80:proto=tcp:toport=8080将80端口的流量转发至8080端口;
(12)firewall-cmd --permanent --zone=public --add-forward-port=80:proto=tcp:toaddr=192.168.1.1将80端口的流量转发至192.168.1.1;
(13)firewall-cmd --permanent --zone=public --add-forward-port=80:proto=tcp:toaddr=192.168.1.1:toport=8080将80端口的流量转发至192.168.1.1的端口8080端口(注:不加ip地址,默认转发到本地的端口;不加--zone表示默认区域;不加--permanent表示临时增加,reload或则restart后失效)。
4.手动编写服务
可以将某个程序需要的端口写在一个自己编写的服务里,然后直接添加服务就可以。
/usr/lib/firewalld/services创建自己的服务,格式可以拷贝/etc/firewalld/services任意一个服务。
(1)查找服务:
firewalld-cmd --get-srvice |grep myself
(2)重新加载配置:
firewall-cmd --reload
---本文部分内容来自星之宇的博客,在此致谢