Passwordless SSH setup for MacOS Hosts

A tiny handbook to setup passwordless ssh in MacOS
macOS
Author

Zeel B Patel

Published

May 14, 2023

Terminology

HOST: The computer physically present with you.
REMOTE: The remote computer that you’d like to access via ssh.
REMOTE-IP: Ip address of the REMOTE.
PORT: The port on which the ssh server is running on REMOTE.

What is the problem?

Similar to Windows machines, one can run the following commands on a macOS HOST for setting up the passwordless ssh:

ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub -p PORT USERANAME@REMOTE-IP

But this does not work out of the box without the following command which lets your HOST know about the private key.

ssh-add ~/.ssh/id_rsa

After this, connection works fine from macOS CLI. However, if you are trying to connect to REMOTE from VS code, make sure you restart VS code before attempting to connect (quit from the Dock as well).

So far so good. But this setup fails when you reboot your HOST since ssh-add is not perstistently adding the pirvate key to HOST.

So, what to do now?

Permenant solution

I found a permenant and full-proof solution here. For each REMOTE you add in your HOST’s ~/.ssh/config, after generating a key pair and copying it to REMOTE with ssh-copy-id command, modify its entry in ~/.ssh/config like the following and the issue should be permenently resolved.

Host REMOTE
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsa
  HostName REMOTE-IP
  Port PORT
  User USERNAME