29

First I am fairly new to SSH.

From this question I have seen the need and benefit of setting up an SSH config file. While I was doing my research I noticed that there is a lot to know on SSH and I also found out that I have been using SSH keys and not the SSH server. I have been using the keys to push my code to my hosted repos.

So now my questions are(I am using windows 10):

  1. Can I set up an SSH config file without a tool like openSSH, if so how do I do it?
  2. Where in my computer is this config file stored?
qujck
  • 14,388
  • 4
  • 45
  • 74
YulePale
  • 6,688
  • 16
  • 46
  • 95

2 Answers2

49

Generally, in Windows machine, the SSH config file stored in the following location: /c/Users/PC_USER_NAME/.ssh/

Just follow the steps in below (if you're using the Git Bash):

  1. Go to the .ssh directory /c/Users/PC_USER_NAME/.ssh/, click right mouse button and choose "Git Bash Here"
  2. Create a file named "config" with the following command:
touch config
  1. Now open the config file with the command:
nano config
  1. Now write the following lines inside the config file

Let's assume you've created two files named id_rsa_hub for Github and id_rsa_lab for GitLab

# GITHUB
Host github.com
   HostName github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_hub

# GITLAB
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_lab
Fatema Tuz Zuhora
  • 3,088
  • 1
  • 21
  • 33
1

By default, your %HOME% will be your %USERPROFILE%

To create new keys, make sure to add to your environment variables:

set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%

That way, you will have all the commands you need, including ssh-keygen, on Windows 10, right from any CMD session (without even opening a git bash session).

To create a new SSH key, try first to use an SSH key without passphrase, and make sure to create it with the legacy format in a CMD session (not git bash):

ssh-keygen -m PEM -t rsa -P "" -f %USERPROFILE%\.ssh\myNewKey

'myNewKey': no extension; no '.xxx'.
(The -m PEM is for producing the legacy format, because not all remote servers are able to understand then new OPENSSH format)

Then add your %USERPROFILE%\.ssh\config file, to associate your new key to your service (in which you will have registered your %USERPROFILE%\.ssh\myNewKey.pub public key)

See "Multiple Github Accounts With Git In Windows" for an concrete example, as in:

ssh-keygen -m PEM -t rsa -P "" -f %USERPROFILE%\.ssh\github_key

Then, in %USERPROFILE%\.ssh\config:

Host gh
 HostName github.com
 User git
 IdentityFile ~/.ssh/github_key  

That way, you can replace the remote URL of GitHub repository with:

gh:<yourGitHubUser>/<yourGitHubRepo>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Can't keys and config be saved under C:\ProgramData\ssh\? – motorbaby Apr 05 '20 at 19:00
  • 1
    @motorbaby not by default, as SSH looks for "`HOME`", which, on Windows, is `%USERPROFILE%`. I suppose the config file itself (which, again, must be in HOME: https://www.ssh.com/ssh/config/) can then refer to public/private keys which can be stored anywhere you want. – VonC Apr 05 '20 at 19:12
  • im in linux. is it necessory to add "User' in config file, can you share any kind of doc to check more about config files – legacy May 27 '21 at 09:04
  • @legacy Adding User is a best practice, to avoid repeating said user in the SSH URL. See more at https://linuxize.com/post/using-the-ssh-config-file/, and https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/ – VonC May 27 '21 at 09:17