62

ssh will look for its keys by default in the ~/.ssh folder. I want to force it to always look in another location.

The workaround I'm using is to add the keys from the non-standard location to the agent:

ssh-agent
ssh-add /path/to/where/keys/really/are/id_rsa 

(on Linux and MingW32 shell on Windows)

tardate
  • 16,424
  • 15
  • 50
  • 50
  • btw, the reason why I wanted to do this is so that I could keep my keys in Dropbox .. works a treat! – tardate Mar 28 '11 at 11:29
  • 8
    @tardate, hmm, trusting dropbox with your keys seems dangerous, unless you password-protect them well ... – gatoatigrado Dec 31 '12 at 23:15
  • 3
    you should really have separate key in each of your computers, this way when someone stols one of them you you will just remove its public key from server without disabling rest of the computers. Having private key on Dropbox is equivalent of having text file with your passwords on a Dropbox => Something may or may not happen but still bad idea. – equivalent8 Nov 20 '14 at 14:14
  • @equivalent8 - noted! I actually use this mainly for seeding my (main one and only) computer from Dropbox. So when I get a new computer, it's ready to go.. – tardate Dec 06 '14 at 13:54

3 Answers3

99

If you are only looking to point to a different location for you identity file, the you can modify your ~/.ssh/config file with the following entry:

IdentityFile ~/.foo/identity

man ssh_config to find other config options.

Drew Frezell
  • 2,628
  • 21
  • 13
  • 3
    Note also that you can list this parameter multiple times for multiple keys. However, listing too many keys (typically >4) can cause auth to fail before prompting for a password on systems where your key isn't valid. ssh-agent, or keychain (http://www.gentoo.org/proj/en/keychain/) are helpful here. – jmanning2k Sep 17 '08 at 20:12
  • if you have password protected for you ssh keys, you'd better `ssh-add your-private-key`, otherwise, it's always asking for key's password – Pengfei.X Aug 06 '14 at 06:46
  • I had to add that line at the beginning of the config. Appending it to the end didn't work. – SebK Apr 05 '17 at 04:52
23

man ssh gives me this options would could be useful.

-i identity_file Selects a file from which the identity (private key) for RSA or DSA authentication is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for pro- tocol version 2. Identity files may also be specified on a per- host basis in the configuration file. It is possible to have multiple -i options (and multiple identities specified in config- uration files).

So you could create an alias in your bash config with something like

alias ssh="ssh -i /path/to/private_key"

I haven't looked into a ssh configuration file, but like the -i option this too could be aliased

-F configfile Specifies an alternative per-user configuration file. If a configuration file is given on the command line, the system-wide configuration file (/etc/ssh/ssh_config) will be ignored. The default for the per-user configuration file is ~/.ssh/config.

Saurabh
  • 5,176
  • 4
  • 32
  • 46
roo
  • 7,106
  • 8
  • 39
  • 45
0

Update for Git Bash on Windows 10: on my system, git bash app will work over the ssh layer (brought by OpenSSH) look for an environment variable called HOME (To Windows key and type in "env" to edit env vars). If this variable points to a place that doesn't exist, git bash may never open.

Like on Linux, Git Bash app will look for its config file in %HOME%\.ssh.

e.g. If you set HOME to C:\Users\Yourname, than it will look for C:\Users\Yourname\.ssh

Finally, within config text file, git bash will look for IdentifyFile path. On Windows, set the path using cygwin notation.

e.g. to /e/var/www/certs/keys/your_passwordless_key.key

Bonus: for free, PHPStorm will use that setup. Restart IDE if you've just changed settings.

Fabien Haddadi
  • 1,814
  • 17
  • 22