Logo Neurocoda

macOS SSH Key Auto-Configuration for Git

Neurocoda
Neurocoda
2026-07-03 12:47:12 205 Words 2 Mins ...

Prerequisites: Generate SSH Key Pair

  1. Open Terminal
    Use Spotlight (⌘+Space) to search for “Terminal” and open it.

  2. Generate Key Pair
    Execute the following command (replace [email protected] with your email):

    Terminal window
    ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/git_example
    • When prompted, press Enter to use default settings.
    • For extra security, you can set a passphrase.
  3. Check Generated Files
    Two files will be created in the ~/.ssh directory:

    Terminal window
    git_example # Private key (keep secure)
    git_example.pub # Public key (to be configured on Git server)

Configure Automatic Use of Specified Key

  1. Edit SSH Config File

    Terminal window
    mkdir -p ~/.ssh && chmod 700 ~/.ssh
    vim ~/.ssh/config
  2. Add Configuration
    Use the following template (modify parameters as needed):

    # Main Git service configuration
    Host git.example.com
    HostName git.example.com
    User git
    Port 22
    IdentityFile ~/.ssh/git_example
    IdentitiesOnly yes
    # Additional configuration (for other Git services)
    Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_key
  3. Set File Permissions

    Terminal window
    chmod 600 ~/.ssh/config

Server-Side Configuration

  1. Add Public Key to Git Service
    Copy the public key content:
    Terminal window
    pbcopy < ~/.ssh/git_example.pub
    Add the public key to:
    • GitHub: Settings → SSH and GPG keys
    • GitLab: Preferences → SSH Keys
    • Self-hosted Git service: Server’s ~/.ssh/authorized_keys file

Verify Configuration

  1. Test SSH Connection

    Terminal window

    On success, you will see a welcome message from the service (e.g., GitHub’s “Hi username!”).

  2. Test with Repository Clone

    Terminal window
    git clone [email protected]:username/repo.git

    The system will automatically use the specified key without manual intervention.

Troubleshooting

  1. Debug Mode
    Add the -v flag to see detailed connection process:

    Terminal window
  2. Common Issues

    • Error Permissions 0644 are too open: Run chmod 600 ~/.ssh/*
    • Connection timeout: Check firewall settings or network proxy configuration.
    • Authentication failed: Confirm that the public key has been correctly added to the server.

Extended Use

By configuring multiple Host blocks, you can:

  • Use different keys for different platforms (GitHub/GitLab/Gitee)
  • Separate work and personal accounts
  • Manage keys in multi-server environments

Note: The git.example.com in this article is a placeholder. Replace it with your actual Git service address.

Title: macOS SSH Key Auto-Configuration for Git Author: Neurocoda Created at: 2026-07-03 12:47:12 Link: https://neurocoda.com/en/posts/macos-ssh-key-auto-configuration-for-git-en/ License: This work is licensed under CC BY-ND 4.0.

Comments