Linux中在防火墙中开启80端口方法示例

2025-05-27 0 40

linux如果刚安装好防火墙时我们常用的端口是没有增加的,也就是说不能访问,那么要怎么把常用端口增加到防火墙通过状态呢,下面我们以80端口为例子吧。

最近自己在学习Linux。搭建一个LNMP环境。在测试时一切都好。然后重启Linux后。再次访问网站无法打开。最终原因是在防火墙中没有加入 80 端口的规则。具体方法如下:

在CentOS下配置iptables防火墙,是非常必要的。来我们学习如何配置!,其它版本一下:

1.打开iptables的配置文件:

代码如下

?

1
vi /etc/sysconfig/iptables

通过/etc/init.d/iptables status
命令查询是否有打开80端口,如果没有可通过两种方式处理:
1.修改vi /etc/sysconfig/iptables命令添加使防火墙开放80端口

代码如下

?

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

2.关闭/开启/重启防火墙

代码如下

?

1

2

3
/etc/init.d/iptables stop

#start 开启

#restart 重启

添加好之后防火墙规则如下所示:

代码如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

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

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT

-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT

-A INPUT -j REJECT –reject-with icmp-host-prohibited

-A FORWARD -j REJECT –reject-with icmp-host-prohibited

COMMIT

/etc/init.d/iptables restart

补充,有些朋友喜欢这样做

代码如下

?

1

2

3
vi /etc/sysconfig/iptables

-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火墙)

-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT(允许3306端口通过防火墙)

按照这种方法测试,发现重启防火墙的时候,回报这两行错误。

?

1

2

3

4

5

6

7

8
[root@localhost ~]# /etc/init.d/iptables restart

iptables:清除防火墙规则: [确定]

iptables:将链设置为政策 ACCEPT:filter [确定]

iptables:正在卸载模块: [确定]

iptables:应用防火墙规则:Bad argument `–-state'

Error occurred at line: 11

Try `iptables-restore -h' or 'iptables-restore --help' for more information.

[失败]

发现这种方法并不好使,于是尝试另外一种,通过命令去添加端口的方法。

代码如下

?

1

2

3
[root@centos httpd]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

[root@centos httpd]# /etc/rc.d/init.d/iptables save

[root@centos httpd]# /etc/init.d/iptables restart

这样就搞定了,查看效果

代码如下

?

1
[root@centos httpd]# /etc/init.d/iptables status

总结

以上就是本文关于Linux中在防火墙中开启80端口方法示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Linux企业运维人员常用的150个命令分享、浅谈Linux的库文件等,有什么问题可以随时留言,小编会及时回复大家的。

原文链接:http://blog.csdn.net/panpan639944806/article/details/24969707

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Linux中在防火墙中开启80端口方法示例 https://www.kuaiidc.com/61389.html

相关文章

发表评论
暂无评论