欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Linux 基本防火墙设置和开放端口命令

最编程 2024-08-11 14:50:49
...
【直播预告】上云 vs 下云:降本增笑?割韭菜?

关闭防火墙

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

    systemctl stop firewalld.service
    

开放端口

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

     #命令方式开放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 永久生效,没有此参数重启后失效
    

使用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
    

推荐阅读