創建轉發腳本

nano /usr/local/bin/socat_forward_1.sh

輸入內容

#!/bin/bash

# 转发本地 TCP 端口 49470 到远程主机 xxxx.com 的 TCP 端口 xxxx
socat TCP-LISTEN:49470,fork TCP:xxxx.com:xxxx

賦予权限

sudo chmod +x /usr/local/bin/socat_forward_1.sh

創建服務单元

sudo nano /etc/systemd/system/socat_forward_1.service

輸入內容

[Unit]
Description=Socat Port Forwarding Service
After=network.target

[Service]
ExecStart=/usr/local/bin/socat_forward_1.sh
Restart=always
User=root
Group=root

[Install]
WantedBy=multi-user.target

重新加载 systemd 配置

sudo systemctl daemon-reload

啟動服務

設置開機自启动

sudo systemctl start socat_forward_1
sudo systemctl enable socat_forward_1

檢查服務狀態

sudo systemctl status socat_forward_1

如果需要udp轉發,則需要新建並編寫新的腳本文件,並將文件中的TCP改為UDP