研究室内部のファイルサーバーに構築したGitリポジトリを研究室の外から利用するための方法メモ。
サーバー環境
[ファイルサーバー] <-- ssh:22番ポート --> [中継サーバ] <-- ssh:22番ポート --> [ローカルマシン]
- ローカルマシン
- Unix系
- OpenSSHインストール済み
ローカルマシンで公開鍵の準備(まだしていなければ)
公開鍵の準備をする
% ssh-keygen -t rsa % cd .ssh % mv id_rsa.pub id_rsa.pub.localmachine % mv id_rsa id_rsa.localmachine
id_rsa.pub.localmachineを安全な方法で、ファイルサーバーと中継サーバーにそれぞれ持っていく。
ファイルサーバーでの設定
Git用ディレクトリの準備
Gitリポジトリを置くディレクトリを準備する。
% mkdir workfiles.git % cd ./workfiles.git % git --bare init --shared=true
中継サーバーでの設定
.sshディレクトリを用意する(もし、まだならば)
% ssh-keygen -t rsa
ローカルマシンのSSH公開鍵を~/.ssh/authorized_keysへ追記する
% cat id_rsa.pub.localmachine >> ~/.ssh/authorized_keys % chmod 600 ~/.ssh/authorized_keys (もし、まだならば)
ローカルマシンでポートフォワードの準備
% cd ~/.ssh % touch config % chmod 600 config
推奨:中継サーバのOpenSSH 5.4以上の場合
中継サーバのOpenSSHのバージョンが5.4以上の場合は、以下のようにする(へきょのーと:netcatを使わずにSSHのProxyCommandで多段sshより)。
Host fileserver HostName 192.168.11.23 ProxyCommand ssh -l next49 192.168.11.300 -W %h:%p
接続実験する。
% ssh fileserver
うまくログインできたならば、ポートフォーワード成功。中継サーバーをトンネルとして利用し、ファイルサーバーにSSHアクセスをしている。
中継サーバにnetcat(nc)がインストールされている場合
中継サーバにnetcat(nc)がインストールされている場合は以下のようにする(コメント欄で教えていただきました)。
Host interchange HostName 192.168.11.300 User next49 Protocol 2,1 IdentityFile ~/.ssh/id_rsa.localmachine Host fileserver HostName 192.168.11.23 User next49 Protocol 2,1 ProxyCommand ssh interchange nc -w 5 %h %p
接続実験する。
% ssh fileserver
うまくログインできたならば、ポートフォーワード成功。中継サーバーをトンネルとして利用し、ファイルサーバーにSSHアクセスをしている。
中継サーバにnetcat(nc)がインストールされていない場合
可能ならば中継サーバにnetcat(nc)をインストールした方が良い。どうしてもできないならば、configの内容を以下のようにする。。
Host interchange HostName 192.168.11.300 User next49 Protocol 2,1 LocalForward 50025 192.168.11.23:22 IdentityFile ~/.ssh/id_rsa.localmachine Host fileserver HostName localhost User next49 Port 50025 Protocol 2,1 IdentityFile ~/.ssh/id_rsa.localmachine
接続実験してみる。
% ssh interchange
うまくログインできたならば、別のターミナルにてファイルサーバーにログインしてみる。
% ssh fileserver
うまくログインできたならば、ポートフォーワード成功。中継サーバーをトンネルとして利用し、ファイルサーバーにSSHアクセスをしている。
ローカルマシンでリポジトリ取得
推奨:中継サーバにnetcat(nc)がインストールされている場合
リポジトリの取得。
% cd % git clone ssh://fileserver/export/home/next49/workfiles.git % cd workfiles % git remote -v (リモートリポジトリの確認。以下のように表示されていればOK) origin ssh://fileserver/export/home/next49/workfiles.git (fetch) origin ssh://fileserver/export/home/next49/workfiles.git (push)
リモートリポジトリにpushしてみる。
% touch newfile % git add newfile % git commit -m "add files" % git push
中継サーバにnetcat(nc)がインストールされていない場合
まず、中継サーバー経由でトンネル準備。
% ssh next49@interchange
別ターミナルにてリポジトリの取得。
% cd % git clone ssh://next49@fileserver:50025/export/home/next49/workfiles.git % cd workfiles % git remote -v (リモートリポジトリの確認。以下のように表示されていればOK) origin ssh://next49@fileserver:50025/export/home/next49/workfiles.git (fetch) origin ssh://next49@fileserver:50025/export/home/next49/workfiles.git (push)
リモートリポジトリにpushしてみる。
% touch newfile % git add newfile % git commit -m "add files" % git push
注意点は、pushやpullするときには中継サーバ経由でトンネルをあけておく必要があるという点。