9. iptables 配置

简介: 9. iptables 配置  iptables 配置文件存放位置:[root@Demon yum.repos.d]# vim /etc/rc.
9. iptables 配置  
iptables 配置文件存放位置:
[root@Demon yum.repos.d]# vim /etc/rc.d/init.d/iptables

一、只给 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
相关文章
|
7月前
|
网络协议 Linux 网络安全
iptables 的四表五链
iptables 是 Linux 系统上用于定义防火墙规则的工具,它通过四个表和五个链来进行配置。下面是这些表和链的详细说明: 四个表: 1. filter 表:filter 表是最常用的表,用于过滤数据包。它包含了 INPUT、OUTPUT 和 FORWARD 三个默认的链。 2. nat 表:nat 表用于网络地址转换 (NAT)。它包含了 PREROUTING、POSTROUTING 和 OUTPUT 三个默认的链。nat 表用于修改数据包的 IP 地址和端口。 3. mangle 表:mangle 表用于修改数据包的特定字段,如 TTL(生存时间)、TOS(服务类型)等。它包含了
158 1
|
8月前
|
网络协议 网络安全 网络性能优化
|
网络协议
Iptables配置
查看iptables加载的模块 lsmod | egrep "nat|filter" iptable_filter 12810 0 ip_tables 27240 1 i...
831 0
|
网络协议 Linux 网络安全
|
网络协议 网络安全 网络虚拟化
|
Web App开发 网络协议 网络安全
|
网络协议