iptables配置脚本
记得提前安装iptables
# Centos/Redhat
~# yum install iptables-services
# Ubuntu/Debian
~# apt-get install iptables-persistent
#!/bin/bash
# 启用 IP 转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# 配置 iptables 规则
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.2:8080
iptables -A FORWARD -p tcp -d 192.168.1.2 --dport 8080 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -j MASQUERADE
# 保存 iptables 规则
iptables-save > /etc/sysconfig/iptables
也可以
## Centos/Redhat系统
~# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
~# systemctl enable iptables
~# systemctl restart iptables
## Ubuntu/Debian系统
~# netfilter-persistent save
~# netfilter-persistent reload
firewall配置脚本
#!/bin/bash
# 启用 IP 转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# 启用 firewalld 服务
systemctl start firewalld
systemctl enable firewalld
# 配置 firewalld 进行端口转发
firewall-cmd --permanent --add-masquerade
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.2
firewall-cmd --reload