SSH config file: ~/.ssh/config, Host blocks and examples
Set up ~/.ssh/config so ssh agent-box replaces the long command: Host blocks, IdentitiesOnly, keepalives, LocalForward, ProxyJump, and why ForwardAgent stays off.
The SSH config file is ~/.ssh/config on macOS and Linux and %USERPROFILE%\.ssh\config on Windows. Create it with permissions 600, then add a block per server: Host agent-box followed by indented HostName, User, Port and IdentityFile lines. After that, ssh agent-box replaces the long command, and scp, rsync, git, mosh and VS Code’s Remote - SSH all use the same name. For each setting, the first value that matches wins, so put specific hosts first and Host * last, and check the result with ssh -G agent-box. Leave ForwardAgent off for any server where a coding agent runs as you.
Where the file is, and the permissions it needs
OpenSSH reads settings from three places in a fixed order: options on the command line, your own ~/.ssh/config, then the system-wide /etc/ssh/ssh_config. The file does not exist until you make it. On macOS or Linux:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/configCopiedThe ssh_config manual says the file must be “read/write for the user, and not writable by others.” The client’s source checks exactly that: a file owned by someone other than you or root, or writable by group or others, stops every connection with Bad owner or permissions on /home/you/.ssh/config. chmod 600 is the usual fix.
On Windows, Microsoft’s OpenSSH documentation gives the same order with Windows paths: a file passed with -F, then %userprofile%\.ssh\config, then %programdata%\ssh\ssh_config, and it points at OpenSSH’s own manual for the keywords. This creates the file only if it is missing, then opens it in Notepad:
if (-not (Test-Path $env:USERPROFILE\.ssh\config)) { New-Item -ItemType File -Force $env:USERPROFILE\.ssh\config }
notepad $env:USERPROFILE\.ssh\configCopiedThe name is exactly config, with no extension: a file saved as config.txt is not on that list, and ssh never reads it.
A block for one server
A Host line opens a block, and everything under it applies up to the next Host or Match line. The name after Host is whatever you want to type; HostName is the real address:
Host agent-box
HostName 203.0.113.10
User you
Port 22
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesCopiedThe indentation is for you; ssh ignores it. Keywords are not case-sensitive and their values are, and a keyword and its value can be separated by spaces or one =. Now each of these uses the block:
ssh agent-box
scp notes.md agent-box:
rsync -av ./project/ agent-box:project/
mosh agent-boxCopiedThat works because none of them speaks SSH on its own. scp runs over an ssh connection and passes its -F straight to ssh; rsync “normally uses ssh for its communications”; mosh logs in through ssh first, and its site says a host alias with a Port line is respected. Git’s own manual says “Usually it is easier to configure any desired options through your personal .ssh/config file.” In VS Code, Remote-SSH: Add New SSH Host writes a block like this one into a config file you pick, the host appears under SSH Targets in the Remote Explorer, and the remote.SSH.configFile setting points it at a different file, per its documentation.
The keywords worth knowing
| Keyword | Default | What it is for |
|---|---|---|
HostName | The name you typed | The real address or DNS name, or a Tailscale name |
User | Your local username | The account on the server |
Port | 22 | A server that listens elsewhere |
IdentityFile | ~/.ssh/id_rsa, id_ecdsa, id_ed25519 and their security-key and post-quantum variants | Which private key to offer; may be given more than once |
IdentitiesOnly | no | Offer only the configured keys, even if your agent holds more |
ForwardAgent | no | Lets the server use your laptop’s agent; keep it off (below) |
LocalForward | None | Brings a port on the server to one on your laptop |
ServerAliveInterval | 0 (off) | Seconds of silence before ssh asks the server whether it is still there |
ServerAliveCountMax | 3 | Unanswered asks before ssh gives up |
ControlMaster, ControlPath, ControlPersist | Off | One connection shared by every later ssh, scp and git to the same host |
ProxyJump | None | Reach a server through another one |
Include | None | Read more files into this one |
IdentitiesOnly yes earns its place as soon as ssh-agent holds a few keys. Without it, ssh offers the agent’s keys as well as the file, one after another. On a server older than OpenSSH 10.6, each key it refuses counts toward its MaxAuthTries limit, six by default, so the connection can close before the right key is offered. Servers on 10.6 or later let six refused offers through before they start counting, which moves the limit rather than removing it. Making the key itself, and putting its public half on the server, is in the ssh-keygen guide.
The first value wins, so Host * goes last
This is the rule that surprises people. ssh does not pick the best-matching block: it reads the file from the top and, for each setting, keeps the first value it finds in any block that matches. The manual’s advice follows from it: “more host-specific declarations should be given near the beginning of the file, and general defaults at the end.” A Host * block at the top with User root in it sets the user for every host below, whatever their own blocks say. The same rule makes the command line win over the file, since options given there are read first.
A whole file in that order, for a server you work on, a forward to a web UI on it, and a second machine reached through the first:
Include config.d/*
Host agent-box
HostName 203.0.113.10
User you
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host agent-box-ui
HostName 203.0.113.10
User you
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
LocalForward 3000 localhost:3000
ExitOnForwardFailure yes
Host gpu-box
HostName 10.0.0.5
User you
ProxyJump agent-box
Host *
ForwardAgent no
ServerAliveInterval 30
ServerAliveCountMax 3
ControlMaster auto
ControlPath ~/.ssh/cm-%C
ControlPersist 10mCopiedWhen a setting does not behave, ask ssh what it ended up with. ssh -G prints the configuration after evaluating every Host and Match block, and exits without connecting:
ssh -G agent-box | grep -iE '^(hostname|user|port|identityfile|identitiesonly|forwardagent) 'Copiedssh -v agent-box goes one step further and shows which files were read and which keys were offered. A misspelled keyword stops ssh with Bad configuration option and the line number.
Keep agent forwarding off on an agent’s server
ForwardAgent yes (or ssh -A) lets programs on the server ask the ssh-agent on your laptop to sign with your keys, which is how people push to GitHub from a server without putting a key there. The manual says to enable it “with caution”: anyone on the server who can get past the permissions on the forwarded socket “can access the local agent through the forwarded connection”. They cannot copy the keys out, but they can use them to log in wherever those keys work, for as long as you stay connected.
The manual does not mention coding agents, but the step is short. The socket belongs to your user on the server, so Claude Code, Codex or any agent running as that user does not need to get past anything. While your session is open, the agent, and any instruction planted in a README or an issue it reads, can sign in as you with every key your laptop’s agent holds. The server will not stop it either: sshd_config’s AllowAgentForwarding defaults to yes, so your config file is the switch that counts.
The default is already no; writing ForwardAgent no in Host * at the end, as in the file above, keeps a later copy-pasted block from turning it on by accident for everything. For the two things forwarding is usually wanted for, there are narrower routes. Reaching a second server is ProxyJump (below), which never exposes your agent on the middle machine. Letting the agent on the server push to GitHub is a key or a token made for it there, scoped to its repositories, which GitHub access for a coding agent’s server sets up, host aliases included.
Long agent sessions: keepalives
With ServerAliveInterval at its default of 0, ssh never checks on a quiet connection, so a dropped network can leave a terminal frozen with no error. Set an interval and ssh sends a message “through the encrypted channel to request a response from the server” after that many seconds of silence, and disconnects after ServerAliveCountMax unanswered ones. The manual’s example is 15 seconds with the default count of 3, which disconnects after about 45 seconds; the 30 above gives about 90.
Keepalives make a dead connection end promptly; they do not keep the work alive. A coding agent started in a plain ssh session still dies with it. Run the agent inside tmux on the server so it outlives the connection, and use mosh if your network changes under you, which reconnects where ssh would have to start again.
A web UI on the server’s localhost
An agent that starts a dev server or a dashboard on the server’s localhost:3000 can be opened on your laptop with a forward. LocalForward 3000 localhost:3000 means: listen on port 3000 here, and send each connection to localhost:3000 as the server sees it. ssh binds that listening port to your laptop’s loopback address by default, so nothing else on your network can use it.
Keep it in its own block, as agent-box-ui above, rather than in the main one: a forward in the main block tries to take port 3000 on every login, so a second terminal, an scp or a git command to the same server competes for a port the first one already holds. Open it with ssh -N agent-box-ui, where -N runs no command, and visit http://localhost:3000. ExitOnForwardFailure yes makes ssh quit when the port cannot be bound, rather than connecting without the forward. Other routes, for a phone or for a team, are in opening a localhost app on a remote server.
One connection, reused
With the three Control lines from the file above, the first ssh agent-box becomes a master connection, and every later ssh, scp, rsync or git to the same host goes through it: no new handshake and no second passphrase prompt, which makes repeated short commands feel instant. ControlMaster auto uses a master if one exists and starts one if not. ControlPath is the socket file; the manual recommends it include at least %h, %p and %r, and %C is a hash of those and more in one short name. ControlPersist 10m keeps the master running for ten idle minutes after the last session closes; without it, closing the first session closes the master.
ssh -O check agent-box
ssh -O exit agent-boxCopiedThe first command shows whether a master is running, the second closes it, which is what to do after changing the server’s settings or your keys, since a reused connection does not log in again. OpenSSH 10.3 added ssh -O conninfo agent-box to show the master’s details. Since 10.0, scp and sftp use an existing master but no longer start one themselves. On Windows, the built-in client’s support for ControlMaster is an open issue as of December 2026, so leave these three lines out of a Windows config.
Through another server: ProxyJump
When a machine is only reachable from inside a network, such as a GPU box behind a server with a public address, ProxyJump agent-box in its block makes ssh connect to agent-box first and carry the connection through it. Your key is used from your laptop for both hops, and nothing is forwarded to the middle machine. Several hops go in one line, separated by commas, and are visited in order. On the command line the same is ssh -J agent-box you@10.0.0.5, but the ssh manual notes that command-line options apply to the destination, not the jump host: settings for the jump host belong in the config file, which is one more reason to write it there.
Include, Match, and retiring a host
Include reads other files, with globs, in alphabetical order; a relative path is taken from ~/.ssh. Put the line at the top of the file: inside a block it only applies when that block matches, and at the end it would come after Host *, so the first-value rule would let your defaults win over everything it brings in. A config.d folder with a file per project or per client keeps a long file manageable.
Match opens a block on conditions that Host cannot express: the user, the local user, a command’s exit status (exec), whether you are on a given local network (localnetwork), and since OpenSSH 10.0 the version of ssh itself and the kind of session, a shell or a single command. Match all matches everything, like Host *. A Match exec runs its command on every connection, so keep it quick.
OpenSSH 10.1 added RefuseConnection, which stops ssh with your message when a block matches. It suits an alias for a server you have deleted, so a script or a habit that still uses the name fails with a reason instead of hanging on an address someone else may now hold:
Host old-agent-box
RefuseConnection "retired in November, use agent-box"CopiedCheck which OpenSSH you have with ssh -V before relying on either: the current release is OpenSSH 10.6p2, from November 5, 2026, and the clients that ship with operating systems are often a few versions behind. A keyword a client does not know stops it with Bad configuration option.