JAVA开发运维(linux环境防火墙与端口开启使用总结记录)

一、问题背景:

       将web项目开发完成,需要上到生产环境。那么我们应用调用的一些ip,端口都是要设置的,比如说应用提供给谁访问,那些人不能访问,应用的端口是多少,也是需要开启才能访问的。在实际研发过程中,这一部分工作一般由运维工程师或者网络工程师管理。但是作为开发人员,也需要了解其中的原理,甚至需要熟练使用。要不然与网络工程师的沟通交流也是相当困难。

二、防火墙介绍:

  1. 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
  2. 如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。
  3. 如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右移动,经过FORWARD链,然后到达POSTROUTING链输出。

可以看出,刚从网络接口进入的数据包尚未进行路由决策,还不知道数据要走向哪里,所以进出口处没办法实现数据过滤,需要在内核空间设置转发关卡、进入用户空间关卡和离开用户空间关卡。

防火墙:

在RHEL 7系统中,firewalld防火墙取代了iptables防火墙。对于接触Linux系统比较早或学习过RHEL 6系统的读者来说,当他们发现曾经掌握的知识在RHEL 7中不再适用,需要全新学习firewalld时,难免会有抵触心理。其实,iptables与firewalld都不是真正的防火墙,它们都只是用来定义防火墙策略的防火墙管理工具而已,或者说,它们只是一种服务。iptables服务会把配置好的防火墙策略交由内核层面的netfilter网络过滤器来处理,而firewalld服务则是把配置好的防火墙策略交由内核层面的nftables包过滤框架来处理。换句话说,当前在Linux系统中其实存在多个防火墙管理工具,旨在方便运维人员管理Linux系统中的防火墙策略,我们只需要配置妥当其中的一个就足够了。虽然这些工具各有优劣,但它们在防火墙策略的配置思路上是保持一致的。大家甚至可以不用完全掌握本章介绍的内容,只要在这多个防火墙管理工具中任选一款并将其学透,就足以满足日常的工作需求了。

Iptables:

iptables是用来管理防火墙的的工具,属于静态防火墙,我们通过 iptables 将过滤规则写入内核,然后 Netfilter 再根据规则进行过滤数据包。所以实际上iptables是通过调用 Netfilter 来进行防火墙管理的,它本身不具备过滤数据包的功能。iptables程序位于 /sbin/iptables ,配置文件位于 /etc/sysconfig/iptables 。在Rhel7之前,防火墙是用 iptables

iptables中也有和netfilter中一模一样的5种规则链,还多了4个规则表。规则表的作用是容纳各种规则链。规则表的划分依据是防火墙规则的作用。四个表中常用的是 filter 表。最常用的链是 INPUT 和 OUTPUT 链。

 

三、防火墙与端口使用:

查看防火墙状态

firewall-cmd --state

 开启防火墙:

systemctl start firewalld.service

开启制定端口:

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

显示 success 表示成功
–zone=public 表示作用域为公共的
–add-port=8080/tcp 添加 tcp 协议的端口端口号为 8080
–permanent 永久生效,如果没有此参数,则只能维持当前 服 务生命周期内,重新启动后失效;

重启防火墙

systemctl restart firewalld.service

重新加载防火墙

firewall-cmd --reload

查看已开启的端口

firewall-cmd --list-ports

关闭指定端口

#关闭指定端口
firewall-cmd --zone=public --remove-port=8080/tcp --permanent
systemctl restart firewalld.service
firewall-cmd --reload

查看端口被占用情况

#查看端口被哪一个进程占用
netstat -lnpt |grep 5672
# centos7默认没有 netstat 命令,需要安装 net-tools 工具:
# 安装 net-tools
yum install -y net-tools

临时关闭防火墙

# 临时关闭防火墙
systemctl stop firewalld.service
# 或者
systemctl stop firewalld

永久关闭防火墙

# 永久关闭防火墙(必须先临时关闭防火墙,再执行该命令,进行永久关闭)
systemctl disable firewalld.service
# 或者
systemctl disable firewalld

测试端口是否能连接

telnet  ip  port

四、iptables的一些使用:

CentOS6

1、查看防火墙状态:service iptables status/etc/init.d/iptables status

2、启/停/重启防火墙:service iptables start/stop/restart

3、查看防火墙是否开机启动:chkconfig iptables --list

4、设置防火墙开机自启/不自启:chkconfig iptables on/off

CentOS7

1、查看防火墙状态:systemctl status firewalld

2、启/停/重启防火墙:systemctl start/stop/restart firewalld.service

3、设置防火墙开机自启/不自启:systemctl enable/disable firewalld.service

4、开启端口:firewall-cmd --zone=public --add-port=80/tcp -permanent

1、基本操作

# 查看防火墙状态

service iptables status  

# 停止防火墙

service iptables stop  

# 启动防火墙

service iptables start  

# 重启防火墙

service iptables restart  

# 永久关闭防火墙

chkconfig iptables off  

# 永久关闭后重启

chkconfig iptables on  

2、开启80端口

vim /etc/sysconfig/iptables
# 加入如下代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
保存退出后重启防火墙

service iptables restart
二、firewall防火墙
1、查看firewall服务状态

systemctl status firewalld

出现Active: active (running)切高亮显示则表示是启动状态。

出现 Active: inactive (dead)灰色表示停止,看单词也行。
2、查看firewall的状态

firewall-cmd --state
3、开启、重启、关闭、firewalld.service服务

# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
4、查看防火墙规则

firewall-cmd --list-all
5、查询、开放、关闭端口

# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口

猜你喜欢

转载自blog.csdn.net/dongjing991/article/details/131470732
今日推荐