How to Set Up SSH Config Files for Easier Server Access
In this article
You can shorten ssh username@203.0.113.55 -p 2222 -i ~/.ssh/my_key.pem to ssh myserver by creating a local configuration file. The SSH client on Linux, macOS, and Windows reads a file named config inside your .ssh directory before every connection. By defining hosts with their usernames, IP addresses, ports, and key files in this text file, you create aliases that act as shortcuts for the full connection command. This method requires no extra software and works with all standard SSH tools.
What You'll Need
- A computer with an SSH client installed (standard on Linux, macOS, and modern Windows 10/11).
- The connection details for your remote server: hostname or IP address, username, and port (if not the default 22).
- An SSH private key file (optional, but recommended for key-based authentication).
- A basic text editor like Nano, Vim, VS Code, or Notepad.
Step-by-Step
- Open your terminal or command prompt. On Windows, open PowerShell or Command Prompt. On macOS, open Terminal from the Utilities folder. On Linux, open your preferred terminal emulator.
- Navigate to the SSH directory. Run the command
cd ~/.ssh. This hidden folder in your home directory stores your SSH keys and configuration. If the command fails because the directory does not exist, create it withmkdir ~/.sshand then navigate into it. - Create or edit the config file. Run
nano config(ornotepad configon Windows if using Command Prompt). This opens a text editor to create or modify the file namedconfig. If the file already exists, you will add new entries to the end. - Add a host entry. Type the configuration for your server using the format below. Replace the details with your own server information. Indentation with spaces is optional but makes the file easier to read.
TheHost myserver HostName 203.0.113.55 User admin Port 2222 IdentityFile ~/.ssh/my_key.pemHostline defines the alias you will type to connect.HostNameis the IP address or domain.Useris your remote username.Portis only needed if your server uses a non-standard SSH port.IdentityFilespecifies the private key for passwordless login. - Save the file. If using Nano, press
Ctrl+OthenEnterto write the file, andCtrl+Xto exit. If using Notepad, save via the File menu. - Set the correct file permissions. The SSH client ignores the config file if it is readable by other users. Run
chmod 600 configto restrict permissions to your user only. On Windows, the filesystem usually handles this, but if you encounter permission errors, right-click the file, select Properties, and ensure only your user account has access. - Test the connection. Type
ssh myserverin your terminal. The SSH client reads the config file, substitutes the full details, and initiates the connection to the specified host with the correct user and key.
Common Mistakes
A frequent error is incorrect indentation. While the SSH config parser is generally forgiving, mixing tabs and spaces can sometimes cause parsing issues on strict systems. Stick to spaces for indentation. Another common mistake is forgetting to set restrictive file permissions. The SSH client prioritizes security and will often refuse to use a config file or a private key that is visible to other users on the system.
Users also often forget to specify the correct IdentityFile path. The path must be the absolute path to your private key (e.g., /home/youruser/.ssh/key.pem), or a path relative to the .ssh directory. If you omit this line, SSH will try the default keys like id_rsa, which may fail if the server expects a specific key.
Tips
You can create wildcards to apply settings to multiple hosts. For example, adding an entry for Host *.mycompany.com with a specific user or key will apply those settings to any server ending in that domain, unless overridden by a more specific entry. This is useful for managing access to many servers in the same organization.
Use the ServerAliveInterval option to keep connections active. Adding ServerAliveInterval 60 to a host entry sends a signal every 60 seconds, preventing the server or a firewall from disconnecting you due to inactivity. This is particularly helpful for laptop users who experience dropped connections after sleep mode.
You can also enable SSH agent forwarding with ForwardAgent yes. This allows you to use your local SSH keys on the remote server to connect to a third server, acting as a jump host, without storing your private keys on the intermediate server.
Troubleshooting
If the connection fails with a "Permission denied" error, first check the file permissions. Run ls -l config and ensure the permissions show -rw------- (read/write for owner only). If they are different, run chmod 600 config again. Also verify that your private key file has similar restricted permissions (typically 600 or 400).
If you receive a "Could not resolve hostname" error, check for typos in the HostName line of your config file. Ensure there are no extra spaces or hidden characters. You can debug the connection process by running ssh -v myserver. The -v flag (verbose) prints detailed debug logs, showing exactly which config lines are being read and where the connection fails.
If SSH ignores your config file entirely, ensure the file is named exactly config with no file extension like .txt or .conf. Windows users saving with Notepad should ensure the file is not saved as config.txt by default. Place the file directly inside the .ssh folder, not in a subfolder.
Frequently Asked Questions
Where is the SSH config file located on Windows?
The file should be located at C:\Users\YourUsername\.ssh\config. If the .ssh folder does not exist, you can create it manually or by running ssh-keygen once.
Can I have multiple hosts in one config file?
Yes, you can list as many hosts as you need. Simply separate them with a blank line. Each host entry starts with the Host keyword and ends when the next Host keyword appears or the file ends.
Does the config file work with SCP and Rsync?
Yes, command-line tools like scp, rsync, and sftp read the SSH config file. You can use the alias defined in the config file instead of the full hostname. For example: scp file.txt myserver:/path/to/destination.
What happens if I specify options on the command line and in the config file?
Command-line options take precedence over the config file. If your config specifies Port 2222 but you run ssh myserver -p 22, the client attempts to connect on port 22.
Is it safe to store passwords in the SSH config file?
No. The SSH config file does not support storing passwords for security reasons. Use the IdentityFile directive to point to an SSH key for passwordless login, or use an SSH agent.
Can I use comments in the config file?
Yes, you can add comments by starting a line with the hash symbol #. The SSH client ignores these lines, which is useful for organizing your file or temporarily disabling a host entry.