article/linux/linux iptables 规则.md
2022-10-26 18:59:55 +08:00

50 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```sh
#允许 tcp 2181 端口 被192.168.53.151 地址访问
iptables -A INPUT -p tcp -s 192.168.53.151 --dport 2181 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.53.152 --dport 2181 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.53.153 --dport 2181 -j ACCEPT
iptables -A INPUT -p tcp -s 127.0.0.1 --dport 2181 -j ACCEPT
#禁止 tcp 2181 端口 被所有地址访问
iptables -A INPUT -p tcp -s 0.0.0.0/0 --dport 2181 -j DROP
#备份 iptables 规则
iptables-save > /etc/sysconfig/iptables
#还原 iptables 规则
iptables-restore < 文件名称
#立即生效 iptables 规则
service iptables save
```
| 参数 | 作用 |
| ------------- | ---------------------------------------------- |
| -P | 设置默认策略:iptables -P INPUT (DROP\|ACCEPT) |
| | FORWARD 转发INPUT 进站OUTPUT 出站 |
| -F | 清空规则链 |
| -L | 查看规则链 |
| -A | 在规则链的末尾加入新规则 |
| -I num | 在规则链的头部加入新规则 |
| -D num | 删除某一条规则 |
| -s | 匹配来源地址IP/MASK加叹号"!"表示除这个IP外。 |
| -R | 替换防火墙规则 |
| -Z | 清空防火墙数据表统计信息 |
| | |
| -d | 匹配目标地址 |
| -i 网卡名称 | 匹配从这块网卡流入的数据 |
| | |
| -o 网卡名称 | 匹配从这块网卡流出的数据 |
| -p | 匹配协议,如tcp,udp,icmp |
| --dport num | 匹配目标端口号 |
| --sport num | 匹配来源端口号 |
| --line-number | 行号 显示 |
![img](http://pic.61dz.com/pic/7775566-fb35d0a138063159.jpg)