How to Transfer SSH Keys to a New Mac
- Difficulty
- Intermediate
- Tested on
- macOS 15.0 - MacBook Pro M4
- You will need
- Access to old Mac, Access to new Mac, Terminal application, External storage or network access
Step by step
Locate the SSH directory on the source Mac
Open the Terminal on your current Mac. Navigate to the hidden SSH directory by entering the command: cd ~/.ssh. Verify the presence of your key files, typically named id_rsa or id_ed25519, by typing ls -la.
Note: Do not share the private key files publicly.Copy the SSH keys to an external drive
Copy the entire .ssh folder contents to an external USB drive or use a secure cloud storage solution. Ensure you include both the public (.pub) and private key files. If using a thumb drive, use the cp command: cp -r ~/.ssh /Volumes/YourDriveName.
Note: Ensure you unmount the external drive before unplugging it.Create the SSH directory on the new Mac
On your new Mac, open Terminal. Create the necessary hidden directory by entering: mkdir -p ~/.ssh. Set the correct permissions to ensure security by running: chmod 700 ~/.ssh.
Note: Strict permission settings are required for SSH to function.Move the keys to the new home folder
Copy the contents from your external source into the new ~/.ssh directory. Once moved, set the file permissions for the private keys to 600 using: chmod 600 ~/.ssh/id_rsa (replace with your specific filename).
Note: Failure to set permissions to 600 will result in 'unprotected private key file' warnings.Add the private key to the SSH agent
Register your key with the macOS SSH agent to enable automatic passphrase handling. Run: ssh-add --apple-use-keychain ~/.ssh/id_rsa. This command stores your passphrase in the macOS Keychain so you do not need to re-enter it frequently.
Note: The --apple-use-keychain flag is unique to macOS.Verify the connection
Test the connection to a known remote host by running: ssh -T [email protected] (or your specific server). If configured correctly, the terminal will confirm successful authentication without prompting for a password.