centos linux防火墙添加80端口iptables

2016-06-04 23:06:06 查看 1327 回复 0

CentOS防火墙在

虚拟机

的CENTOS装好APACHE不能用,郁闷,解决方法如下 

 

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT 

/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT 

 

然后保存: 

 

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

centos 5.3,5.4以上的版本需要用 

service iptables save 

来实现保存到配置文件。 

这样重启计算机后,CentOS防火墙默认已经开放了80和22端口。 

 

这里应该也可以不重启计算机: 

 

/etc/init.d/iptables restart 

 

CentOS防火墙的关闭,关闭其服务即可: 

 

查看CentOS防火墙信息:/etc/init.d/iptables status 

 

关闭CentOS防火墙服务:/etc/init.d/iptables stop 

 

永久关闭?不知道怎么个永久法: 

 

chkconfig –level 35 iptables off 

 

上面的内容是针对老版本的centos,下面的内容是基于新版本。 

 

iptables -P INPUT DROP 

 

这样就拒绝所有访问 CentOS 5.3 本系统数据,除了 Chain RH-Firewall-1-INPUT (2 references) 的规则外 , 呵呵。 

 

用命令配置了 iptables 一定还要 service iptables save 才能保存到配置文件。 

 

cat /etc/sysconfig/iptables 可以查看 防火墙 iptables 配置文件内容 

 

# Generated by iptables-save v1.3.5 on Sat Apr 14 07:51:07 2001 

*filter 

:INPUT DROP [0:0] 

:FORWARD ACCEPT [0:0] 

:OUTPUT ACCEPT [1513:149055] 

:RH-Firewall-1-INPUT - [0:0] 

-A INPUT -j RH-Firewall-1-INPUT 

-A FORWARD -j RH-Firewall-1-INPUT 

-A RH-Firewall-1-INPUT -i lo -j ACCEPT 

-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type any -j ACCEPT 

-A RH-Firewall-1-INPUT -p esp -j ACCEPT 

-A RH-Firewall-1-INPUT -p ah -j ACCEPT 

-A RH-Firewall-1-INPUT -d 224.0.0.251 -p udp -m udp --dport 5353 -j ACCEPT 

-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT 

-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT 

-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 

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

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited 

COMMIT 

# Completed on Sat Apr 14 07:51:07 2001 

 

另外补充: 

CentOS 防火墙配置 80端口 

看了好几个页面内容都有错,下面是正确方法: 

#/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT 

#/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT 

 

然后保存: 

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

 

再查看是否已经有了: 

[root@

vc

entos ~]# /etc/init.d/iptables status 

Table: filter 

Chain INPUT (policy ACCEPT) 

num target prot opt source destination 

1 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:80 

2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 

3 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0 

 

Chain FORWARD (policy ACCEPT) 

num target prot opt source destination 

1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0 

 

* 设置iptables为自动启动 

chkconfig --level 2345 iptables on 

 

可能因为大家使用的版本不一,所有使用方法也略有不同。 

 

如果需要远程管理

mysql

,则使用以下指令临时打开,用完后关闭  

 

* 打开指令  

iptables -A INPUT -p tcp -s xxx.xxx.xxx.xxx --dport 3306 -j ACCEPT  

 

* 关闭指令  

iptables -D INPUT -p tcp -s xxx.xxx.xxx.xxx --dport 3306 -j ACCEPT