iptables 配置
基于Centos 7
注意:CentOS 7默认的防火墙不是iptables,而是firewalld
停止firewalld服务
1# 停止firewalld服务
2$ systemctl stop firewalld
3# 禁用firewalld服务
4# $ systemctl mask firewalld
5# 删除firewalld
6$ yum erase firewalld
安装 iptables
1# 先检查是否安装了iptables
2$ systemctl status iptables
3# 安装iptables
4$ yum install iptables iptables-services -y
启动 iptables
1# 注册iptables服务,相当于以前的chkconfig iptables on
2$ systemctl enable iptables
3# 开启服务
4$ systemctl start iptables
5# 查看状态
6$ systemctl status iptables
7# 重启防火墙
8$ systemctl restart iptables
9# 保存规则
10$ service iptables save
11# 如果报“-bash: service: command not found”,则需要安装initscripts
12$ yum install initscripts -y
基础规则
注意添加规则的先后顺序
1# 允许本地回环接口(即运行本机访问本机)
2$ iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
3# 允许已建立的或相关连的通行
4$ iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
5# 允许ping
6# $ iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
7# or
8$ iptables -A INPUT -p icmp -j ACCEPT
9# 添加SSH访问端口
10$ iptables -A INPUT -p tcp -m tcp --dport 28124 -j ACCEPT
11# 允许所有本机向外的访问
12$ iptables -P OUTPUT ACCEPT
13# 禁止其他未允许的规则访问
14$ iptables -P INPUT DROP // 默认入站规则为拒绝
15$ iptables -P FORWARD DROP // 默认转发规则为拒绝
其它规则
1# 允许访问指定端口
2$ iptables -A INPUT -p tcp -m tcp --dport 6443 -j ACCEPT
3# 允许访问连续端口
4$ iptables -A INPUT -p tcp -m tcp --dport 21:25 -j ACCEPT
5# 允许访问不连续端口
6$ iptables -A INPUT -p tcp -m multiport --dport 21:25,135:139 -j ACCEPT
7
8# 转发数据包
9# 将<本地端口>接收到的TCP数据包,直接转发到<目标IP>的<目标端口>
10$ iptables -t nat -A PREROUTING -p tcp --dport <本地端口> -j DNAT --to-destination <目标IP>:<目标端口>
11# 将<目标IP>的<目标端口>发来的TCP数据包,IP地址修改为<本地IP>后,原路转发回去
12$ iptables -t nat -A POSTROUTING -p tcp -d <目标IP> --dport <目标端口> -j SNAT --to <本地IP>
13# 如果要添加内网ip信任(接受其所有TCP请求)
14$ iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
15# 封停一个IP
16$ iptables -I INPUT -s ***.***.***.*** -j DROP
17# 删除规则
18$ iptables -D INPUT #rulenum#
19# or 使用建立规则时条件,删除指定规则,这个方法比如适合代码使用
20$ iptables -D INPUT -p tcp -m tcp --dport 6443 -j ACCEPT
21# 解封一个IP
22$ iptables -D INPUT -s ***.***.***.*** -j DROP
查看规则
1$ iptables -L -n --line-numbers
重置规则
1# 使用这些命令刷新和重置 iptables 到默认状态
2
3# 清除已有iptables规则
4$ iptables -F
5
6$ iptables -X
7
8$ iptables -t nat -F
9
10$ iptables -t nat -X
11
12$ iptables -t mangle -F
13
14$ iptables -t mangle -X
15
16$ iptables -t raw -F
17
18$ iptables -t raw -X
19
20$ iptables -t security -F
21
22$ iptables -t security -X
23
24$ iptables -P INPUT ACCEPT
25
26$ iptables -P FORWARD ACCEPT
27
28$ iptables -P OUTPUT ACCEPT
相关专栏文章
- Nuitka 参数列表
- ProxmoxVE 配置 DHCP 网络
- Windows防火墙配置
- 在 CentOS 上部署 NetBIOS
- Tmux 命令
- 配置 CentOS 7 的 CT 容器
- Ncftp 使用手册
- Centos7 安装 Postgresql
- 在CentOS中安装 Laravel 框架
- 虚拟主机安装 Laravel 框架
- snap 设置代理
- Rime输入法设置
- Windows系统配置
- 灌篮高手大结局(全国大赛后) 下
- 灌篮高手大结局(全国大赛后) 上
- VScode 在 Docker 容器内开发
- VSCode 配置 Python 开发环境
- ProxmoxVE 配置 BBR
- ProxmoxVE 关闭订阅提示
- Linux 文本文件操作
- Deepin 使用远程桌面
- Hyper-V 下设置 Deepin 分辨率
- VsCode 使用 Remote SSH 连接 Alpine Linux
- Alpine Linux 安装 Bash
- frp安装配置
- Alpine Linux 配置
- screen 命令
- Systemd 系统工具命令指南
- ProxmoxVE 命令行
- Hyper-V 环境下给 CentOS 磁盘扩容
- Hyper-V 下设置 Ubuntu 分辨率
- Windows 10 家庭版安装 Hyper-V
- Hyper-V 网络设置
- Docker Compose 指令
- 使用 SSH 连接 Github
- Alpine Linux 安装 Docker
- Alpine Linux 网络设置
- 虚拟机安装 Alpine Linux 3.16
- 山克 UPS 安装手册
- Git 命令行
- 使用 Snap 安装 Docker
- (CentOS 7 | Rocky 9) 安装 Docker
- 修复 ProxmoxVE RRD 错误
- FFmpeg脚本
- Yum 版本库管理
- Linux系统内核升级
- iptables 配置
- ProxmoxVE 配置 NAT 网络
- MySQL 数据操作
- 使用 nmcli 配置网络
- 使用 sed 命令操作
- Linux 系统初始化配置
- Github使用Hugo生成Blog
- Docker使用命令
- About
- Search