在使用centos7安装完mysql、tomcat、nginx后,都需要配置防火墙才能正常访问。

下面系统的学习一下防火墙的配置。

centos7默认使用firewall,需要关闭,然后使用iptable

一、关闭firewall

  systemctl stop firewalld.service #停止firewall
  systemctl disable firewalld.service #禁止firewall开机启动
  firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

二、安装iptable iptable-service

  #先检查是否安装了iptables
  service iptables status
  #安装iptables
  yum install -y iptables
  #升级iptables(安装的最新版本则不需要)
  yum update iptables 
  #安装iptables-services
  yum install iptables-services

三、设置现有规则

  #查看iptables现有规则

  iptables -L -n

  #先允许所有,不然有可能会杯具

  iptables -P INPUT ACCEPT

  #清空所有默认规则

  iptables -F

  #清空所有自定义规则

  iptables -X

  #所有计数器归0

  iptables -Z

  #允许来自于lo接口的数据包(本地访问)

  iptables -A INPUT -i lo -j ACCEPT

  #开放22端口

  iptables -A INPUT -p tcp --dport 22 -j ACCEPT

  #开放21端口(FTP)

  iptables -A INPUT -p tcp --dport 21 -j ACCEPT

  #开放80端口(HTTP)

  iptables -A INPUT -p tcp --dport 80 -j ACCEPT

  #开放443端口(HTTPS)

  iptables -A INPUT -p tcp --dport 443 -j ACCEPT

  #允许ping

  iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

  #允许接受本机请求之后的返回数据 RELATED,是为FTP设置的

  iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

  #其他入站一律丢弃

  iptables -P INPUT DROP

  #所有出站一律绿灯

  iptables -P OUTPUT ACCEPT

  #所有转发一律丢弃

  iptables -P FORWARD DROP

四、其他规则

  #如果要添加内网ip信任(接受其所有TCP请求)

  iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT

  #过滤所有非以上规则的请求

  iptables -P INPUT DROP

  #要封停一个IP,使用下面这条命令:

  iptables -I INPUT -s ***.***.***.*** -j DROP

  #要解封一个IP,使用下面这条命令:

  iptables -D INPUT -s ***.***.***.*** -j DROP

五、保存规则

  #保存上述规则

  service iptables save

六、开启iptables服务

  #注册iptables服务

  #相当于以前的chkconfig iptables on

  systemctl enable iptables.service

  #开启服务

  systemctl start iptables.service

  #查看状态

  systemctl status iptables.service

七、映射端口(如将mysql默认的3306端口映射成1306对外提供服务)

  iptables -t mangle -I PREROUTING -p tcp --dport 1306 -j MARK --set-mark 883306 
  iptables -t nat -I PREROUTING -p tcp --dport 1306 -j REDIRECT --to-ports 3306 
i  ptables -I INPUT -p tcp --dport 3306 -m mark --mark 883306 -j ACCEPT

Linux Centos7配置防火墙开启端口的更多相关文章

  1. centos开启nginx服务成功,却无法访问。没有开启80端口。centos配置防火墙 开启80端口

    Linux配置防火墙 开启80端口 编辑配置文件/etc/sysconfig/iptables [root@weixinht ~]# vim /etc/sysconfig/iptables 1 # F ...

  2. Linux网络——配置防火墙的相关命令

    Linux网络——配置防火墙的相关命令 摘要:本文主要学习了如何在Linux系统中配置防火墙. iptables命令 iptables准确来讲并不是防火墙,真正的防火墙是运行于系统内核中的netfil ...

  3. centos7 防火墙 开启端口 并测试

    1.防火墙 CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,google之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如何使用fir ...

  4. Centos7(Firewall)防火墙开启常见端口命令

    使用云服务器的,一定要注意开启安全组配置的响应端口 Centos7默认安装了firewalld,如果没有安装的话,则需要YUM命令安装:firewalld真的用不习惯,与之前的iptable防火墙区别 ...

  5. Centos7命令行安装Tomcat以及配置防火墙开放端口

    [转载]Centos 7 yum安装tomcat 命令: 系统环境CentOS Linux release 7.2.1511 (Core) 一.搭建准备:1.先到tomcat官网https://tom ...

  6. Linux配置防火墙8080端口

    1.查看防火墙状态,哪些端口开放了 /etc/init.d/iptables status 2.配置防火墙 vi /etc/sysconfig/iptables ################### ...

  7. Centos7 配置防火墙 firewall

    一.firewall 1.从CentOS7开始,默认使用firewall来配置防火墙,没有安装iptables(旧版默认安装). 2.firewall的配置文件是以xml的格式,存储在 /usr/li ...

  8. Linux centos7. 配置安装Oracle

    oralcle 11g r2 配置一下前期的网络环境 一 修改linux核心配置 1.修改用户的SHELL限制vi /etc/security/limits.conf oracle soft npro ...

  9. Linux Centos7配置mysql8.0数据库

    本文转至:672530440 在此感谢博主,撒花!!! 本文主要从以下几个方面对自己在centos7 下安装mysql8过程做如下总结: CentOS7 安装mysql8 步骤: window下的Na ...

随机推荐

  1. [Machine Learning]学习笔记-Logistic Regression

    [Machine Learning]学习笔记-Logistic Regression 模型-二分类任务 Logistic regression,亦称logtic regression,翻译为" ...

  2. django事务处理

    #导包 from django.db import transaction try: #django默认是自动提交到数据库,此处设置不让其自动提交 transaction.set_autocommit ...

  3. java版Web Socket,实现消息推送

    # web socket是什么? WebSocket协议是基于TCP的一种新的网络协议. 它实现了浏览器与服务器全双工(full-duplex)通信,允许服务器主动发送信息给客户端. ## 用途 实时 ...

  4. DIV居中的经典方法

    1. 实现DIV水平居中 设置DIV的宽高,使用margin设置边距0 auto,CSS自动算出左右边距,使得DIV居中. 1 div{ 2 width: 100px; 3 height: 100px ...

  5. epoll 惊群处理

    #include <sys/types.h> #include <sys/socket.h> #include <sys/epoll.h> #include < ...

  6. POJ2251-Dungeon Master

    题意:给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是连 ...

  7. PHP+Mysql基于事务处理实现转账功能的方法

    <?php header("Content-Type:text/html;charset=utf-8"); $mysqli=new mysqli("localhos ...

  8. 天天乐宝APP开发

    "互联网+"时代是一个"信息过剩"的时代,也是一个"注意力稀缺"的时代,怎样在"无限的信息中"获取"有限的注意 ...

  9. poj 3168 Barn Expansion 几何yy

    题链:http://poj.org/problem? id=3168 Barn Expansion Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  10. Android系统匿名共享内存(Anonymous Shared Memory)Java调用接口分析

    一.Ashmem驱动程序 ~/Android/kernel/goldfish ----include ----linux ----ashmem.h ----mm ----ashmem.c 驱动程序具体 ...