ITPub博客

首页 > 云计算 > 公有云实践 > 分享:有关Linux服务器(在防火墙iptables)开放端口的操作总结

分享:有关Linux服务器(在防火墙iptables)开放端口的操作总结

公有云实践 作者:shangyun 时间:2021-12-17 18:01:35 0 删除 编辑

用的小鸟云内蒙节点BGP线路的Linux云服务器,在使用过程中,遇到需要在防火墙中打开指定的端口,但不知道怎么操作,在提交工单咨询后,又参考了他们的文档,总结如下:

一、开放指定的端口

开放指定端口语法如下:

firewall-cmd --zone=public --add-port=开放指定端口/tcp --permanent

注意:执行完上述命令后,需要重加载配置立即生效,命令为:firewall-cmd --reload

--zone:表示作用域

作用域级别有如下可选:

1. drop:丢弃所有进入的包,而不给出任何响应

2. block:拒绝所有外部发起的连接,允许内部发起的连接

3. public: 允许指定的进入连接

4. external:同上,对伪装的进入连接,一般用于路由转发

5. dmz:允许受限制的进入连接

6. work:允许受信任的计算机被限制的进入连接,类似 workgroup

7. home:同上,类似 homegroup

8. internal:同上,范围针对所有互联网用户

9. trusted:信任所有连接

--add-port:表示添加的端口,端口后跟通信协议,比如:开放80端口(--add-port=80/tcp)

--permanent:表示永久生效,若没有此参数,防火墙重启将失效

例如开放80端口,命令如下:

`firewall-cmd --zone=public --add-port=80/tcp --permanent`

二、在iptables上放行新的端口(这里将默认22端口号修改为33端口号)

输入命令放行33端口。

[root@niaoyun ~]# iptables -I INPUT -p tcp --dport 33 -j ACCEPT

查看防火墙规则,发现33端口号已经放行了。

[root@niaoyun ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:33

295 23186 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

34 2310 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0

2342 200K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 15 packets, 1412 bytes)

pkts bytes target prot opt in out source destination

I

ptables规则已经更改,我们需要对规则进行保存。

[root@niaoyun ~]# service iptables save

iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]

保存完毕,重启iptables服务。

[root@niaoyun ~]# service iptables restart

iptables: Setting chains to policy ACCEPT: filter [ OK ]

iptables: Flushing firewall rules: [ OK ]

iptables: Unloading modules: [ OK ]

iptables: Applying firewall rules: [ OK ]

同样,用此方法也可以放行web的默认端口80。

iptables -I INPUT -p tcp --dport 80 -j ACCEPT && service iptables save && service iptables restart

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69990023/viewspace-2848378/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-12-04

  • 博文量
    92
  • 访问量
    55395