8 ms·
If you have systemd, you could do this: [Unit] Description=look ma, no autossh After=network.target [Service] Type=exec ExecStart=
by xk3 2y 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
- nine_k 2y ago[flagged]
- sevg 2y agoDo we really still have to turn every conversation into systemd friction?
- redundantly 2y ago[flagged]
- nine_k 2y agoNo. Some people use ssh while not running Linux, and not by running something exotic; macOS is widely popular.
- ChoHag 2y agoThat'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 }
- botto 2y agoThis is quite clean and tidy
- denimnerd42 2y agobeen doing this since 2012... autossh wasn't the solution back then even. you want ServerAliveCountMax too but default is 3.
- 8xeh 2y agoThis 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 2y agoCan'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
- xk3 2y agoThe default of ServerAliveCountMax is already 3
- xk3 2y 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
- lloeki 2y agoRuntimeMaxSec would have systemd kill a live forwarded connection though?
- xk3 2y agoclosing ssh doesn't close the ports if they are being used, at least with ControlMaster. You need to run something like this to force the ssh daemon to close the port ssh -O cancel -L 4102:localhost:4000 pc but if ControlMaster is stuck maybe autossh is better in that case, or use this: Host * ServerAliveInterval 11
- dietr1ch 2y agoNo 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 2y agoNo. WantedBy will have no impact on startup. Before or after would, but not Wantedby.
- polalavik 2y agoI think this is actually superior to autossh. Doesn’t autossh not restart after crash/reboot?
- LaputanMachine 2y agoIt 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.
- pferde 2y agoYou could run autossh as a systemd service that starts on boot. :-)
- svnt 2y agoI think you meant this as a joke, but this is what we landed on about a decade ago and it was the most reliable setup we found.
- johnklos 2y agoThis 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.
- jauntywundrkind 2y agoIf 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.
- johnklos 2y agoToday I learned that some people make mistakes, but I already knew that ;) ServerAliveInterval doesn't do this properly and consistently. I've used my own autossh type script for two decades now. It's mostly used to give access to machines behind shitty NAT, and/or that have addresses that constantly change, and/or for systems on CGNAT, like Starlink. If ServerAliveInterval works so well and negates the need for something like autossh to exist, then why have sessions created by my script, which has ServerAliveInterval (and ServerAliveIntevalMaxCount) gotten hung up where the script needs to kill the old and create a new ssh connection now and then? My script logs each timeout, each session hang, and each new connection, and depending on the network, it can happen often. Please read the bit where it's explained how autossh sends test data back and forth. Do you think you just magically and cleverly discovered ServerAliveIntevalMaxCount and that the autossh people have no idea that it exists? Or perhaps they know it exists, they know it's not perfect, and they used another mechanism to make up for the shortcomings of what ssh offers out of the box?
- reycharles 2y agoThe README has this text: > For example, if you are using a recent version of OpenSSH, you may wish to explore using the ServerAliveInterval and ServerAliveCountMax options to have the SSH client exit if it finds itself no longer connected to the server. In many ways this may be a better solution than the monitoring port.
- walrus01 2y agoThis 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 https://atlas.ripe.net/docs/howtos/software-probes.html
- rs_rs_rs_rs_rs 2y agoWhat is the reason to run 'sleep 20m'?
- lloeki 2y agoexits (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.
- mikrotikker 2y agoOk 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.