12

Is it possible to run ssh with ignoring default .ssh directory and specify other one or - better - specified private key ?

For example:

ssh --private-key other_id_rsa login@host
hsz
  • 148,279
  • 62
  • 259
  • 315

3 Answers3

15

You can use the -i option.

Source: man ssh

-i identity_file
    Selects a file from which the identity (private key) for public key authentication is read.  The default is ~/.ssh/identity for protocol
    version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and ~/.ssh/id_rsa for protocol 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 configuration
    files).  ssh will also try to load certificate information from the filename obtained by appending -cert.pub to identity filenames.
Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • yes this is a good answer. Not sure if this is recommended practice or not but make sure you file permission are ok. e.g. `chmod 400 private_rsa_key.pem` then `ssh -i private_rsa_key.pem user@host` – Daniel Oct 21 '21 at 10:24
5

You can also add a specific configuration to each host you access, which is pretty much the same as persisting the usage of the flags available in ssh.

There's an entire world of flags available, and there are some mappings for each different service specialization provided. In your case, using specific id_rsa files, you could write down to your ~/.ssh/config file:

...

Host host_alias
    HostName host_name
    IdentityFile ~/.ssh/id_rsa_you_want

...

Then, you can simply use:

ssh host_alias

And the id_rsa_you_want will be used -- as well as any further configurations you may apply to the connection. See man ssh_config for the entire list of available directives.

Rubens
  • 14,478
  • 11
  • 63
  • 92
3

Another way is to use ssh-agent and ssh-add commands manually before using ssh.

ssh-agent (if not running already)
ssh-add /path/to/private_key

example:
ssh-agent
ssh-add ~/.ssh/id_rsa
Jonathan L
  • 9,552
  • 4
  • 49
  • 38