在CentOS下配置iptables防火墙端口

CentOS下配置iptables防火墙

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

1.打开iptables的配置文件:

 代码如下

复制代码

vi /etc/sysconfig/iptables

通过/etc/init.d/iptables status

命令查询是否有打开80端口,如果没有可通过两种方式处理:

方法一:

1.修改vi /etc/sysconfig/iptables命令添加使防火墙开放80端口

 代码如下

复制代码

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

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

 代码如下

复制代码

/etc/init.d/iptables stop  

 

#start 开启 

#restart 重启

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

 代码如下

复制代码

 

# 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

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

[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.   [失败]

方法二:

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

 代码如下

复制代码

[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

这样就搞定了,查看效果

 代码如下

复制代码

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值