DEV Community

Cover image for Optimizing SSH Connections
Sibelius Seraphini for Woovi

Posted on

8

Optimizing SSH Connections

As we move to remote development, we need to optimize SSH connections.
SSH (Secure Shell) is an essential tool for securely managing remote systems. However, its performance and usability can be significantly improved with a few tweaks. Here's how:

Using an SSH Config File

The SSH config file (~/.ssh/config) allows you to simplify and optimize your SSH connections. Instead of repeatedly typing lengthy commands, you can define settings for each host.

Host myserver
  HostName 192.168.1.100
  User myuser
  Port 22
  IdentityFile ~/.ssh/id_rsa
  Compression yes
  ServerAliveInterval 60
  ServerAliveCountMax 3
  LogLevel QUIET
Enter fullscreen mode Exit fullscreen mode

Benefits:
Ease of Use: Simplify commands with aliases (e.g., ssh myserver).
Performance: Enable compression for faster transfers, especially over slow connections.
Stability: Avoid disconnections with ServerAliveInterval and ServerAliveCountMax

SSH Multiplexing

Multiplexing reuses existing SSH connections for multiple sessions, reducing authentication overhead and improving speed for subsequent logins.

Host *
  ControlMaster auto
  ControlPath ~/.ssh/cm-%r@%h:%p
  ControlPersist 10m
Enter fullscreen mode Exit fullscreen mode

Explanation:
ControlMaster auto: Enables multiplexing.
ControlPath: Defines a socket file for reusing connections.
ControlPersist 10m: Keeps the connection open for 10 minutes after the session ends.

Automating LocalForward and RemoteForward

Port forwarding is a powerful SSH feature for securely tunneling traffic. Automating it in the config file ensures consistency and saves time.

LocalForward:
Use LocalForward to forward a local port to a remote service:

Host myserver
  LocalForward 8080 127.0.0.1:80
Enter fullscreen mode Exit fullscreen mode

Access a remote web server running on port 80 via localhost:8080.
RemoteForward:
Use RemoteForward to expose a local service to the remote host:

Host myserver
  RemoteForward 9090 127.0.0.1:3306
Enter fullscreen mode Exit fullscreen mode

Expose a local MySQL instance running on port 3306 to the remote host's port 9090.

Using AutoSSH for Persistent Connections

AutoSSH is a utility that automatically restarts SSH sessions if they get disconnected, ensuring a persistent, fault-tolerant connection. It’s particularly useful for remote systems that require continuous monitoring or tunneling, like when you are maintaining a VPN-like setup using SSH tunnels or monitoring remote systems.

autossh -M 0 -f -N -T -L 8080:localhost:80 myserver
Enter fullscreen mode Exit fullscreen mode

with SSH config

Host myserver
  HostName 192.168.1.100
  User myuser
  IdentityFile ~/.ssh/id_rsa
  LocalForward 8080 127.0.0.1:80
  RemoteForward 9090 127.0.0.1:3306
  ControlMaster auto
  ControlPath ~/.ssh/cm-%r@%h:%p
  ControlPersist 10m
  ProxyCommand autossh -M 0 -f -N -T -L 8080:localhost:80 %h
Enter fullscreen mode Exit fullscreen mode

Conclusion

By leveraging SSH configuration files, enabling multiplexing, and automating forwarding options, you can significantly enhance your SSH workflow. These tweaks reduce repetitive tasks, improve connection speed, and make remote management seamless.

Optimize your setup today and enjoy faster, more efficient SSH connections!


Woovi
Woovi is a fintech platform revolutionizing how businesses and developers handle payments in Brazil. Built with a developer-first mindset, Woovi simplifies integration with instant payment methods like Pix, enabling companies to receive payments seamlessly and automate financial workflows.

If you want to work with us, we are hiring!

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay