一、只給 Centos 6.5 打開 22 和 80 端口,并且重啟后有效:
1、查看所有 iptables 配置:
[root@Demon yum.repos.d]# iptables -L -n (當前防火墻是關閉的)
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2、只允許 80 和 22 端口通過防火墻
# 清除所有規則
[root@Demon opt]# iptables -F
# 只允許 發送的包從 22 端口進入和返回, -A 的意思是添加一條 INPUT 規則, -p 指定為什么協議,--dport 為目標端口
[root@Demon opt]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@Demon opt]# iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
# 允許本機訪問本機
[root@Demon opt]# iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
[root@Demon opt]# iptables -A OUTPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
# 允許所有 IP 訪問 80 端口
[root@Demon opt]# iptables -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT
[root@Demon opt]# iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
[root@Demon opt]# iptables -P INPUT DROP
[root@Demon opt]# iptables -P OUTPUT DROP
[root@Demon opt]# iptables -P FORWARD DROP
# 保存配置
[root@Demon opt]# iptables-save > /etc/sysconfig/iptables
[root@Demon opt]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT all -- localhost localhost
ACCEPT tcp -- anywhere anywhere tcp dpt:http
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp spt:ssh state ESTABLISHED
ACCEPT all -- localhost localhost
ACCEPT tcp -- anywhere anywhere tcp spt:http state ESTABLISHED
# 重啟電腦后, ping 一下百度:
[root@Demon opt]# ping www.baidu.com
ping: unknown host www.baidu.com
二、關閉 iptables
# 打開防火墻,重啟生效
# chkconfig iptables on
# 關閉防火墻,重啟生效
# chkconfig iptables off
# 打開防火墻,立即生效,重啟后不生效
# chkconfig start
# 關閉防火墻,立即生效,重啟后不生效
# chkconfig stop