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 user@server
ssh -i /path/to/private_key user@server

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

ssh -i /path/to/private_key -p port_number user@server
ssh -i /path/to/private_key -p port_number user@server

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 user@server
ssh -o "IdentitiesOnly=yes" -i /path/to/private_key -p port_number user@server

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