This guide shows you how to configure custom connection options for your SSH client, letting you define aliases, set specific ports, use custom identity files, and enable connection keep-alives without typing long commands every time. You’ll edit your SSH configuration file to create reusable profiles for different servers.
What You’ll Need
- A computer with an SSH client installed (OpenSSH is standard on Linux, macOS, and Windows 10/11).
- Access to your user’s home directory (to create or edit
~/.ssh/config). - Basic knowledge of a text editor (like nano, vim, or Notepad).
- The hostname or IP address of the server you want to connect to.
Step-by-Step
- Open or create your SSH config file. On Linux/macOS, open a terminal and run:
nano ~/.ssh/config. If the file doesn’t exist, this creates it. On Windows (OpenSSH), the path isC:\Users\YourName\.ssh\config– create the.sshfolder if needed. - Set correct permissions (Linux/macOS only). Run
chmod 600 ~/.ssh/configto ensure only you can read/write the file. SSH will ignore the file if permissions are too open. - Add your first host block. Each server gets a block starting with
Hostfollowed by an alias you choose. For example:Host myserver HostName example.com User myusername Port 2222 IdentityFile ~/.ssh/mykeyThe
Hostline is the short name you’ll type;HostNameis the actual domain/IP;Useris your login;Portoverrides the default 22;IdentityFilepoints to your private key. - Add connection keep-alive options. To prevent idle disconnects, add these lines inside the host block:
ServerAliveInterval 60 ServerAliveCountMax 3
This sends a keep-alive packet every 60 seconds and disconnects after 3 missed responses.
- Use wildcards for groups of servers. If multiple servers share settings, use a pattern like
Host *.example.comorHost prod-*. Settings in a more specific block override wildcards. - Set default options for all hosts. Add a block at the top:
Host *with options likeCompression yesorForwardAgent no. This applies to every connection unless overridden. - Enable SSH agent forwarding (use carefully). Add
ForwardAgent yesif you need to hop from one server to another without re-entering your passphrase. Only enable this for trusted hosts. - Test your configuration. Save the file and run
ssh myserver. If it connects, your config works. If not, check the error messages and your syntax. - Verify with verbose mode. Run
ssh -v myserverto see which config file is being read and which options are applied. This helps debug unexpected behavior.
Common Mistakes
- Wrong file permissions: On Unix systems, if your config or key files are readable by others, SSH will refuse to use them with a warning like “Bad permissions”. Fix with
chmod 600on config and keys. - Using spaces instead of tabs or misaligned indentation: SSH config is whitespace-sensitive but not strict about tabs vs spaces; however, consistent indentation (spaces are fine) is required. A common error is putting options on the same line as
Host. - Forgetting the
Hostline: Every block must start withHost. If you only haveHostName, SSH won’t recognize it as a new block. - Overwriting the default port: If you set
Portin aHost *block, it applies to all hosts, which might break connections to servers that use port 22. Override per host. - Using a relative path for IdentityFile: Relative paths are resolved from the user’s home directory, not the current directory. Use
~/.ssh/keyor an absolute path. - Quoting values that don’t need quotes: Quotes are only needed for values with spaces. Adding them elsewhere can cause parsing errors.
Tips
- Use descriptive aliases: Name hosts by function (e.g.,
web-prod,db-backup) to make commands readable. - Keep backups: Before major edits, copy your config:
cp ~/.ssh/config ~/.ssh/config.bak. - Use
Includeto split configs: If you have many hosts, put them in separate files and addInclude ~/.ssh/config.d/*at the top of your main config. - Leverage
Matchfor dynamic conditions: You can useMatch host example.com user adminto apply options only when both conditions are true. - Test with
-G: Runssh -G myserverto print the effective configuration for that host without connecting. This shows exactly which options will be used.
Troubleshooting
Connection refused or timeout: Check the HostName and Port values. Ensure the server is reachable (ping) and the port is open (use nc -zv host port).
Permission denied (publickey): Verify the IdentityFile path is correct and the key file has permissions 600. Also confirm the public key is installed on the server’s ~/.ssh/authorized_keys.
Config not being read: Run ssh -v and look for the line “Reading configuration data …”. If it doesn’t show your file, you may be using a different path or the file is not named config (case-sensitive on Linux/macOS).
Option not applied: Check for typos in option names (e.g., ServerAliveInterval not ServerAliveInterval). Use ssh -G to see the resolved values.
Bad owner or permissions: SSH prints a clear error. Run chmod 600 ~/.ssh/config and chmod 700 ~/.ssh (the directory should be 700).
Cannot connect after adding wildcard: A wildcard may be overriding a specific host. Move the specific host block after the wildcard, or use a more specific pattern.
Frequently Asked Questions
Can I use the same config file for both work and personal servers?
Yes, just add multiple Host blocks. Use Include to separate them into different files if you prefer.
How do I set a different username for a specific host?
Inside that host’s block, add User yourname. This overrides the default username.
What does ServerAliveInterval do exactly?
It sends a packet to the server every specified number of seconds to keep the connection alive. If the server doesn’t respond after ServerAliveCountMax times, SSH disconnects.
Can I use environment variables in the config file?
No, SSH config does not expand environment variables. Use absolute paths or ~ for home directory.
Why is my config file being ignored?
Common reasons: wrong file location, incorrect permissions, or a syntax error earlier in the file that prevents parsing. Check with ssh -v and ssh -G.
Is it safe to use ForwardAgent?
It’s safe only if you trust the remote host completely, because the remote user can access your local SSH agent. Disable it by default and enable per trusted host.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Secure Boot Battlefield 6 ASUS: Fix & Enable Guide
- Best Tech Websites to Follow for Pros & Beginners
- Second Wind Quest Walkthrough: Where Winds Meet6.00 min/km
- Enshrouded Weapon Upgrade Guide: How to Improve Gear5.00 min/km
- Connect Phone to Controller for Cloud Gaming
- Python Search Highlighting: Snippets & Matches