(linux笔记)开放防火墙端口

关闭防火墙

  1. CentOS 7、RedHat 7 之前的 Linux 发行版防火墙开启和关闭( iptables ):

    • 即时生效,重启失效
    #开启
    service iptables start
    #关闭
    service iptables stop
    
    • 重启生效
    chkconfig iptables on
    #关闭
    chkconfig iptables off
    
  2. CentOS 7、RedHat 7 之后的 Linux 发行版防火墙开启和关闭( firewall )

    systemctl stop firewalld.service
    

开放端口

  1. CentOS 7、RedHat 7 之前的 Linux 发行版开放端口:

     #查看开放的端口列表
     firewall-cmd --list-ports 
    
     #命令方式开放5212端口命令
     #开启5212端口接收数据
     /sbin/iptables -I INPUT -p tcp --dport 5212 -j ACCEPT
    
    
     #开启5212端口发送数据
     /sbin/iptables -I OUTPUT -p tcp --dport 5212 -j ACCEPT
    
     #保存配置
     /etc/rc.d/init.d/iptables save
    
     #重启防火墙服务
     /etc/rc.d/init.d/iptables restart
    
     #查看是否开启成功
     /etc/init.d/iptables status
    
    
    
  2. CentOS 7、RedHat 7 之后的 Linux 发行版开放端口:

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

使用iptables

  1. iptables 的安装
  • 系统默认使用 firewall 作为防火墙,把他停止和屏蔽,并且装一个iptable【CentOs 7 】

    systemctl stop firewalld
    systemctl mask firewalld
    
    yum install -y iptables
    yum install iptables-services
    
  • 升级iptables

    yum update iptables
    yum update iptables-services
    
  • 解除禁止 iptables 服务

    systemctl enable iptables
    
  • 开启服务

    systemctl start iptables.service
    
  • 配置 /etc/sysconfig 下的 iptables 文件[vim /etc/sysconfig/iptables]

    -A INPUT -p tcp -m state --state NEW -m tcp --dport 3000 -j ACCEPT
    
  • 重启防火墙使配置生效

    systemctl restart iptables.service
    
    
  • 设置防火墙为开机启动

    systemctl enable iptabls.service
    
  • 检查是否安装了 iptables

    service iptables status
    
  • 其它命令

    systemctl start iptables //开启/暂停服务
    systemctl stop iptables //
    
    systemctl disable iptables //禁止/解除禁止 iptables 服务
    systemctl enable iptables

转:https://www.cnblogs.com/jjxhp/p/11111898.html

参考:https://www.cnblogs.com/marso/archive/2018/01/06/8214927.html

原文地址:https://www.cnblogs.com/worldbugMsg/p/13265676.html