Home > git, magit, networking, proxy, socks, ssh > Use magit with SOCKS proxy

Use magit with SOCKS proxy

Sometimes it is necessary to use git in places where it’s blocked by overzealous firewalls. Instead of a successful push you end up with this:

ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

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

A known way to overcome this problems is tunneling a traffic through an external server with SOCKS protocol over SSH using tsocks, possibly with autossh instead of regular ssh to make a connection permanent, for example:

autossh -M 15000 home -D 12000

In this example home is a name of our external server configured in ~/.ssh/config. We tell autossh to listen on port 15000 and create SOCKS proxy on port 12000 – these are not privileged ports so we don’t need sudo. To actually use SOCKS we need tsocks. First we need to tell tsocks what’s address and a port of our SOCKS proxy. Add this to /etc/tsocks.conf:

server = 127.0.0.1
server_port = 12000

Using tsocks is very simple – just put it before every command you want to tunnel like this:

tsocks git push

Now how to make magit use git in conjunction with tsocks? It’s not correct to do this:

(setq magit-git-executable "tsocks git")

When pushing with magit:

Searching for program: no such file or directory, tsocks git

The problem is that magit will look for a program „tsocks git“ which of course does not exist. What we need to do is to wrap git inside a shell script and call tsocks in it. For example, make ~/bin/tgit with the following contents:

#!/usr/bin/env sh

tsocks git "$@"

Now tell magit to use it:

(setq magit-git-executable "~/bin/tgit")

That’s it – now it’s possible to use magit comfortably as usual.

Kategoriengit, magit, networking, proxy, socks, ssh Tags:
  1. Bisher keine Kommentare