赞
踩
iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分。可以直接配置,也可以通过许多前端和图形界面配置。
iptables(选项)(参数)
-t<表>:指定要操纵的表; -A:向规则链中添加条目; -D:从规则链中删除条目; -i:向规则链中插入条目; -R:替换规则链中的条目; -L:显示规则链中已有的条目; -F:清楚规则链中已有的条目; -Z:清空规则链中的数据包计算器和字节计数器; -N:创建新的用户自定义规则链; -P:定义规则链中的默认目标; -h:显示帮助信息; -p:指定要匹配的数据包协议类型; -s:指定要匹配的数据包源ip地址; -j<目标>:指定要跳转的目标; -i<网络接口>:指定数据包进入本机的网络接口; -o<网络接口>:指定数据包要离开本机所使用的网络接口。
iptables安装与基本语法
# 安装 iptables
yum install iptables-services -y
# 开启 iptables
systemct start iptables 或 service iptables start
# 清楚所有规则,一定要执行,不然,你现在所有端口外部都没法访问
iptables -F
# 关闭端口开放,raw 为 table 名称,PREROUTING 为 “路由规则” 的前置拦截
iptables -t raw -I PREROUTING -p tcp --dport 端口 -j DROP
# 端口针对 ip 开放
iptables -t raw -I PREROUTING -s ip -p tcp --dport 端口 -j ACCEPT
iptables -t raw -I PREROUTING -s ip/16 -p tcp --dport 端口 -j ACCEPT
# 查看设置的规则
iptables -t raw -nvL
# 查看规则编号
iptables -t raw -L -n --line-number
# 删除添加的规则
iptables -t raw -D PREROUTING xx
# 开机自启动
systemctl enable iptables 或 service iptables enable
开放指定的端口
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机) iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #允许已建立的或相关连的通行 iptables -A OUTPUT -j ACCEPT #允许所有本机向外的访问 iptables -A INPUT -p tcp --dport 22 -j ACCEPT #允许访问22端口 iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口 iptables -A INPUT -p tcp --dport 21 -j ACCEPT #允许ftp服务的21端口 iptables -A INPUT -p tcp --dport 20 -j ACCEPT #允许FTP服务的20端口 iptables -A INPUT -j reject #禁止其他未允许的规则访问 iptables -A FORWARD -j REJECT #禁止其他未允许的规则访问
- 参数说明:
- --dport:目标端口
- --sport:源端口
- -d:目标ip
- -s:源ip
- -A:添加一条INPUT的规则(添加现有规则后面)
- -I:添加一条INPUT的规则(添加现有规则前面)
- -j:指定ACCEPT或DROP
- -p:协议
- -m iprange:
- -m 添加模块iprange,可同时设置多个ip或者连续ip段
- iprange模块指定连续的ip地址范围
- --src-range:源地址范围
- --dst-range:目标地址范围
-
- 规则配置
- #连续ip段
- iptables -I INPUT -m iprange --src-range 192.168.1.1-192.168.1.5 -j ACCEPT
-
- #允许192.168.1.1和192.168.1.5访问本机的3306和8080端口
- #添加多ip时使用逗号分开
- #添加多端口时使用"-m multiport"然后使用逗号分隔
- iptables -I INPUT -s 192.168.1.1,192.168.1.5 -p tcp -m multiport --dport 3306,8080 -j ACCEPT
-
- #不允许192.168.1.1-192.168.1.5访问本机的22端口
- iptables -I INPUT -m iprange --src-range 192.168.1.1-192.168.1.5 -p tcp --dport 22 -j DROP
-
- #目标地址转换:
- iptables -t nat -A PREROUTING -d 192.168.23.110 -p tcp --dport 80 -j DNAT --to-destination 192.168.23.111:8080
-
- #将本机的 80 端口转发到 8080 端口
- iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
- iptable -t nat -A PREROUTING -p tcp --dport "原端口" -j REDIRECT --to-port "目标端口"
-
-
-
-
屏蔽IP
iptables -I INPUT -s 123.45.6.7 -j DROP #屏蔽单个IP的命令 iptables -I INPUT -s 123.0.0.0/8 -j DROP #封整个段即从123.0.0.1到123.255.255.254的命令 iptables -I INPUT -s 124.45.0.0/16 -j DROP #封IP段即从123.45.0.1到123.45.255.254的命令 iptables -I INPUT -s 123.45.6.0/24 -j DROP #封IP段即从123.45.6.1到123.45.6.254的命令是
查看已添加的iptables规则
iptables -L -n -v
删除已添加的iptables规则
将所有iptables以序号标记显示,执行:
iptables -L -n --line-numbers比如要删除INPUT里序号为8的规则,执行:
iptables -D INPUT 8
完整配置
- # 清楚所有规则,一定要执行,不然,你现在所有端口外部都没法访问
- iptables -F
-
- #查看防火墙状态
- service iptables status
-
- #限制VNC只能堡垒机访问
- iptables -A INPUT -s 192.168.22.0/24 -p tcp --dport 5901 -j ACCEPT
-
- #保存规则
- service iptables save
-
- #重启防火墙
- service iptables restart
-
- #设置防火墙开机自启动
- systemctl enable iptables 或 service iptables enable
-
- #查看已添加规则
- iptables -L -n -v
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。