News How to Get Persistent SSH Connections in Linux Using Eternal Terminal

w_barath

Distinguished
Aug 22, 2011
46
17
18,535
You're on the go, on mobile data, or WIFI, or even just want the freedom to let your device suspend to save battery life:

On the remote:
$ sudo apt install screen || sudo yum install screen

On your roving mobile terminal:

$ sudo atp install autossh || sudo yum install autossh

Then

$ autossh -C host -t 'screen -xRR'

This will automatically re-attach a disconnected shell session, leaving you exactly where you were, without HUPping the command you were running. This is great for when you're running system updates, or compiling, or anything else that requires a long connection.

Personally I use it to shell into a cloud host, which I then have shells to all my other servers combined into a single screen session so I can hop between machines with CTRL-A #, and I set the window names to reflect the hosts.

nVMuXzt.png



For another trick - if you want to be able to shell into your laptop and you don't know what it's IP is, set up a TOR hidden SSH service on it. That will allow you to connect no matter what your IP is. It also makes it possible to ssh between two hosts where neither of them know the other's IP. There will be some latency, but you can always connect directly after you've established what the IPs are. Also very handy if your laptop is stolen since your autossh will connect as soon as they connect to the internet, and you can shred your disks or use x11vnc, your laptop's microphone and camera to spy on their activities, get their public IP, and send the cops their way.
 
Last edited:
  • Like
Reactions: Jbar_
Jan 3, 2021
2
0
10
For another trick - if you want to be able to shell into your laptop and you don't know what it's IP is, set up a TOR hidden SSH service on it.

More simple, if you have an ssh access to a machine always connected and reachable from Internet : ask autossh to start and monitor a reverse ssh tunnel.

(I let you search the command line to do so).
 

w_barath

Distinguished
Aug 22, 2011
46
17
18,535
More simple, if you have an ssh access to a machine always connected and reachable from Internet : ask autossh to start and monitor a reverse ssh tunnel.

Not everyone has an ssh box with a static public IP to push reverse connections through.

To add SSH as a TOR hidden service to your linux box is trivial:

Code:
$ sudo apt install tor && sudo systemctl enable tor.service && sudo systemctl start tor.service
$ sudo nano /etc/tor/torrc # ucomment the sample hidden service and SSH port (22) line
$ sudo service tor restart
$ sudo su -c 'cat /var/lib/tor/*/hostname' # to see the newly generated hidden service domain name
$ sudo apt install netcat

then add this to your ~/.ssh/config:

Code:
Host *.onion
  VerifyHostKeyDNS no
  ProxyCommand nc -x localhost:9050 -X 5 %h %p

After that you can ssh to the generated tor address like any other domain name.


Alternatively, on a client machine all you need is to install netcat and TorBrowser and add this line to your ~/.ssh/config:

Code:
Host *.onion
  VerifyHostKeyDNS no
  ProxyCommand nc -x localhost:9150 -X 5 %h %p

That will use the tor client which TorBrowser installs, which will work in countries and with hotspots which block tor.
 

anscarlett

Honorable
Jul 18, 2018
22
6
10,525
Not everyone has an ssh box with a static public IP to push reverse connections through.
I just use a spare raspberry pi and install Duckdns on it

I generally use mosh and tmux, as this provides resilience to bad connectivity issues. I have no permanent broadband connection at home, I use multiple 4g services which generally works fine, but can occasionally drop connections.
 
Last edited: