关闭某端口的防火墙

1、 永久关闭防火墙,重启后生效

开启:chkconfig iptables on

关闭:chkconfig iptables off

 

2、当时生效,重启后失效

开启:service iptables start

失效:service iptables stop

状态:service iptables status

 

3、查看iptables文件

vim /etc/sysconfig/iptables

-----------------------------------------------------------------------------------------------------

CentOS 7 使用firewalld代替了原来的iptables,使用方法如下:

>>>关闭防火墙

systemctl stop firewalld.service            #停止firewall
systemctl disable firewalld.service        #禁止firewall开机启动

>>>开启端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

命令含义

--zone #作用域
--add-port=80/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效

>>>重启防火墙

firewall-cmd --reload

其他常用命令:

 firewall-cmd --state                          ##查看防火墙状态,是否是running
firewall-cmd --reload                          ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones                      ##列出支持的zone
firewall-cmd --get-services                    ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --query-service ftp              ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp                ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent    ##永久开放ftp服务
firewall-cmd --remove-service=ftp --permanent  ##永久移除ftp服务
firewall-cmd --add-port=80/tcp --permanent    ##永久添加80端口
iptables -L -n                                ##查看规则,这个命令是和iptables的相同的
man firewall-cmd                              ##查看帮助

更多命令,使用 firewall-cmd --help 查看帮助文件

>>> CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下

1、直接关闭防火墙

systemctl stop firewalld.service          #停止firewall
systemctl disable firewalld.service    #禁止firewall开机启动

2、设置 iptables service

yum -y install iptables-services
如果要修改防火墙配置,如增加防火墙端口3306
vi /etc/sysconfig/iptables
增加规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

保存退出后
systemctl restart iptables.service #重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动

最后重启系统使设置生效即可。

 

二、只开放某个端口

linux系统(centos、redhat等)的防火墙是默认只对外开放了22端口。

1、手动修改配置文件

修改Linux系统防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,如果要开放哪个端口,在里面添加一条

 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT 就可以了,其中 8080 是要开放的端口号,然后重新启动linux的防火墙服务,

 /etc/init.d/iptables restart。或service iptables restart

配置:view plain c

 
  1. # Firewall configuration written by system-config-firewall  
  2. # Manual customization of this file is not recommended.  
  3. *filter  
  4. :INPUT ACCEPT [0:0]  
  5. :FORWARD ACCEPT [0:0]  
  6. :OUTPUT ACCEPT [0:0]  
  7. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
  8. -A INPUT -p icmp -j ACCEPT  
  9. -A INPUT -i lo -j ACCEPT  
  10. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  
  11. -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT  
  12. -A INPUT -j REJECT --reject-with icmp-host-prohibited  
  13. -A FORWARD -j REJECT --reject-with icmp-host-prohibited  
  14. COMMIT  
 
查看开放的端口
[plain] view plain copy
 
  1. /sbin/iptables -L -n  

2、命令行方式

在命令行添加外网端口访问 linux开启允许外网访问的端口 LINUX开启允许对外访问的网络端口 LINUX通过下面的命令可以开启允许对外访问的网络端口:

 /sbin/iptables -I INPUT -p tcp --dport 8000 -j ACCEPT 

#开启8000端口 /etc/rc.d/init.d/iptables save 

#保存配置 /etc/rc.d/init.d/iptables restart

#重启服务 查看端口是否已经开放 /etc/init.d/iptables status

posted @ 2019-08-27 10:57  归零19  阅读(2449)  评论(0编辑  收藏  举报