Linux启动防火墙与指定端口号

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/R_s_x/article/details/88914682

防火墙

  1. 查看防火墙状态
[root@localhost ~]# service iptables status
ptables: Firewall is not running.  

(防火墙未开启)

  1. 开启防火墙
[root@localhost init.d]# service iptables start
iptables: Applying firewall rules:                         [  OK  ]

(开启成功)

  1. 关闭防火墙
[root@localhost init.d]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

打开指定端口

查看对外开放端口

开发的端口在 /etc/sysconfig/iptables中。

[root@localhost init.d]# cat /etc/sysconfig/iptables
# 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 2181 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2888 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3888 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

开放端口(port:8080)
方法一
  1. 编辑iptables:
vim /etc/sysconfig//iptables

添加-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT

  1. 重启服务
/etc/init.d/iptables restart
  1. 保存
/etc/rc.d/init.d/iptables save
方法二

直接输入命令:

#iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

注意:
如果/etc/sysconfig/iptables不存在;
原因:
在新安装的Linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所以不存在/etc/sysconfig/iptables文件。
解决:
在控制台使用iptables命令随便写一条防火墙规则,如:iptables -P OUTPUT ACCEPT,
使用service iptables save进行保存,默认就保存到了/etc/sysconfig目录下的iptables文件中。

参考资料:
https://blog.csdn.net/lv_shijun/article/details/52453882
https://www.jianshu.com/p/b3068288d80d

猜你喜欢

转载自blog.csdn.net/R_s_x/article/details/88914682