iptables基本用法

iptables基本用法

参考官方文档:https://wiki.centos.org/zh/HowTos/Network/IPTables

注意 iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 这句很重要!网上好多其他教程都漏了这句,这样会导致只能建立连接但无法通过该连接接收数据。

 

火狐截图_2016-08-25T17-37-58.570Z


example:

#!/bin/bash
#
# iptables 设置脚本

#
# 清除 iptables 内一切现存的规则
#
iptables -F

#
# 接收来自 localhost 的包
#
iptables -A INPUT -i lo -j ACCEPT

#
# 接收来自现有连接(ESTABLIESHED, RELATED)的包
#
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#
# 允许 SSH 连接到 tcp 端口 22
# 当通过 SSH 远程连接到服务器,你必须这样做才能群免被封锁于系统外
#
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

#
# 设置 INPUT、FORWARD、及 OUTPUT 链的缺省政策
#
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

#
# 允许 ping
#
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

#
# 接收来自信任地址的包
#
iptables -A INPUT -s 112.124.104.159 -j ACCEPT

#
# 存储设置
#
/sbin/service iptables save

#
# 列出规则
# -L是列出,-v是列出详细信息,-n是数值显示ip跟port
#
iptables -L -v -n

 

Leave a Reply

Your email address will not be published. Required fields are marked *