Description
The SSH config file allows each user to define specific SSH options for connecting to multiple different remote hosts.
Instructions
Files
~/.ssh/config
This is the per-user configuration file. This file is used by the SSH client. Because of the potential for abuse, this file must have strict permissions: read/write for the user, and not writable by others.
/etc/ssh/ssh_config
Systemwide configuration file. This file provides defaults for those values that are not specified in the user’s configuration file, and for those users who do not have a configuration file. This file must be world-readable.
If these files don’t exist, you may need to create them first.
If the config file is new, you might need to set the following permissions:
1 | $ chmod 600 ~/.ssh/config |
This ensures that ONLY your user has access to edit this file.
Example 1: Basic Usage
The following example can be used to setup pre-defined connections to hosts on your local network.
~/.ssh/config
1 | Host my-ssh-host |
Sample connection:
1 | $ ssh my-ssh-host |
Example 2: Connecting to GitHub.com
The following example allows you to connect to multiple github.com accounts using seperate keys for each connection. Since GitHub.com does not allow the use of the same SSH key accross multiple accounts.
1 | Host podcast |
In the above example three host entries are defined; podcast
, example.com
, and radio
.
The HostName
for each Host
entry points to github.com
A unique IdentityFile
is specified for each Host
entry.
Verify that the permissions of each IdentityFile are 400.
1 | $ chmod 400 ~/.ssh/id_rsa_github |
This verifies that the file is read-only and accessible only by your user.
The following example shows how to reference each git repo based on the above config file:
1 | $ git clone git@{HOST}:{ORG_NAME}/{REPO_NAME}.git |
1 | git@podcast:username/podcast.git |
Sources
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
https://dev.to/arnellebalane/setting-up-multiple-github-accounts-the-nicer-way-1m5m#:~:text=GitHub does not allow us,~%2F. ↩︎
https://futurestud.io/tutorials/simplify-your-ssh-connections-with-ssh-config-file ↩︎
https://www.cloudsavvyit.com/4274/how-to-manage-an-ssh-config-file-in-windows-linux/ ↩︎
https://superuser.com/questions/772660/howto-force-ssh-to-use-a-specific-private-key ↩︎
https://superuser.com/questions/268776/how-do-i-configure-ssh-so-it-doesnt-try-all-the-identity-files-automatically ↩︎
https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/ ↩︎
https://unix.stackexchange.com/questions/606832/ssh-config-global-settings-vs-host ↩︎
https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use ↩︎