SSH Workflow Optimization Guide
This guide outlines the steps to streamline the process of connecting to virtual machines via SSH. By implementing these changes, you will eliminate the need to memorize IP addresses and type passwords for every connection.
Prerequisites
- A local machine with a terminal (macOS, Linux, or Windows WSL/PowerShell).
- Access to the remote server (username and current password).
1. Password-less Authentication (SSH Keys)
Using SSH keys is more secure than passwords and allows for instant login.
Step 1: Generate a Key Pair
Check if you already have a key pair. Run this in your local terminal:
ls ~/.ssh/id_rsa.pub
If the file does not exist, generate a new one (press Enter through all prompts to accept defaults):
ssh-keygen -t rsa -b 4096
Step 2: Copy Public Key to Server
Use ssh-copy-id to transfer your public key to the remote VM. Replace the placeholders with your actual info.
ssh-copy-id user@your-server-ip
You will be asked for your password one last time.
Verification: Try logging in with
ssh user@your-server-ip. You should not be prompted for a password.
2. Shortening Hostnames (SSH Config)
The SSH config file allows you to map complex server details to a short nickname. This works for SSH, SCP, Rsync, and VS Code Remote.
- Open or create your config file:
nano ~/.ssh/config -
Add the following configuration block:
Host myvm HostName 192.168.1.55 User remote_username # IdentityFile ~/.ssh/id_rsa <-- Optional: Only needed if using a custom key pathParameter Description Host The short name you want to type (e.g., dev,db-server).HostName The actual IP address or domain name. User Your username on the remote server. - Save the file (
Ctrl+O,Enter) and exit (Ctrl+X).
Usage: You can now connect using:
ssh myvm
3. Creating Shell Aliases (Optional)
For the fastest possible access, you can alias the ssh command in your shell profile.
- Open your shell configuration file (
~/.bashrcor~/.zshrc). - Add the following line to the bottom of the file:
alias vm="ssh myvm" - Reload your configuration:
source ~/.bashrc # or ~/.zshrc
Usage: Type
vmand press Enter to connect instantly.
Appendix: Connection Stability
If your SSH sessions tend to freeze or drop after periods of inactivity, add the following “Heartbeat” configuration to the top of your ~/.ssh/config file:
Host *
ServerAliveInterval 60
ServerAliveCountMax 2