How to fix "connect to host github.com port 22: Operation timed out"

Jul 9, 2024·
Gert de Pagter
Gert de Pagter
· 2 min read

The problem

This weekend I was at a holiday resort. In the morning I wanted to do a little bit of work, and encountered the following error when I tried to pull a repo.

$ git pull
ssh: connect to host github.com port 22: Operation timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

At first, I assumed my internet had a hick-up, so I just ran the command again, only to be greeted by the same error message as before.

As it turns out, the holiday resort I was in was blocking basically anything that wasn’t normal http requests, which use port 80 and 443 for http and https.

Connecting ssh over another port

Thankfully both GitHub and GitLab offer a solution for this problem. Both have an alternative ssh server, that you can ssh to on port 443. To use it, you need to change your ssh config, which you can find in ~/.ssh/config.

Add the following bits in your ssh config for either GitLab or GitHub. The Host <url> part tells us the next bit of config is for that host. The Hostname part makes your ssh use this for host instead of the original one. The User key give it the user (which is git by default, but we specify it to be sure. And finally the Port is the bit that helps us out, as we will now ssh over port 443. If you attempt to just change the SSH port to 443, but keep the normal host, that unfortunately won’t work, as the normal servers only accept ssh over port 22.

# GitLab config
Host gitlab.com
    Hostname altssh.gitlab.com
    User git
    Port 443

# GitHub config
Host github.com
    Hostname ssh.github.com
    Port 443
    User git

Once your holiday is over, and your back home, you can just remove the config you added, and you’re back on the original GitHub or GitLab servers.

If you want to get notified of the next blog post, join the newsletter.