Gitサーバーの初期設定 - Gitサーバの構築 - Git

gitサーバーの初期設定をします。

gitユーザーの作成

gitを起動させるユーザーを作成します。この作業は必須ではなく"nobody"ユーザーで動作させることもできます。
以下のコマンドを実行します。
useradd -u (ID番号) -s /usr/bin/git-shell git
セキュリティを高めるためには上記のコマンドでシェルを git-shellに設定するのが良いですが、動作確認などをする場合はshやbashを指定しておいたほうが便利です。
useradd -u (ID番号) -s /bin/sh git
または
useradd -u (ID番号) -s /bin/bash git

今回は以下のコマンドとしました。
 useradd -u 6000 -s /bin/false git

リポジトリの作成

リポジトリを作成します。以下のコマンドを実行します。
コマンドはgitユーザーで実行します。
su git
cd /var/lib/git
mkdir (リポジトリの名称.git)
cd (リポジトリの名称.git)
git init --bare --shared=true

解説

共有リポジトリのため "--bare"オプションと "--shared=true"オプションをつけます。

補足

rootで"git init"コマンドをコマンドを実行した場合は下記のコマンドでディレクトリの所有者を変更します。
cd /var/lib/git
chown git -R (リポジトリの名称.git)
chgrp git -R (リポジトリの名称.git)

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=(gitリポジトリパス) --export-all --reuseaddr --verbose (gitリポジトリパス)

今回は下記のコマンドになります。
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/(リポジトリ名)

今回は下記のコマンドになります。
git clone git://localhost/ipentec.git



コマンドを実行しCloneが作成できると下図のメッセージが表示されます。


また、サーバー側の端末ウィンドウに下図のメッセージが表示されます。


リポジトリのクローンが作成されると内部に".git"ディレクトリを持つディレクトリが作成されます。

SSHの設定

SSHの起動

以下のコマンドを実行してSSHを起動します。
/etc/rc.d/init.d/sshd start

SSHキーペアの作成

SSH用のRSAキーペアを作成します。作成手順はこちらの記事を参照してください。

サーバーへの公開鍵の登録

サーバーへSSHの公開鍵を追加します。手順はこちらの記事を参照してください。

動作確認

こちらの記事を参照してクライアントからリポジトリに接続できるか確認します。

xinetdの設定

これまでの設定でGitサーバーとして動作しますが、高速な読み込み専用のサーバーとして構築する場合は、xinetdにホストして動作させます。

/etc/hosts.allow の編集

/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 を追記します。いったんすべてのホストからのアクセスを許可します。

/etc/xinetd.d/git の編集

git-daemon をインストールすると /etc/xinetd.d/git ファイルが作成されます。/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
}

/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            = 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を再起動します。下記のコマンドを実行します。

再起動の場合

/etc/rc.d/init.d/xinetd restart

起動の場合

/etc/rc.d/init.d/xinetd start

xinetdのサービス確認

chkconfig --list
コマンドを実行し、xinetdのサービス一覧を確認します。詳細はこちらの記事を参照してください。

xinetd ベースのサービスに"git:"が含まれていることを確認します。また、サービスの状態を確認します。(下図ではoff)になっています。

gitのサービス起動

先のxinetdのサービス状態でgitが起動していない場合は
chkconfig git on
コマンドを実行しgitのサービスを起動します。詳しくはこちらの記事を参照してください

"chkconfig --list"コマンドを実行しサービスが起動できたかを確認します。

netstatによるポート確認

netdtat -anp | grep 9418
コマンドを実行し、9418ポートでポートリスニングされているか確認します。

ポートリスニングされている場合は、下図の
tcp 0 0 :::9418 :::* LISTEN (pid)/xinetd
が表示されます。(リスニングされていなければ何も表示されません。)

gitプロトコルのポート解放

外部のサーバーからgitプロトコルでアクセス可能にするために、ファイアウォールのGitプロトコルのポートを解放します。手順はこちらの記事を参照してください。

エクスポートの許可

今回の例では "/etc/xinetd.d/git" のserver_args で "--export-all"が指定されているため、すべてがエクスポート許可になっていますが、"--export-all"を指定しない場合は、リポジトリディレクトリ(今回の例では /var/lib/git/ipentec.git)内に"git-daemon-export-ok"ファイルの配置が必要です。ファイルの作成は以下のコマンドで実行します。
cd /var/lib/git/ipentec.git
touch git-daemon-export-ok

コマンド実行後lsコマンドでディレクトリ内に"git-daemon-export-ok"ファイルが作成されていることを確認します。

動作確認

サーバー自身でのアクセス

サーバーの端末で
git clone git://localhost/(リポジトリ名)
コマンドを実行します。
今回の場合は
git clone git://localhost/ipentec.git/
コマンドを実行します。

正常に実行できると下図の画面となります。

リモートクライアントからのアクセス

こちらの記事を参照してGitプロトコルでクライアントからリポジトリのクローンが作成できるか確認します。
また、こちらの記事を参照してSSHプロトコルでクライアントからリポジトリに接続できるかも確認します。
著者
iPentecのプログラマー、最近はAIの積極的な活用にも取り組み中。
とっても恥ずかしがり。
掲載日: 2013-02-04
iPentec all rights reserverd.