~/.ssh/config
Save repeatable connection options in the client configuration file.
Before you run
The first block is file content, not a shell command: edit ~/.ssh/config locally. Keep specific Host blocks before broad Host * defaults because the first obtained value normally wins. Protect the file (typically chmod 600 ~/.ssh/config). Keep passwords and private-key contents out of it. ServerAlive settings detect an unresponsive SSH peer, not every application failure. ssh -G prints effective configuration without connecting and may expose private addresses or paths.
Examples
Add a lab alias with port, identity and SSH keepalive settings.
Host lab
HostName server.example.com
User alice
Port 2222
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
ServerAliveInterval 30
ServerAliveCountMax 3Connect using the alias, or inspect its effective configuration.
ssh lab
ssh -G labOptions and examples
Host patternCommonStart a block matching an SSH alias or host pattern. Put specific blocks before general ones: the first obtained value is used.
Host lab
HostName server.example.comHostName / User / PortCommonSet the actual server address, login name, and SSH port for the matching host.
Host lab
HostName server.example.com
User alice
Port 2222IdentityFile / IdentitiesOnlyCommonSelect a private key and, when set to yes, limit offered identities to configured ones.
Host lab
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesProxyJumpCommonConnect through one or more SSH jump hosts.
Host lab
ProxyJump alice@bastion.example.comLocalForwardConfigure a local listener forwarded to a destination reached from the SSH server.
Host lab
LocalForward 127.0.0.1:15432 db.internal.example.com:5432RemoteForwardConfigure a remote listener forwarded to a destination reached from the local computer; server policy applies.
Host lab
RemoteForward 127.0.0.1:18080 127.0.0.1:8080DynamicForwardConfigure a local SOCKS listener. Clients must be configured separately.
Host lab
DynamicForward 127.0.0.1:1080ExitOnForwardFailureFail the SSH connection if requested initial forwarding listeners cannot be established.
Host lab
ExitOnForwardFailure yesServerAliveInterval / ServerAliveCountMaxCommonConfigure SSH-level peer checks and the number of unanswered checks before disconnecting.
Host lab
ServerAliveInterval 30
ServerAliveCountMax 3ConnectTimeoutSet the connection timeout in seconds.
Host lab
ConnectTimeout 10ForwardAgentControl agent forwarding. A compromised remote host can request signatures; enable only for trusted hosts.
Host lab
ForwardAgent noIncludeInclude additional SSH configuration files; order affects which values are selected.
Include ~/.ssh/config.d/*.confAuthoritative references
Reviewed October 27, 2026