0

What I want is like following:

  1. I could setup a ssh tunnel at background on a Linux machine(Actually, I'd like to setup it in my .bashrc)
  2. I could run process using this tunnel as any user on this machine at any time, only if the ssh tunnel process is active.
  3. When I quit from the terminal/session, the ssh tunnel should also quit as well.

I know I could establish a background ssh tunnel in Linux as following:

ssh -Nf -L 8000:1.2.3.4:8000 username@2.3.4.5

While at the time I exit from this terminal/session, this ssh tunnel would still run at background.

How to make it also quit automitically?

For my case, I need set a SSH tunnel almost every time I login my server(in order to bypass stupid company firewall). While on another hand, I prefer leaving nothing after my logout.


Please note:
This http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/ doesn't meet my requirement. Because it requires me to launch my process right after the ssh tunnel process, and if my first task with ssh tunnel finished, I need setup the ssh tunnel again for a second task. On the other hand, if I set a long sleep time, then the ssh tunnel will not quit with my terminal.

Maybe editing .bash_logout is possible? I hope there's a clever solution with lest scripts involved.

migrated from serverfault.com Jan 31 '16 at 15:03

This question came from our site for system and network administrators.

1

The ~/.bashrc file loads when the particular user login to the machine, any process launched by the bashrc file will have the user's session as the parent process. Now unless the user's session terminates the process will not terminate automatically.

I don't think it would be wise to use the tunnelling command in your bashrc file. Instead you can create an alias of the tunnelling command in bashrc file. Invoke the alias in particular terminal, and when you close that terminal the tunnel will stop, because now the parent of that tunnel will be the terminal.

  • Thanks for your support. For my case, I need set the SSH tunnel almost every time I login my server(in order to bypass stupid company firewall). While on another hand, I prefer leaving nothing after my logout. So even alias is shorter, but once I login, I need input the alias, it is really not what I expected. – Qiu Yangfan Jan 15 '16 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.