0

I have a sshfs/fuse mounted folder and 2 local port forwarding in the background.

From this post:

How to clear local ssh port for forwarding to remote?

With this command i see only my mounted sshfs/fuse folder not the local port forwarding connection.

ps -af |grep [s]sh

See the local port forwarding connection process with:

ps -x |grep "[s]sh -fN"

or from this post:

Finding the process that is using a certain port in Linux

lsof -i tcp:[PORT]

I can kill 1 of the process for the local port forwarding connection with:

kill <id>

When i try to close/kill the process with:

ssh -O cancel -L [PORT]:[IP]:[PORT] [IP]

I receive error a msg like:

No ControlPath specified for "-O" command

From this post:

How do I remove an SSH forwarded port

This commands won't work too:

pkill -f "ssh -f -N [REMOTE_IP]"

and

ssh -O exit [REMOTE_IP]

From this post:

How to delete local ssh tunnel

and

ssh -o ControlPath=$socket -O check

i receive

command-line line 0: Missing argument.

From post:

How to tell if an ssh ControlMaster connection is in use

I wanna close/kill a given connection with a port and not all, if i got as an example 3 connections on a host with diff ports, with a ssh command not with kill process like:

ssh -O cancel -L ....

or an other ssh command.

How can i fit this and where is my error with?

ssh -O cancel -L [PORT]:[IP]:[PORT] [IP]

Z0OM
  • 1
  • 1
  • 4
  • 20

2 Answers2

1

If you know the port of the Process (here 8080) simply use

kill $(lsof -t -i:8080)

where

lsof -i:8080

gives you all PIDs

0

As far as I know, you need to identify the control path when cancelling port forwarding.

So when you create the port forwarding you would specify a control path such as /tmp/mysshcontrolpath, i.e.

ssh -o ControlMaster=auto -o ControlPath=/tmp/mysshcontrolpath -N -R [PORT]:[IP]:[PORT] [IP]

... and then when you want to stop the port forwarding you would use exactly the same thing, but with the addition of "-O exit", i.e.

ssh -o ControlMaster=auto -o ControlPath=/tmp/mysshcontrolpath -O exit -N -R [PORT]:[IP]:[PORT] [IP]

... and then you should get the response "Exit request sent".

Additionally I always use the ExitOnForwardFailure option when creating port forwarding so that it doesn't hang if the attempt fails, i.e.

ssh -o ExitOnForwardFailure=yes -o ControlMaster=auto -o ControlPath=/tmp/mysshcontrolpath -N -R [PORT]:[IP]:[PORT] [IP]

I understand that some flavours of Linux come with a control path pre-specified in a ~/.ssh/config file, so that one doesn't need to specify a control path when creating port forwarding. You can create this config file in any version of Linux, as discussed here.

Hope this gives you some clues to help solve your problem...