Many developers who work with multiple servers face this situation: your primary SSH connections use a custom port (like 1234) configured in ~/.ssh/config
, but GitHub strictly uses port 22. When your default SSH config specifies a non-standard port, Git operations fail with connection errors.
The issue occurs because SSH applies the global port setting to all connections unless specifically overridden. Here's what happens when your config contains:
# ~/.ssh/config
Port 1234
Host *
User git
This makes all SSH connections (including GitHub) attempt to use port 1234.
1. Host-Specific SSH Configuration
The cleanest solution is to modify your SSH config to handle GitHub separately:
# ~/.ssh/config
# Default settings for your servers
Host mydomain.com
Port 1234
User admin
# GitHub-specific settings
Host github.com
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/github_key
2. URL-Based Port Specification
For one-off cases, you can modify the Git remote URL:
[remote "origin"]
url = ssh://git@github.com:22/asdf/asdf.git
3. SSH Config Inheritance
If you have multiple GitHub accounts or special cases, use pattern matching:
Host github.com
HostName github.com
User git
Port 22
Host github-work
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/work_key
Host *
Port 1234
Test your configuration with:
ssh -T git@github.com
You should see GitHub's success message without port-related errors.
For developers managing both personal and work repositories:
# Personal account
Host github.com-personal
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_personal
# Work account
Host github.com-work
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519_work
# Default for other servers
Host *
Port 1234
Then configure Git remotes accordingly:
[remote "origin"]
url = git@github.com-work:company/project.git
Many developers working with multiple servers configure a default non-standard SSH port (like 1234) in their ~/.ssh/config
for security reasons:
Host *
Port 1234
This creates conflicts when connecting to GitHub, which exclusively uses port 22. The global setting forces all SSH connections (including GitHub) to use port 1234, resulting in connection failures.
The solution lies in SSH's host-specific configuration capabilities. Here's the proper way to structure your config file:
# Default configuration for most servers
Host *
Port 1234
ForwardAgent yes
# GitHub specific configuration
Host github.com
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/github_key
Ensure your Git remote URLs use the SSH protocol format. Both these formats work with the above configuration:
git@github.com:user/repo.git
ssh://git@github.com:22/user/repo.git
For existing repositories, update the URL with:
git remote set-url origin git@github.com:user/repo.git
Verify your configuration works by testing the SSH connection:
ssh -T git@github.com
If you encounter issues, use verbose mode for debugging:
ssh -Tv git@github.com
For developers managing multiple GitHub accounts with different SSH keys, extend the configuration:
Host github-work
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/work_github_key
Host github-personal
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/personal_github_key
Then use these custom hosts in your Git remotes:
[remote "origin"]
url = git@github-work:company/project.git