Question

how to forward open ssh from vps to vps

hi , i have 2 diiferent vps with 2 different ip and domain , i made some ssh accounts so peoples can login and use ssh to bypass my country internet filtering . bcuz of some reason i need to use 2 vps , but i wanna make accounts only on vps1 and the vps2 get forwarded and login to vps 1 accounts : for example :

on my vps 1 , i made some accounts so users can login with this : vps1 domain or ip ssh port username password

and use free internet .

but bcuz of some reasons , i need to give some other peoples my Vps 2 domain and ip , but i dont wanna make accounts again on vps 2 .

so what i want is when someone use : vps 2 domain or ip ssh port ( same as vps 1 ) username password

get forwarded to Vps 1 that i made ssh username and password on it and login to use free internet . is it possible to do such a thing only with open ssh ? is it possible to have vps 1 with ssh username and passes and vps2 without any user and only get forwarded to vps1 and use that username and pass data to get login to vps 1 ?


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 2, 2023
Accepted Answer

Heya @driftinglightcyanurchin,

You can achieve this by using SSH port forwarding between your two VPS and a simple systemd service. You’ll need to configure VPS2 to forward incoming SSH connections to VPS1.

In such cases, I’ve used the socat utility. Socat (for SOcket CAT) establishes two bidirectional byte streams and transfers data between them. Data channels may be files, pipes, devices (terminal or modem, etc.), or sockets (Unix, IPv4, IPv6, raw, UDP, TCP, SSL)

To install it, run:

  1. sudo apt-get update
  2. sudo apt-get install socat

Configure VPS2 to forward incoming SSH connections to VPS1:

Create a new systemd service file at /etc/systemd/system/ssh-forward.service:

[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

Don’t forget to replace vps1.example.com with the domain or IP address of VPS1. Enable and start the SSH forwarding service:

  1. sudo systemctl daemon-reload
  2. sudo systemctl enable ssh-forward.service
  3. sudo systemctl start ssh-forward.service

Now, when someone connects to VPS2 using the SSH port (22 by default), their connection will be forwarded to VPS1. They can then authenticate using the username and password created on VPS1.

Please note that this setup assumes that VPS2 doesn’t need to accept SSH connections for its own management. If you need to manage VPS2 via SSH, you should configure a different port for the forwarding service.

thanks man, i needed to forward 2 more ports 443 , 13081 , i made the same file with ssh-dorward2 , 3 and now ports 2255 for ssh , and 13081 works perfect , but on port 443 bcuz its for v2ray and has an TLS option it wont work , i think its bcuz on this config i put my vps1 domain cert , key . i think thats why when someone use the connection that has vps2 domain cant work well with this 443 port forward , how can i fix this problem ?

Try DigitalOcean for free

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

Sign up

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.