centos iptables 53(centos7)

本篇文章给大家谈谈centos iptables 53,以及centos7对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。

如何开放CentOS的端口

Centos升级到7之后,内置的防火墙已经从iptables变成了firewalld。所以,端口的开启还是要从两种情况来说明的,即iptables和firewalld。更多关于CentOs防火墙的最新内容,请参考Redhat官网。

一、iptables

1.打开/关闭/重启防火墙

开启防火墙(重启后永久生效):chkconfig iptables on

关闭防火墙(重启后永久生效):chkconfig iptables off

开启防火墙(即时生效,重启后失效):service iptables start

关闭防火墙(即时生效,重启后失效):service iptables stop

重启防火墙:service iptables restartd

2.查看打开的端口

/etc/init.d/iptables status

3.打开某个端口(以8080为例)

(1)开启端口

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

(2)保存并重启防火墙

/etc/rc.d/init.d/iptables save

/etc/init.d/iptables restart

4.打开49152~65534之间的端口

iptables-A INPUT-p tcp--dport 49152:65534-j ACCEPT

同样,这里需要对设置进行保存,并重启防火墙。

5.其他打开方式

我们还可以通过修改/etc/sysconfig/iptables文件的方式开启端口,如下

vi/etc/sysconfig/iptables

然后在文件中增加一行

-A RH-Firewall-1-INPUT-m state–state NEW-m tcp-p tcp–dport 8080-j ACCEPT

参数说明:

–A参数就看成是添加一条规则

–p指定是什么协议,我们常用的tcp协议,当然也有udp,例如53端口的DNS

–dport就是目标端口,当数据从外部进入服务器为目标端口

–sport数据从服务器出去,则为数据源端口使用

–j就是指定是 ACCEPT-接收或者 DROP不接收

二、firewalld

Centos7默认安装了firewalld,如果没有安装的话,可以使用 yum install firewalld firewalld-config进行安装。

1.启动防火墙

systemctl start firewalld

2.禁用防火墙

systemctl stop firewalld

3.设置开机启动

systemctl enable firewalld

4.停止并禁用开机启动

sytemctl disable firewalld

5.重启防火墙

firewall-cmd--reload

6.查看状态

systemctl status firewalld或者 firewall-cmd--state

7.查看版本

firewall-cmd--version

8.查看帮助

firewall-cmd--help

9.查看区域信息

firewall-cmd--get-active-zones

10.查看指定接口所属区域信息

firewall-cmd--get-zone-of-interface=eth0

11.拒绝所有包

firewall-cmd--panic-on

12.取消拒绝状态

firewall-cmd--panic-off

13.查看是否拒绝

firewall-cmd--query-panic

14.将接口添加到区域(默认接口都在public)

firewall-cmd--zone=public--add-interface=eth0(永久生效再加上--permanent然后reload防火墙)

15.设置默认接口区域

firewall-cmd--set-default-zone=public(立即生效,无需重启)

16.更新防火墙规则

firewall-cmd--reload或firewall-cmd--complete-reload(两者的区别就是第一个无需断开连接,就是firewalld特性之一动态

添加规则,第二个需要断开连接,类似重启服务)

17.查看指定区域所有打开的端口

firewall-cmd--zone=public--list-ports

18.在指定区域打开端口(记得重启防火墙)

firewall-cmd--zone=public--add-port=80/tcp(永久生效再加上--permanent)

说明:

–zone作用域

–add-port=8080/tcp添加端口,格式为:端口/通讯协议

–permanent#永久生效,没有此参数重启后失效

centos7 iptables

在 CentOS7系统中,firewalld和 iptables是两种常见的网络防火墙解决方案。虽然两者都提供了类似的功能,但在使用场景和配置方式上存在显著差异。

iptables默认规则相对开放,而 firewalld默认规则相对封闭。因此,推荐使用 iptables进行更细致的网络控制。

iptables的规则在/etc/sysconfig/iptables中存储,而 firewalld的配置则储存在/usr/lib/firewalld/和/etc/firewalld/中的 XML文件中。iptables在更改规则时,会清除所有旧规则并读取新规则,而 firewalld则不会创建新规则,仅运行不同规则,使其在运行时改变设置而不丢失当前配置。

在使用 firewalld时,可以通过以下命令查看和管理防火墙规则:

bash

firewall-cmd--list-all

firewall-cmd--list-services

firewall-cmd--list-port

firewall-cmd--reload

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

firewall-cmd--add-service=http--permanent

firewall-cmd--remove-port=8080/tcp--permanent

firewall-cmd--remove-service=http--permanent

firewall-cmd--add-forward-port=port=80:proto=tcp:toport=8080--permanent

firewall-cmd--zone=public--add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.217.128--permanent

为了使用 iptables,需先关闭 firewalld并安装 iptables:

bash

systemctl stop firewalld.service

systemctl disable firewalld.service

yum-y install iptables-services

systemctl restart iptables.service

在 iptables中配置规则的常用命令有:

bash

iptables-L-n

iptables-F

iptables-X

可以使用以下方式在 iptables中修改防火墙规则:

修改/etc/sysconfig/iptables文件(重启 iptables使更改生效)。

直接使用命令(需要使用 `save`命令使配置生效)。

例如,直接添加一条开放 8080端口的规则:

bash

iptables-I INPUT-p tcp-m state--state NEW-m tcp--dport 8080-j ACCEPT

service iptables save

遇到的问题是,使用 `iptables-A`添加规则时,虽然规则被写入配置文件,但无法访问。后来发现使用 `iptables-I`添加配置可以解决问题。原因是 `-A`添加在规则列表末尾,而 `-I`添加在第一条,因此优先级不同。

在 iptables中,可以使用以下规则进行更细粒度的控制:

同时开放多个端口:`iptables-I INPUT-p tcp-m multiport--dport 22,80-j ACCEPT`。

开放连续端口范围:`iptables-I INPUT-p tcp--dport 5000:6000-j ACCEPT`。

允许特定网段访问:`iptables-I INPUT-p all-s 0.0.0.0/0-j ACCEPT`。

允许特定 IP的特定端口访问:`iptables-I INPUT-p tcp-s 0.0.0.0-dport 8080-j ACCEPT`。

禁止特定主机访问:`iptables-I INPUT-p tcp-s 0.0.0.0-j DROP`。

移除规则:`iptables-D INPUT 7`。

在 iptables配置中,放行使用 `ACCEPT`,禁止使用 `DROP`。

Centos怎么让iptables允许PPTP服务

iptables是什么?有什么用

iptables是一个IP信息包过滤系统,主要是用来设置、维护和检查linux内核的IP包过滤规则的。如果要使用PPTPD服务的话,就必须要用到TCP协议1723端口和47协议。

使用的代码很简单,刚开始摸索阶段走了很多弯路,以为很复杂,其实跟tcp协议是一样的,仅需要使用几行代码即可完成,下面写出来给大家分享:

1.iptables-A INPUT-p 47-j ACCEPT

2 iptables-A OUTPUT-p 47-j ACCEPT

3 iptables-A INPUT-p tcp--dport 1723-j ACCEPT

4 iptables-A OUTPUT-p tcp--sport 1723-j ACCEPT

简单的令人发指啊,但是解决的过程确实非常的辛酸的,大家要是有碰到同样的问题,可以参考上面的代码解决问题。

iptables允许PPTP后,还不需要要允许DNS,它必须要用到tcp和udp协议,代码如下:

1 iptables-A OUTPUT-p udp--sport 53-j ACCEPT

2 iptables-A INPUT-p udp--dport 53-j ACCEPT

以上是个人的分享,希望能帮到大家。

阅读剩余
THE END