SSH can do much more than open terminals

Summary

On Hackaday, Al Williams collects SSH tricks that go beyond the plain terminal. The focus is on host entries in ~/.ssh/config, shared connections with ControlMaster and remote commands whose output is processed locally. A speed test with pv and a pointer to sshfs complete the list.

Configuration instead of long command lines

A host entry in ~/.ssh/config bundles host name, port, user, key and options under a short name; afterwards ssh homeserver is enough. ControlMaster auto with a ControlPath lets several sessions to the same target share one TCP connection, which speeds up opening further sessions. ControlPersist keeps the master connection in the background, with a number such as 180 only for three minutes of idleness.

BatchMode prevents any prompts and suits scripts. SendEnv transfers environment variables such as LS_COLORS if the server accepts them via AcceptEnv.

Commands from afar

Williams flashes his 3D printer's firmware with a script that uses ssh to stop the print server on a Raspberry Pi, copies the file with scp, starts the flashing and restarts the service. In the same way, a capture on a remote machine can be fed straight into a local Wireshark: ssh root@server 'tcpdump -c 1000 -nn -w - not port 22' | wireshark -k -i -.

The speed of a connection is measured with yes | pv | ssh remote_host "cat >/dev/null". sshfs mounts a remote directory via FUSE without needing more than the SSH service on the server.

Ideas

  • Host entries replace long and error-prone command lines.
  • ControlMaster shares one TCP connection between several sessions.
  • ControlPersist keeps the master connection in the background for a set time.
  • BatchMode makes scripts fail instead of waiting for input.
  • Remote commands deliver their output straight into local pipelines.
  • tcpdump on the server and Wireshark locally complement each other over ssh.

Insights

  • SSH is a toolkit for automation, not just a terminal.
  • Shared connections save time but also concentrate load and failures on one link.

Facts

  • ssh -S none bypasses a shared connection in individual cases.
  • SendEnv only works if the server allows the variable via AcceptEnv.
  • In OpenSSH, TCPKeepAlive defaults to yes.
  • The %C token in ControlPath stands for a hash of local host, target, port, user and ProxyJump.

References

Critique

  • Williams presents TCPKeepAlive as an option you first have to switch on; according to the manual it is already the default. Against drops caused by NAT timeouts, ServerAliveInterval helps more, because it checks inside the encrypted connection.

Remarks

  • The article only announces port forwarding, the SOCKS proxy and tunnels; Williams promises further Linux Fu posts for them.

Recommendations

  • Create ControlPath with %C, for example ~/.ssh/cm-%C, instead of deleting orphaned files via rc.local.
  • Set ServerAliveInterval for connections over NAT and mobile networks.
  • Use BatchMode yes in scripts so they fail immediately when login is missing.

Read the original article

Search the Web Archive

Found a mistake or have something to add? Get in touch.