How to Get Persistent SSH Connections in Linux Using Eternal Terminal

Persistent SSH Connections
(Image credit: Tom's Hardware)

An SSH connection is typically used to connect with remote machines. It works on a client /

server model, in that the machine being used to connect to a remote machine is called a client, while the latter is the server. To establish the connection with the remote machine, you must know its IP address, as well as the username/password to authenticate the connection. 

Latest Videos FromTom's Hardware
Shashank Sharma
Freelance Writer

Shashank Sharma is a freelance writer for Tom’s Hardware US, where he writes about his triumphs and joys of working with the Linux CLI.

  • w_barath
    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.



    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.
    Reply
  • Jbar_
    You may look also at autossh, which do the same job, since very long time.
    https://linux.die.net/man/1/autossh
    Reply
  • Jbar_
    w_barath said:
    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).
    Reply
  • w_barath
    Jbar_ said:
    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:

    $ 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:

    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:

    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.
    Reply
  • anscarlett
    w_barath said:
    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.
    Reply