Remote server with reverse tunnel, the busy port is sometimes closed

1

What happens is that I have two remote servers (serv1 and serv2) what happens is that when I enter the serv1 to then make the entry through the tunnel inverse to the serv2 there are times that the port through which entry is closed or it does not respond and in the terminal it returns "ssh: connect to host localhost port 2022: Connection refused" But there are times when I can access. I wanted to know how I can keep port 2022 in this case always open.

Thank you very much in advance.

    
asked by Diego C 22.11.2018 в 20:33
source

1 answer

0

There may be a process that is occupying that port. You can simply kill the process that is running on that port and then connect but first check the process occupies that port. Enter with ssh and monitor that port with some script, cronjob or with watch and with sudo netstat -tlpn | grep 2022 or with sudo lsof -i tcp:2022 .

That's thinking that there may be another process using that port, maybe if it can not connect, it closes it and then opens it to connect.

If I were in your case, that would be the first thing I would do since it could be (in one of the worst scenarios) an intrusion through that port.

The other is that you try to connect by another port to server1 and by another to server2, that is.

Server1

It will allow connections through the port, it is an example of port, 2001 from server2 (remote) to local port 22. That is, server2 is going to connect to its localhost through port 2001 (the one I came up with to indicate that it is server1).

ssh -N -f -R 2001:127.0.0.1:22 usuario@ip_servidor2

Server2

It will allow connections through the port, it is an example of port, 2002 from server1 (now it is remote) to local port 22. In a similar way to the previous one.

ssh -N -f -R 2002:127.0.0.1:22 usuario@ip_servidor1

And you can connect to server2 from server1 with:

$ ssh [email protected] -p 2002

And vice versa, from server2 to server1 with:

$ ssh [email protected] -p 2001

The explanation of what the parameters -N , -f and -R do can be found in man ssh . But in summary -f sends all requests to background, -N does not execute any command remotely and -R does a port forwarding as follows -R <puerto del servidor remoto>:<host>:<puerto_local_de_conexión> that is, with ssh -R to a remote ip, it allows that remote server to connect to its own host and its agreed local port and to enter through to the server that allowed that connection, allows it to see the traffic. And, in the case of the previous example, as it is to the port of ssh (port 22), it allows obtaining a shell.

Remember that all this can work with your pair of keys, or it does not matter, if you know the password of both users of both servers it also works.

    
answered by 25.11.2018 в 03:58