Readit News logoReadit News
xk3 · a year ago
If you have systemd, you could do this:

    [Unit]
    Description=look ma, no autossh
    After=network.target
    
    [Service]
    Type=exec
    ExecStart=/usr/bin/ssh -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -Nn -R 7070:localhost:22 pc 'sleep 20m'
    Restart=always
    RestartSec=20
    RuntimeMaxSec=30m
    
    [Install]
    WantedBy=default.target

johnklos · a year ago
This is no better than ssh in a loop, which is trivially done by a shell script - no systemd needed.

However, when you have shitty NAT routers (SonicWall, any AT&T fiber device, for instance), the connections will be timed out or will die and there'll be long periods where you're waiting for the next iteration of the loop, and/or sometimes it'll get stuck and never try again.

autossh deals with this by actually passing traffic and taking action if traffic doesn't move.

oasisaimlessly · a year ago
> autossh deals with this by actually passing traffic and taking action if traffic doesn't move.

The `ServerAliveInterval` option above achieves this.

jauntywundrkind · a year ago
If you read what the person wrote, you'll see a ServerAliveInterval.

If there are ServerAliveIntevalMaxCount (defaults to 3) attempts that fail, the ssh connection will drop. And systemd will restart it.

Today you learned. Nice. I've dropped autossh for years and you can too, even on flaky connections.

8xeh · a year ago
This approach works very well. I've had dozens of extremely remote systems hooked up this way for about 8 years. The only problem I've seen is that occasionally the server ssh process will get stuck, so you have to log in to the server and kill it. It seems to happen when a remote goes offline and reconnects without closing the old connection first.

If I were doing it now, I'd probably use wireguard, probably. This is simpler to set up and works great.

elashri · a year ago
Can't you just add something like ServerAliveCountMaxto help with solving stale connections?

So something like that would solve that

[Unit] Description=look ma, no autossh After=network.target

[Service] Type=exec ExecStart=/usr/bin/ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -Nn -R 7070:localhost:22 pc 'sleep 20m' Restart=always RestartSec=20 RuntimeMaxSec=30m

[Install] WantedBy=default.target

boris · a year ago
> The only problem I've seen is that occasionally the server ssh process will get stuck, so you have to log in to the server and kill it.

You also need ClientAliveInterval on the server side (in addition to ServerAliveInterval on the client). In other words, both the client and the server need to be configured to monitor the connection. With this setup I had no issues with reconnections.

xk3 · a year ago
> ssh process stuck

systemd's RuntimeMaxSec should help in this case but I've never had trouble with sshd personally

To add more context I use the above service to ssh from my phone to my laptop via my desktop PC. The service runs on my laptop and binds port 22 of my laptop to port 7070 of my PC but wiregaurd would probably work similarly

walrus01 · a year ago
This is not very dissimilar from how the RIPE Atlas software probe (debian package) maintains a persistent SSH command/control session to the anchors and RIPE infrastructure. As I recall it installs itself as a systemd service.

https://atlas.ripe.net/docs/howtos/software-probes.html

dietr1ch · a year ago
No passphrase for the key? What about spotty connection? Doesn't WantedBy block startup on this starting properly? (I'm pretty sure I've been soft locked out of my computer when Comcast decides to do Comcast things.
j33zusjuice · a year ago
No. WantedBy will have no impact on startup. Before or after would, but not Wantedby.
botto · a year ago
This is quite clean and tidy
mikrotikker · a year ago
Ok now how to tell if the connection is running in the systemd status output? Cos this will show as active even when the connection is down or trying to reconnect.
denimnerd42 · a year ago
been doing this since 2012... autossh wasn't the solution back then even.

you want ServerAliveCountMax too but default is 3.

rs_rs_rs_rs_rs · a year ago
What is the reason to run 'sleep 20m'?
lloeki · a year ago
exits (and so restarts) every 20min, e.g ensuring there's no hung sshd on the other side for longer than that.

IIRC if there's an active connection on the forwarding thingy, that ssh command won't exit until the forwarded connection is closed, so this won't interrupt an active forwarded connection every 20min.

polalavik · a year ago
I think this is actually superior to autossh. Doesn’t autossh not restart after crash/reboot?
pferde · a year ago
You could run autossh as a systemd service that starts on boot. :-)
LaputanMachine · a year ago
It doesn't by default, but you can set the AUTOSSH_GATETIME environment variable to 0 so that autossh retries even if the first connection attempt fails.
ChoHag · a year ago
That's so much better than bourne/bash, which requires this monstrous wart of a code blob:

autossh() {

        # Tiny delay after failure in case of connection errors

        while ! ssh "$@"; do echo Restarting ssh "$@"...; sleep 1; done

}

Dead Comment

beagle3 · a year ago
14 years ago, i was using auto ash to keep SSH tunnels up; but at some point (quite far back - perhaps 2016?) ssh gained everything needed to do this internally except the restart.

At this point I configure all of the keep alive and retry options in ssh_config and sshd_config, and use

    While true; do ssh user@host ; sleep 10; done
To get the same effect, but with much more flexibility - e.g. alternating connection addresses on a multihomed host, add logging, run from daemontools or systemd unit instead of a loop and let them track the process and restart, etc.

vincentpants · a year ago
Curious what advantages this has over mosh?

https://mosh.org/

mjw1007 · a year ago
mosh is for interactive sessions, to keep them working when the connection is flaky.

autossh is for keeping unattended ssh tunnels alive, if the connection is flaky or one end is only intermittently available. So for using tunnels for the sort of thing you might otherwise use a VPN for.

st380752143 · a year ago
AIK, for using mosh, you need to install mosh on target host as well. Seems like autossh doesn't need this step.
leni536 · a year ago
I have used autossh + tmux before to enable X forwarding (just for clipboard sharing). Couldn't do that in mosh.
suninsight · a year ago
Biggest issue i have with mosh is that it you cant scroll up the history. It is kind of a deal breaker for me.
lloeki · a year ago
hence why most throw in tmux/screen on the other end, possibly automatically so:

https://github.com/blinksh/blink/discussions/1526

Deleted Comment

cperciva · a year ago
If your concern is to have secure tunnels between hosts, you should probably use spiped rather than SSH, since it uses a separate TCP connection for each pipe -- this avoids the "connection dropped" problem and also the "multiplexing many connections over one TCP connection" performance hit.

Also, spiped is way simpler and more secure than SSH. (On my servers, I tunnel SSH over spiped, to protect the sshd from attacks.)

eichin · a year ago
paulfharrison · a year ago
For web-servers on remote machines, I have found this useful:

  socat TCP4-LISTEN:1234,fork,bind=127.0.0.1 EXEC:'ssh my.remote.server nc 127.0.0.1 1234'
1234 = local/remote port. Can be adapted to use unix sockets at the remote end. my.remote.server = your remote server address.

This will set up a tunnel only when needed, and seems to play nicely with my browser.

botto · a year ago
I've used autossh to have a reverse tunnel open connection back to my work desktop, IT never found it and I had that in place for year
hi-v-rocknroll · a year ago
The last time I used autossh it was on a client site to keep 2 layers of ssh tunnels open to jump through their network isolation hoops.

In general, when flexibility is possible, such a use-case nowadays would often be better served by deploying WireGuard. Grouchy, out-of-touch corporate net admins probably don't even know what it is and insist on their antiquated Cisco VPNs.