私のディレクトリに基づいてSSHキーを自動的に追加します。

私のディレクトリに基づいてSSHキーを自動的に追加します。

2つのGitHubアカウントがあります。個人用コピー1個と業務用コピー1個。それぞれは独自のSSHキーgh_personalgh_business内部エントリで設定されます~/.ssh

にプライベートプロジェクトがあります~/code/bejebeje。プロジェクトを操作するたびに、次のコマンドを実行する必要があります。

eval `ssh-agent -s`       

以下は:

ssh-add ~/.ssh/gh-personal

そうしてgit pullこそ同じことができますgit push

推測されましたが、私はいつもこのことをするのを忘れています。

ディレクトリに入るときにLinuxシステムにこのタスクを自動的に実行させる方法はありますか~/code/bejebeje

私はアーチLinuxを使用しています。

ベストアンサー1

次のように、さまざまなアドレスに一致するssh_configAKAを作成できます。~/.ssh/config

Host *
  Protocol                 2
  PreferredAuthentications publickey,password
  VisualHostKey            yes

Host work_gh
  HostName                 github.com
  IdentityFile             ~/.ssh/id_rsa.work
  IdentitiesOnly           yes
  ForwardAgent             no
  LogLevel                 DEBUG

Host personal_gh
  HostName                 github.com
  IdentityFile             ~/.ssh/id_rsa.personal
  IdentitiesOnly           yes
  ForwardAgent             no
  LogLevel                 DEBUG

また、この構成にはssh-agentは必要なく、プロキシを使用しようとしません。設定できるオプションのIdentitiesOnly詳細をご覧ください。man ssh_configssh_config

Gitに戻り、複製するアドレスのホスト名として「personal_gh」という名前を使用できます。

git clone git@personal_gh:mypersonalghuser/things.git

You can also change existing git repositories:

git remote show origin
* remote origin
  Fetch URL: git@personal_gh:mypersonalghuser/things.git
  Push  URL: git@personal_gh:mypersonalghuser/things.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

2つのアカウントにプッシュする必要があるリポジトリがある場合は、gitに複数のリモートデバイスを追加し、次のようにプッシュすることをお勧めします。

git push my-dest-account-1 master
git push my-dest-account-2 master

以下からリモコンを追加できます。

git remote add --help

とにかく、SSHクライアント構成でこれを構成した場合、使用されるSSHキーは通常、gitアドレスのホスト名部分に基づいて一致します。~/.ssh/config

おすすめ記事