gitサーバーの初期設定をします。
gitを起動させるユーザーを作成します。この作業は必須ではなく"nobody"ユーザーで動作させることもできます。
以下のコマンドを実行します。
セキュリティを高めるためには上記のコマンドでシェルを git-shellに設定するのが良いですが、動作確認などをする場合はshやbashを指定しておいたほうが便利です。
または
今回は以下のコマンドとしました。
useradd -u 6000 -s /bin/false git
リポジトリを作成します。以下のコマンドを実行します。
コマンドはgitユーザーで実行します。
共有リポジトリのため "--bare"オプションと "--shared=true"オプションをつけます。
rootで"git init"コマンドをコマンドを実行した場合は下記のコマンドでディレクトリの所有者を変更します。
ipentecリポジトリの作成例です。
su git
cd /var/lib/git
mkdir ipentec.git
cd ipentec.git
git init --bare --shared=true
下図の状態になります。
/home/repository にリポジトリを作成しようと試しましたが、xinetd でホストした際に正しく動作しませんでした。デフォルトの/var/lib/git にリポジトリを作成するのが確実のようです。
以下のコマンドを実行し、git-daemonを起動します。
今回は下記のコマンドになります。
sudo -u git /usr/libexec/git-core/git-daemon --base-path=/var/lib/git/ --export-all --reuseaddr --verbose /var/lib/git
サーバーが起動すると制御が戻ってきませんので別の端末ウィンドウを開き、以下のコマンドを入力しcloneを作成します。
今回は下記のコマンドになります。
git clone git://localhost/ipentec.git
以下のコマンドを実行してSSHを起動します。
SSH用のRSAキーペアを作成します。作成手順はこちらの記事を参照してください。
サーバーへSSHの公開鍵を追加します。手順はこちらの記事を参照してください。
こちらの記事を参照してクライアントからリポジトリに接続できるか確認します。
これまでの設定でGitサーバーとして動作しますが、高速な読み込み専用のサーバーとして構築する場合は、xinetdにホストして動作させます。
/etc/hosts.allow ファイルを編集します。編集し忘れると接続できない状態に悩まされますので注意してください。
#
# hosts.allow This file contains access rules which are used to
# allow or deny connections to network services that
# either use the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
All:All
All:All を追記します。いったんすべてのホストからのアクセスを許可します。
git-daemon をインストールすると /etc/xinetd.d/git ファイルが作成されます。/etc/xinetd.d/git を編集します。
# default: off
# description: The git damon allows git repositories to be exported using \
# the git:// protocol.
service git
{
disable = yes
socket_type = stream
wait = no
user = nobody
server = /usr/libexec/git-core/git-daemon
server_args = --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
log_on_failure += USERID
}
# default: off
# description: The git damon allows git repositories to be exported using \
# the git:// protocol.
service git
{
disable = yes
socket_type = stream
wait = no
user = git
server = /usr/libexec/git-core/git-daemon
server_args = --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
log_on_failure += USERID
}
git用のユーザーを作成しなかった場合、userはnobodyのままとします。"--base-path"は任意のディレクトリを指定できますが、/home以下のディレクトリでは正しく動作しませんでした。
/var/log/messages に "git-daemon[xxxx]: base-path '/home/repository' does not exist or is not a directory"のエラーメッセージが記録されます。
xinetdを再起動します。下記のコマンドを実行します。
コマンドを実行し、xinetdのサービス一覧を確認します。詳細はこちらの記事を参照してください。
xinetd ベースのサービスに"git:"が含まれていることを確認します。また、サービスの状態を確認します。(下図ではoff)になっています。
先のxinetdのサービス状態でgitが起動していない場合は
コマンドを実行しgitのサービスを起動します。詳しくはこちらの記事を参照してください
"chkconfig --list"コマンドを実行しサービスが起動できたかを確認します。
コマンドを実行し、9418ポートでポートリスニングされているか確認します。
ポートリスニングされている場合は、下図の
外部のサーバーからgitプロトコルでアクセス可能にするために、ファイアウォールのGitプロトコルのポートを解放します。手順はこちらの記事を参照してください。
今回の例では "/etc/xinetd.d/git" のserver_args で "--export-all"が指定されているため、すべてがエクスポート許可になっていますが、"--export-all"を指定しない場合は、リポジトリディレクトリ(今回の例では /var/lib/git/ipentec.git)内に"git-daemon-export-ok"ファイルの配置が必要です。ファイルの作成は以下のコマンドで実行します。
コマンド実行後lsコマンドでディレクトリ内に"git-daemon-export-ok"ファイルが作成されていることを確認します。
サーバーの端末で
コマンドを実行します。
今回の場合は
git clone git://localhost/ipentec.git/
コマンドを実行します。こちらの記事を参照してGitプロトコルでクライアントからリポジトリのクローンが作成できるか確認します。
また、こちらの記事を参照してSSHプロトコルでクライアントからリポジトリに接続できるかも確認します。