logo

Force SSH client to use given private key

Posted on
Authors

Problem

You want force SSH client to use given private key to login to server.

Solution

ssh -i /path/to/private_key [email protected]
ssh -i /path/to/private_key [email protected]

Or with custom SSH port: add parameter -p port_number.

ssh -i /path/to/private_key -p port_number [email protected]
ssh -i /path/to/private_key -p port_number [email protected]

If you added some SSH keys into ssh-agent, you need to add parameter -o "IdentitiesOnly=yes" to prevent ssh-agent from overriding the private key specified.

ssh -o "IdentitiesOnly=yes" -i /path/to/private_key -p port_number [email protected]
ssh -o "IdentitiesOnly=yes" -i /path/to/private_key -p port_number [email protected]

SSH Config File

Host server_name
  HostName server_ip_address
  User user
  Port port_number
  # Add config here
  IdentitiesOnly yes
  IdentityFile /path/to/private_key
Host server_name
  HostName server_ip_address
  User user
  Port port_number
  # Add config here
  IdentitiesOnly yes
  IdentityFile /path/to/private_key