The ~/.ssh/config file lets you define a per-server alias with Host and attach HostName (the real address), User (remote account), Port (TCP port, 22 by default) and IdentityFile (private key). With one block in place, “ssh web1” replaces “ssh -i ~/.ssh/id_ed25519 -p 2222 [email protected]”, and those same fields are what any saved mobile connection asks for.
What each core ~/.ssh/config directive controls
OpenSSH takes values from the command line first, then from ~/.ssh/config, and finally from /etc/ssh/ssh_config. Five directives cover nearly every case:
- Host: the alias or pattern you type after
ssh. Any short name works:web1,nas,prod-db. - HostName: the real IP or DNS name. If it is missing, OpenSSH uses the alias itself as the address.
- User: the remote account, so you stop typing
user@on every command. - Port: the TCP port of the SSH server. When omitted, 22 is used.
- IdentityFile: the path to the private key offered to the server. It accepts
~for your home directory.
Every other option, including ProxyJump and IdentitiesOnly, is documented in the ssh_config manual page.
A real ~/.ssh/config block with alias, user and port
This file defines a web server on a non-standard port, a bastion host and a database that is only reachable through the bastion:
# ~/.ssh/config
Host web1
HostName 203.0.113.10
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519_web1
IdentitiesOnly yes
Host bastion
HostName bastion.example.com
User admin
IdentityFile ~/.ssh/id_ed25519
Host db1
HostName 10.0.0.5
User dba
IdentityFile ~/.ssh/id_ed25519
ProxyJump bastion
Host *
ServerAliveInterval 30With this in place, ssh web1 connects to 203.0.113.10 on port 2222 as deploy using its own key. IdentitiesOnly yes means only the listed key is offered, not every key loaded in the agent, which matters when a server drops you after a few failed authentication attempts. ssh db1 logs into bastion first and hops to 10.0.0.5 without you opening two sessions. ServerAliveInterval 30 sends a keepalive message every 30 seconds to every host.
How OpenSSH resolves ~/.ssh/config: the first value wins
For each parameter, OpenSSH keeps the first value it finds and ignores later ones. That is why specific blocks go at the top and Host * goes at the bottom: a Host * with User root placed first would stop every later block from changing the user.
To see which values will apply without actually connecting, use ssh -G:
ssh -G web1 | grep -E '^(hostname|user|port|identityfile) 'The output is the fully evaluated configuration, in lowercase. If the port shows 22 when you expected 2222, an earlier block matches the alias or the Host name has a typo.
~/.ssh/config compared with saved connections on iOS and Android
Every line of a Host block has a direct counterpart in the form of a saved connection in a mobile client. Moving your desktop setup to the phone is a field-by-field copy:
| Setting | Desktop: ~/.ssh/config | Mobile: saved connection on iOS/Android |
|---|---|---|
| Short name | Host web1 | Connection name or label |
| Address | HostName 203.0.113.10 | Server or address field |
| Account | User deploy | Username field |
| Port | Port 2222 | Port field; 22 if left empty |
| Key | IdentityFile pointing to a file | Key chosen from the client's store, not a path |
| Check | ssh -G alias | Test the connection from the app itself |
The key is the main difference. On the desktop, IdentityFile points to a file; on a phone the usual approach is one key per device. In termi1 the key is generated on the phone, stored in the system's secure storage, and the private part never leaves the device: you only copy the public key into the server's authorized_keys. The terminal works without an account; an optional account syncs connections and shortcuts across devices. Tag a connection as production and Production Guard stops the AI from pressing Enter and holds back anything that deletes, stops or changes something before it reaches the prompt.
Permissions and key type that make ~/.ssh/config work
OpenSSH expects ~/.ssh at mode 700, and private keys and authorized_keys at 600; ssh_config(5) also requires the config file not to be writable by other users:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config ~/.ssh/id_ed25519_web1
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_web1Ed25519 has been supported since OpenSSH 6.5 (January 2014), so any reasonably current server accepts it. If an old RSA key suddenly fails, note that since OpenSSH 8.8 (September 2021) the SHA-1 ssh-rsa signature is disabled by default; generating an Ed25519 key is usually quicker than re-enabling algorithms. For FIDO2 security keys there is the ed25519-sk type, available since OpenSSH 8.2.
Using ~/.ssh/config aliases with Mosh, scp and sftp
Mosh starts mosh-server over an initial SSH connection, so mosh web1 reuses the alias, user, port and key from the block. After that the session runs over UDP, by default on ports 60000-61000, which the firewall must allow. Mosh does not do port forwarding or X11 forwarding.
scp and sftp understand aliases too: sftp web1 or scp report.txt web1:/tmp/. Since OpenSSH 9.0 (April 2022), scp uses the SFTP protocol by default.
On the phone, termi1's mobile SSH and Mosh terminal keeps the session alive with Mosh when you switch from wifi to mobile data, uses tmux so whatever you left running is still there, and ships with built-in SFTP. Its keyboard adds Esc, Ctrl, Tab and arrow keys, and the AI types a command into the prompt from a plain sentence without pressing Enter. You can download termi1 for iPhone and point HostName at an address on a private mesh network when the server has no public IP.
Frequently asked questions
Where is the ~/.ssh/config file and do I need to create it?
The ~/.ssh/config file lives in the .ssh directory of your user account, and OpenSSH does not create it for you. Create it with any editor, add your Host blocks and set its mode to 600 so no other user can modify it.
Can one Host block have several IdentityFile lines?
A Host block accepts several IdentityFile lines, and OpenSSH offers those keys to the server in the order they appear. Combined with IdentitiesOnly yes, only the listed keys are tried, not every key currently loaded in the SSH agent.
Why does ssh ignore the User I set in ~/.ssh/config?
OpenSSH ignores a User from ~/.ssh/config when an earlier block matching the same alias already set that value, because the first value found wins. A username typed on the command line also overrides it. Run ssh -G alias to see the effective value.
Does Mosh honour the port and key from ~/.ssh/config?
Mosh honours the port, user and key from ~/.ssh/config because it first opens a normal SSH connection to launch mosh-server. After that the session switches to UDP, by default on ports 60000-61000, which must be open on the server.