How To Ssh In Linux

How To Ssh In Linux

Reading time1 min
#Linux#SSH#Security#SSHconfig#LinuxSysadmin#DevOps

Mastering Secure and Efficient SSH Access in Linux: Beyond the Basics

SSH remains essential across modern Linux environments. Default SSH configurations offer the minimum—strong enough for local VMs, but easily outpaced by real-world infrastructure requirements. The following are hard-earned methods to lock down SSH, streamline repetitive tasks, and avoid longstanding pitfalls in enterprise contexts.


Weak Defaults: Why Operators Harden SSH

Attack surface expands quickly in any Linux fleet. Password authentication (“PasswordAuthentication yes”), direct root login, and lack of host restrictions expose even patched hosts to trivial attacks. Misconfigured jump hosts and agent forwarding shorten time-to-compromise for lateral movement. When an automation pipeline grinds to a halt due to “Permission denied (publickey),” incomplete credential management is usually to blame.


1. Enforce Key Authentication and Restrict Access

Generate Ed25519 keys (as of OpenSSH 7.6+, Ed25519 is preferred for both performance and cryptographic strength):

ssh-keygen -t ed25519 -C "ops-team+2024@company.tld"

Legacy deployments might still require -t rsa -b 4096—verify OpenSSH support on the remote endpoint:

ssh -V
# Example output:
# OpenSSH_8.4p1 Ubuntu-5ubuntu1.4, OpenSSL 1.1.1f

Deploy public keys via ssh-copy-id if password authentication is temporarily allowed:

ssh-copy-id -i ~/.ssh/id_ed25519 user@host

For systems without ssh-copy-id:

cat ~/.ssh/id_ed25519.pub | ssh user@host \
  "install -d -m 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Harden /etc/ssh/sshd_config:

PasswordAuthentication no
PermitRootLogin prohibit-password
ChallengeResponseAuthentication no
UsePAM yes
AllowUsers admin ops

Note: UsePAM yes enables session modules (tty restrictions, 2FA), but audit what PAM modules you actually require.

Don’t forget to restart the service:

sudo systemctl restart sshd
# Confirm with:
sudo systemctl status sshd

2. Multiplex Connections to Eliminate Handshake Latency

Repeated SSH authentications are a workflow killer, especially with agent-based keys or hardware tokens.

Persistent sessions with ControlMaster:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/sock/%r@%h:%p
    ControlPersist 10m

Create the socket directory if missing:

mkdir -p ~/.ssh/sock && chmod 700 ~/.ssh/sock

Trade-offs:
If ControlPersist is too long, stale sessions may accumulate; too short, benefits are lost. Monitor with ls -l ~/.ssh/sock.

Side note: Some ephemeral CI runners don’t clean ControlPath sockets after pipeline teardown—explicitly remove them in post steps.


3. Seamless Multi-Hop Access with ProxyJump

Multi-level SSH tunnels are common, yet chaining manual hops (ssh -t) is error-prone.

Use ProxyJump (OpenSSH ≥7.3):

Host app-internal
    HostName 10.132.0.52
    User deploy
    ProxyJump admin@bastion.company.tld
ssh app-internal

Legacy tip: For OpenSSH <7.3, ProxyCommand with nc (Netcat) achieves similar results, but with edge-case breakages under connection loss.


4. Managing Agents and Persistent Keys: Keychain Approach

Typing a passphrase on every session multiplies support tickets and reduces adoption of strong keys.

Standard agent initialization (most Linux DEs):

ssh-add ~/.ssh/id_ed25519

For long-lived shell or tmux sessions, use Keychain (keychain v2.8.5+, https://www.funtoo.org/Keychain):

sudo apt install keychain
echo 'eval $(keychain --eval id_ed25519)' >> ~/.bashrc

Note: Some distribution-curated Keychain packages are outdated. Cross-check your distro repo; manual install may be required for bugfixes (e.g., GPG agent forwarding in WSL2 shells).


5. Streamlined Host Aliasing & Utility Functions

Real-world example: Managing twelve Kubernetes nodes behind separate bastions. Avoid copy-paste errors:

Host k8s-node-*.corp
    User sysadmin
    HostName %h.corp.internal
    ProxyJump bastion@bastion.corp.net
    IdentityFile ~/.ssh/cluster_ed25519

Now:

ssh k8s-node-03.corp

Tip: Parameterized HostName values (%h) and globbing can collapse massive configs.

Routine scripting: A simple bash function to copy authorized_keys across a fleet:

update_ssh_keys() {
    for host in "$@"; do
        scp ~/.ssh/id_ed25519.pub "$host:~/.ssh/authorized_keys.tmp"
        ssh "$host" "sort -u ~/.ssh/authorized_keys.tmp >> ~/.ssh/authorized_keys && rm ~/.ssh/authorized_keys.tmp"
    done
}
update_ssh_keys node01 node02 node03

Known issue: sort -u can damage ordering-dependent file entries—grep for old keys if you automate pruning.


6. Surviving Flaky Networks: mosh

SSH stalls or disconnects on network changes, e.g., WiFi hopping during incident response.

Use mosh (v1.4.0+):

sudo apt install mosh
mosh admin@vpn-host

Caveats:

  • Not all enterprise firewalls permit Datagram (UDP) traffic needed by mosh.
  • No native support for SSH agent forwarding or port tunnels.

Yet, in multi-hour sessions with mobile endpoints, mosh is unmatched for resilience.


Summary Table: High-Impact SSH Practices

MethodSecurity ImpactOps ImpactNotes
Key-only authenticationCriticalMinor inconvenienceDisable password logins everywhere
MultiplexingModerateMajor performanceClose unused master connections
ProxyJumpMajorSimplifies workflowRequires OpenSSH 7.3+
Keychain/ssh-agentModerateSaves timeWatch out for old versions in distros
Host aliases/scriptsModerateEliminates errorsKeep config under source control
moshLowMajor reliabilityNo agent forwarding, uses UDP

Non-Obvious: Per-Host Log Verbosity

For troubleshooting:

Host noisyhost
    HostName legacy-db.prod.lan
    LogLevel DEBUG3

Later, redirect connection logs to a file:

ssh noisyhost |& tee ssh-noisyhost-debug.log

Valuable for dissecting config order-of-operations or odd failures with agent/forwarding.


Checklist:

  • Remove all forms of password authentication across servers.
  • Use ControlMaster with reasonable persist times.
  • Explicitly configure ProxyJump for each internal tier.
  • Employ Keychain or similar tools for agent lifecycle.
  • Store ~/.ssh/config under version control (private, with restrictive access).
  • Review authorized_keys files monthly—prune obsolete entries.

If you’ve identified SSH reliability issues not covered above—especially in hybrid cloud deployments—document and automate the remedy. Edge-cases appear fast.

Further discussion open for robust SSH session management in containerized or short-lived environments.