Margrop
Articles158
Tags353
Categories21
1password AC AP API AppDaemon Aqara Cron Date Diagrams.net HA HADashboard HomeAssistant IP IPv4 Java LVM‑Thin Linux MacOS MySQL NAS PPPoE PostgreSQL ProcessOn Proxmox VE SSL Shell TTS TimeMachine UML Uptime Kuma Web Windows activate ad adb adblock agent aligenie aliyun alpine annotation aop authy autofs backup baidupan bash bitwarden boot brew browser caddy2 cdn centos cert certbot charles chat chrome classloader client clone closures cloudflare cmd command commit container crontab ctyun ddsm demo dependency deploy developer devtools dll dns docker domain download draw drawio dsm dump dylib edge exception export fail2ban feign firewall-cmd flow frp frpc frps fuckgfw function gcc gfw git github golang gperftools gridea grub gvt-g hacs havcs heap hello hexo hibernate hidpi hoisting homeassistant hosts html htmlparser https idea image img img2kvm import index install intel io ios ip iptables iptv ipv6 iso java javascript jetbrains jni jnilib jpa js json jsonb jupter jupyterlab jvm k8s kernel key kid kms kodi koolproxy koolproxyr kvm lan lastpass launchctl learning lede letsencrypt linux live low-code lvm lxc m3u8 mac macos mariadb markdown maven md5 microcode mirror modem modules monitor mount mstsc mysql n2n n5105 nas network nfs node node-red nodejs nohup notepad++ npm nssm ntp oop openfeign openssl os otp ovz packet capture pat pdf pem perf ping pip plugin png powerbutton print pro proxy pve pvekclean python qcow2 qemu qemu-guest-agent rar reboot reflog remote remote desktop renew repo resize retina root route router rule rules runtime safari sata scipy-notebook scoping scp server slmgr so socks source spk spring springboot springfox ssh ssl stash string supernode svg svn swagger sync synology systemctl tap tap-windows tapwindows telecom template terminal tls token totp tvbox txt ubuntu udisk ui undertow uninstall unlocker upgrade url v2ray vhd vim vlmcsd vm vmdk web websocket wechat windows with worker wow xiaoya xml yum zip 中国电信 云电脑 交换机 光猫 公网IP 内存 内网IP 升级 启动 夏令时 天猫精灵 天翼云 安装 容器 导入 小米 常用软件 广告屏蔽 序列号 应用市场 异常 抓包 描述文件 时区 显卡虚拟化 智能家居 智能音箱 梯子 模块 流程 流程图 浏览器 漫游 激活 火绒 电信 画图 直播源 续期 网关 网络风暴 群晖 腾讯 虚拟机 证书 路由 路由器 软件管家 软路由 运维监控 镜像 镜像源 门窗传感器 防火墙 阿里云 阿里源 集客

Hitokoto

Archive

【转】iptables持久化方案

【转】iptables持久化方案

debian若没有安装iptables,使用以下命令安装

1
apt-get install iptables

清除已有规则

1
iptables -F;iptables -X;iptables -Z

开放端口

允许本地回环接口(即运行本机访问本机) && 允许已建立的或相关连的通行

1
iptables -A INPUT -i lo -j ACCEPT;iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

允许所有本机向外的访问

1
iptables -A OUTPUT -j ACCEPT

转自https://www.cnblogs.com/goldenstones/articles/8868577.html

允许访问22端口

1
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

允许访问80端口

1
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

允许本地访问MySQL3306端口,禁止外部访问

1
iptables -A INPUT -p tcp -s 127.0.0.1 --dport 3306 -j ACCEPT;iptables -A INPUT -p tcp --dport 3306 -j DROP

允许FTP服务的21和20端口

1
iptables -A INPUT -p tcp --dport 21 -j ACCEPT;iptables -A INPUT -p tcp --dport 20 -j ACCEPT

允许安全宝监控的udp 161端口

1
2
iptables -I INPUT -p udp -s 60.195.252.107 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 60.195.252.110 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 127.0.0.1 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 45.63.121.42 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 192.168.1.2 --dport 161 -j ACCEPT
#如果有其他端口的话,规则也类似,稍微修改上述语句就行

允许ping

1
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

禁止其他未允许的规则访问(注意:如果22端口未加入允许规则,SSH链接会直接断开。)

1
iptables -A INPUT -j REJECT;iptables -A FORWARD -j REJECT

屏蔽单个IP的命令

1
iptables -I INPUT -s 123.45.6.7 -j DROP

封整个段即从123.0.0.1到123.255.255.254的命令

1
iptables -I INPUT -s 123.0.0.0/8 -j DROP

封IP段即从123.45.0.1到123.45.255.254的命令

1
iptables -I INPUT -s 124.45.0.0/16 -j DROP

封IP段即从123.45.6.1到123.45.6.254的命令

1
iptables -I INPUT -s 123.45.6.0/24 -j DROP

查看已添加的iptables规则

1
2
iptables -L -n
iptables -L -n --line-numbers

删除INPUT里序号为3的规则

1
iptables -D INPUT 3

临时保存规则

1
iptables-save

持久化保存

该命令保存的规则在系统重启后会失效,如何持久化保存?接着往下看
正常情况下,我们写入的iptables规则将会在系统重启时消失。即使我们使用iptables-save命令将iptables规则存储到文件,在系统重启后也需要执行iptables-restore操作来恢复原有规则。

iptables持久化方案,如何让防火墙规则重启后依旧有效?
推荐使用iptables-persistent工具:

首先,安装:

1
apt-get install iptables-persistent

保存当前设置的iptables防火墙规则

1
2
3
/etc/init.d/iptables-persistent save

netfilter-persistent save

重新载入防火墙规则

1
2
3
/etc/init.d/iptables-persistent reload

netfilter-persistent reload
  • 说明:保存的规则文件路径如下
    1
    2
    /etc/iptables/rules.v4
    /etc/iptables/rules.v6
Author:Margrop
Link:http://blog.margrop.com/post/persistent-iptables/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可