Linux-iptables

教程

http://man.linuxde.net/iptables
https://www.cnblogs.com/zclzhao/p/5081590.html
http://blog.51cto.com/ailixing/38543
https://www.linuxprobe.com/chapter-08.html
http://blog.51cto.com/xiaogongju/2087254
https://blog.csdn.net/reyleon/article/details/12976341
http://www.zsythink.net/archives/1199
https://www.cnblogs.com/linuxprobe/p/5643684.html

  • 设置默认规则 & 添加规则
1
2
3
4
5
6
iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 允许443端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # ssh 端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT # tcp ftp 端口
iptables -A INPUT -p udp --dport 21 -j ACCEPT # udp ftp 端口
iptables -A INPUT -p icmp -j ACCEPT # 允许ping命令
iptables -P INPUT DROP # `-P` 设置默认规则,不参与规则匹配顺序检索

概述

  • 未开放的端口,用nmap扫描的话,端口状态是closed

命令概览

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-I # 插入规则,默认插入到头部
-A # 追加规则,追加到规则末尾
-D # 删除规则
-R # 替换规则
-L # 显示所选链的所有规则
-F # 清空所选链
service iptables save # 保存规则,否则重启会失效 Saving firewall rules to /etc/sysconfig/iptables
iptables-save # 备份规则,需要额外参数,例如备份文件路径
iptables-restore # 恢复规则,需要额外参数,例如备份文件路径

# 停止 iptables
service iptables stop
systemctl stop iptables

# 重启 iptables
service iptables restart
systemctl restart iptables

# 启动 iptables
service iptables start
systemctl start iptables
1
2
3
4
-s(source)//数据包的发送源
-p(protocol)//协议
--dport(端口)
-j 规则执行的操作

列出规则,主要开关-L

1
2
3
4
5
6
iptables -L -n -v

# `--line-numbers` 显示规则列表的时候加上规则序号,
# `-n` IP地址用数字形式表示
iptables -L -n --line-numbers
iptables -nvL --line-n
  • 删除规则,编号从1开始
1
iptables -D INPUT 1
  • 清空规则
1
iptables -F
  • 备份/还原规则
1
2
iptables-save > ~/iptables.rules
iptables-restore < ~/iptables.rules
  • 重启 iptables
1
service iptables restart
  • 允许已建立的或相关连的通行
1
2
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  • 允许所有本机向外的访问
1
iptables -A OUTPUT -j ACCEPT
  • ??
1
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

Debian 上的 iptables

https://wiki.debian.org/iptables

Iptables if-pre-up blocking my network interfaces - Debian


Linux-iptables
https://dnacore.github.io/post/9a1d4f18-1009-4672-8c33-b915262b24d7.html
作者
DNACore
发布于
2018年12月11日
更新于
2023年12月12日
许可协议