简体   繁体   中英

Git - SSH - Hosts: How can I delegate different IPs to remote origin, depending on what works each time?

When at home, git fetch origin master should connect over ssh to git@192.168.xx.xx .

When on the road, git fetch origin master should connect over ssh to git@xxx.linkpc.net .

Possible Solutions

  • Having multiple remotes for the same repository/branch works but git tracks multiple heads unnecessarily. This solution just messes with the beauty.
  • Assigning a hostname to remote and comment/uncomment /etc/hosts entries to delegate ips is a nice solution but includes sudoing and entering the root password which is kind of tedious.
  • A per user hosts file is out of the question.
  • Writing a script that each time the origin is called will delegate the correct ip, is ideal .
  • Writing a script that will be executed as root each time a user calls it, seems to be a marginally fair solution .

Question

How could someone tackle with this issue using a solution that -by all means- shall not be considered "of a hackish character"?

I'm going to give you a hint and you can formulate the solution. You might be able to use your SSH config with the Match and exec keyword :

Match

Restricts the following declarations (up to the next Host or Match keyword) to be used only when the conditions following the Match keyword are satisfied.

...

The exec keyword executes the specified command under the user's shell. If the command returns a zero exit status then the condition is considered true.

Remember that you are tunneling over SSH, so your ~/.ssh/config brings a per-user hosts file back into the question, eg,

Host dynamic-repo-host
    HostName 192.168.xx.xx
    #HostName xxx.linkpc.net

Then modify the URL of origin as in

ssh config remote.origin.url git@dynamic-repo-host

Modifying ~/.ssh/config by hand as appropriate then gives the effect you want.

If you want a completely hands off solution, look into using Match in ~/.ssh/config , but the specific commands to execute will depend on the particulars of the networks on which your local machine runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM