1、CentOS7
CentOS7中防火墙为firewall。
1.1 firewall配置
系统配置目录
/usr/lib/firewalld/services
目录中存放定义好的网络服务和端口参数,系统参数,不能修改。
用户配置目录
/etc/firewalld/
自定义添加端口
用户可以通过修改配置文件的方式添加端口,也可以通过命令方式添加端口,注意,修改的内容会在/etc/firewalld/ 目录下的配置文件中体现。
命令方式添加端口
firwall-cmd --permanent --add-port=9999/tcp
参数介绍:
firwall-cmd
:是Linux提供的操作firewall的一个工具--permanent
:表示设置为持久--add-port
:标识添加的端口
修改配置文件的方式添加端口/协议
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas.</description>
<rule family="ipv4">
<source address="122.10.70.234"/>
<port protocol="udp" port="514"/>
<accept/>
</rule>
<rule family="ipv4">
<source address="123.60.255.14"/>
<port protocol="tcp" port="10050-10051"/>
<accept/>
</rule>
<rule family="ipv4">
<source address="192.249.87.114"/> //放通指定ip,指定端口、协议
<port protocol="tcp" port="80"/>
<accept/>
</rule>
<rule family="ipv4"> //放通任意ip访问服务器的9527端口
<port protocol="tcp" port="9527"/>
<accept/>
</rule>
</zone>
上述的一个配置文件可以很好的看出:
1、添加需要的规则,开放通源ip为122.10.70.234,端口514,协议tcp;
2、开放通源ip为123.60.255.14,端口10050-10051,协议tcp;/3、开放通源ip为任意,端口9527,协议tcp;
1.2 firewall常用命令
重启、关闭、开启firewalld.service
服务
service firewalld restart #重启防火墙
service firewalld start #开启防火墙
service firewalld stop #关闭防火墙
或
systemctl restart iptables.service #重启防火墙
systemctl start firewalld.service #开启防火墙
systemctl stop firewalld.service #关闭防火墙
systemctl enable firewalld.service #开机启动防火墙
systemctl disable firewalld.service #禁止防火墙开机启动
查看命令
firewall-cmd --list-ports #查看打开的端口
systemctl status firewall #查看防火墙服务状态
firewall-cmd --state #查看防火墙的状态
firewall-cmd --list-all #查看防火墙规则
2、CentOS6
CentOS6中防火墙为iptable。
1.1 iptable配置
用户配置目录
/etc/sysconfig/iptables
自定义添加端口
命令方式添加端口
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save
参数介绍:
-A
:指定链名-p
:指定协议类型-d
:指定目标地址--dport
:指定目标端口(destination port 目的端口)--sport
:指定源端口(source port 源端口)-j
:指定动作类型
修改配置文件的方式添加端口/协议
vim /etc/sysconfig/iptables
修改后,重启防火墙。
service iptables restart
1.2 iptables常用命令
重启、关闭、开启iptables
服务
chkconfig iptables on #开启防火墙,重启后不会复原
chkconfig iptables off #关闭防火墙,重启后不会复原
service iptables restart #重启防火墙
service iptables start #开启防火墙,重启后复原
service iptables stop #关闭防火墙,重启后复原
查看命令
/etc/init.d/iptables status #查看打开的端口
service iptable status #查看防火墙状态
参考链接:
http://blog.csdn.net/xlgen157387/article/details/52672988
http://www.jb51.net/article/101576.htm
http://www.cnblogs.com/baby123/p/5799052.html