iptables&gost端口转发

iptables

一键脚本

natcfg

wget --no-check-certificate -qO natcfg.sh https://raw.githubusercontent.com/arloor/iptablesUtils/master/natcfg.sh
bash natcfg.sh

输出如下:

#############################################################
# Usage: setup iptables nat rules for domian/ip             #
# Website:  http://www.arloor.com/                          #
# Author: ARLOOR <admin@arloor.com>                         #
# Github: https://github.com/arloor/iptablesUtils           #
#############################################################

你要做什么呢(请输入数字)?Ctrl+C 退出本脚本
1) 增加转发规则          3) 列出所有转发规则
2) 删除转发规则          4) 查看当前iptables配置
#?

此时按照需要,输入1-4中的任意数字,然后按照提示即可

系统自带

文章来源

安装iptables

yum -y install iptables

开启关闭

临时(重启失效):

sysctl -w net.ipv4.ip_forward=1 #开启
sysctl -w net.ipv4.ip_forward=0 #关闭

永久:
vi /etc/sysctl.conf修改或者添加

net.ipv4.ip_forward = 1 #开启
net.ipv4.ip_forward = 0 #关闭

或者

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

执行生效:sysctl -p

公网转发

A(转发机)
公网:1.1.1.1
内网:10.0.0.1

B(目标主机)
公网:2.2.2.2

A主机8880端口转发至B主机8443端口

iptables -t nat -A PREROUTING -p tcp --dport 8880 -j DNAT --to-destination 2.2.2.2:8443
iptables -t nat -A POSTROUTING -p tcp -d 2.2.2.2 --dport 8443 -j SNAT --to-source 10.0.0.1

查看转发

iptables  -t  nat  -nL

删除

删除指定

iptables -t nat -A xxxxxx
改为
iptables -t nat -D xxxxxx

删除所有

iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING

备份恢复

修改完规则后,将规则从内存保存到文件,位置根据需要可以改

iptables-save > /root/iptables.conf

开机自动恢复,在/etc/rc.d/rc.local添加

iptables-restore < /root/iptables.conf

如果上面方法无效

yum -y install iptables-services

放行端口特别注意,避免SSH无法连接

iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

iptables-services与iptables有点区别
A主机8880端口转发至B主机8443端口

iptables -t nat -A PREROUTING -p tcp --dport 8880 -j DNAT --to-destination 2.2.2.2:8443
iptables -t nat -A POSTROUTING -p tcp -d 2.2.2.2 --dport 8443 -j SNAT --to-source 10.0.0.1
iptables -t filter -I FORWARD -d 2.2.2.2 -j ACCEPT 
iptables -t filter -I FORWARD -s 2.2.2.2 -j ACCEPT #这段可以不执行

自动启动

chkconfig iptables on
service iptables save //每次修改规则后执行
service iptables restart
##这里无法自启iptables-services规则的话,可以用上面的方式

其他用法

屏蔽全部IP连接25端口

iptables -I FORWARD -p tcp --dport 25 -j DROP

禁止域名为http://www.xxx.com的网站

iptables -I FORWARD -d http://www.xxx.com -j DROP

禁止本机访问所有外网22端口

iptables -A OUTPUT -p tcp --sport 22 -j DROP

禁止/解封 IP

iptables -I INPUT -s 10.0.0.1 -j DROP
iptables -D INPUT -s 10.0.0.1 -j DROP

gost

一键脚本

来源

wget https://ghproxy.com/https://raw.githubusercontent.com/KANIKIG/Multi-EasyGost/master/gost.sh && chmod +x gost.sh
./gost.sh

源码

github

wget -N --no-check-certificate https://github.com/ginuerzh/gost/releases/download/v2.11.0/gost-linux-amd64-2.11.0.gz && gzip -d gost-linux-amd64-2.11.0.gz
mv gost-linux-amd64-2.11.0 gost
chmod +x gost

国内镜像

wget -N --no-check-certificate https://zhujiget.com/wp-content/uploads/sh/gost.gz && gzip -d gost.gz
chmod +x gost

普通端口转发模式

nohup ./gost -L=tcp://:本地端口/转发的IP:远程端口 -L=udp://:本地端口/转发的IP:远程端口 >> /dev/null 2>&1 &

停止

yum -y install lsof  ##安装lsof命令
lsof -i:8090  ##查看8090端口占用的进程
kill -9 12345  ##此处的12345为进程PID号

更多参考&来源

最后修改:2022 年 04 月 30 日 02 : 18 PM