Question

port forwarding with socat

hi , i needed some port forwarding with socat or anything to connect my vps2 to my vps1 and load user and pass from it . i use vps1 and vps2 for openssh vpn so my users can get free internet without any filtering , someone helped me and i managed to port forward my ssh and load the user data on my vps1 with making this service file :

[Unit]

Description=SSH Forwarding Service

After=network.target



[Service]

User=nobody

ExecStart=/usr/bin/socat TCP-LISTEN:22,fork,reuseaddr TCP:vps1.example.com:22



[Install]

WantedBy=multi-user.target

but i also have v2ray panel on my vps1 with 2 config , one of em on port 443 with TLS on and cer , key file on my vps1 , the other on port 13081 without TLS . so i made 2 more file like this :

[Unit]

Description=SSH Forwarding Service

After=network.target



[Service]

User=nobody

ExecStart=/usr/bin/socat TCP-LISTEN:13081,fork,reuseaddr TCP:vps1.example.com:13081



[Install]

WantedBy=multi-user.target

both port 2255 and 13081 works fine but the port 443 that has TLS config doesnt work .

when i use this :

socat TCP-LISTEN:2255,fork TCP:sd1.aronashop.xyz:2255|socat TCP-LISTEN:443,fork TCP:sd1.aronashop.xyz:443|socat TCP-LISTEN:13081,fork TCP:sd1.aronashop.xyz:13081

all work fine even port 443 but the problem is when i close the SSH window it stops .

so how can i fix this port 443 problem ? i tried so many ways but none of em worked


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
May 3, 2023
Accepted Answer

Heya @driftinglightcyanurchin,

I just answered the question you provided here and in https://www.digitalocean.com/community/questions/how-to-forward-open-ssh-from-vps-to-vps?comment=196105. Sorry for the delay!

Anyway, I’ll post it here just to be sure:

To make the socat command run as a background process even after closing the SSH window, you can use the nohup command combined with &. However, I recommend creating a systemd service for each port you want to forward, just like you did for port 22

  1. Create a new systemd service file at /etc/systemd/system/v2ray-443-forward.service with the following content:
[Unit]
Description=V2Ray Port 443 Forwarding Service
After=network.target

[Service]
User=nobody
ExecStart=/usr/bin/socat TCP-LISTEN:443,fork,reuseaddr TCP:vps1.example.com:443

[Install]
WantedBy=multi-user.target
  1. sudo systemctl daemon-reload
  2. sudo systemctl enable v2ray-443-forward.service
  3. sudo systemctl start v2ray-443-forward.service

Do the same for the other ports (e.g., 13081) if you haven’t already created a systemd service for them.

Now, when you close your SSH window, the socat processes will continue to run in the background, managed by systemd.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel