Gitのリモート環境を作ってSSHで繋げてみた

いい加減バージョン管理の1個や2個覚えないと・・・と思いつつなかなかやってないので、導入だけ頑張ってみました。
といっても、既に書かれてることをやっただけなんですがね・・・


参考

http://progit.org/book/ja/
http://network.station.ez-net.jp/special/git/client/ssh.linux.asp

サーバにGitを入れる。

ここら辺を見ながら。
http://progit.org/book/ja/ch1-4.html

手持ちの環境
CentOS 6.2 x64_86

uname -a
Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux

最小構成で導入したのでwgetさえありません。
gitのビルドに必要なものを適当に集めます(依存パッケージ含む)

yum -y install sudo
yum -y install wget

yum -y install curl-devel
yum -y install expat expat-devel
yum -y install gettext gettext-devel
yum -y install openssl-devel
yum -y install zlib-devel
yum -y install gcc
yum -y install make
yum -y install perl
yum -y install perl-CPAN # ExtUtil-MakeMakerがおまけでついてくる!

Gitのビルド

cd /usr/local/src
wget http://git-core.googlecode.com/files/git-1.7.8.3.tar.gz
tar xzvf git-1.7.8.3.tar.gz
cd git-1.7.8.3
make prefix=/usr/local all
make prefix=/usr/local install

SSH設定

SSH接続の設定ファイルをいじる。
vi /etc/ssh/sshd_config

# RSAを有効
RSAAuthentication yes
# 公開鍵認証を有効
PubkeyAuthentication yes
# 公開鍵の位置(デフォルトではhome(~/)から見た位置)
AuthorizedKeysFile      .ssh/authorized_keys
# rootのログインを禁止する
PermitRootLogin no
# 空パスワードを認めない
PermitEmptyPasswords no
# パスワード認証を無効
PasswordAuthentication no
# ssh-keygenしたときの-bの値。今回は2048
ServerKeyBits 2048

再起動
/etc/init.d/sshd restart

gitアカウント作成

次にGit鯖のためにユーザ作ったり鍵作ったり、色々やります。
これもやはり、書いてあるので言うとおりにします。
http://progit.org/book/ja/ch4-4.html

useradd git
su - git

SSH接続のための鍵を作ります。

[git@localhost ~]$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa):
Created directory '/home/git/.ssh'.
Enter passphrase (empty for no passphrase): # パスワード入力
Enter same passphrase again:
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
53:7a:90:c0:ac:5c:c5:75:e5:af:dd:97:21:81:0b:02 git@localhost.localdomain
The key's randomart image is:
 +--[ RSA 2048]----+
 |     E.o... ...  |
 |      =... ...   |
 |   . o .o... ..  |
 |    o   .+. . .. |
 |        S .. . ..|
 |         o    .o+|
 |              ..+|
 |                .|
 |                 |
 +-----------------+

[git@localhost ~]$ ls -l .ssh
合計 8
 -rw-------. 1 git git 1743  1月 18 22:46 2012 id_rsa
 -rw-r--r--. 1 git git  407  1月 18 22:46 2012 id_rsa.pub


id_rsa秘密鍵、id_rsa.pubが公開鍵です。
秘密鍵を各端末に持たせます(USBに焼いたり・・・)
公開鍵は.sshにauthorized_keysへ名前を変えて配備しておきます。

mv .ssh/id_rsa.pub .ssh/authorized_keys
chmod 0600 .ssh/authorized_keys


適当に秘密鍵を使って適当なクライアントでSSH接続してみてください。

login as: git
Authenticating with public key "imported-openssh-key"
Passphrase for key "imported-openssh-key": [Password入力]
[git@localhost ~]$

クライアントによってはサポートしてない形式かもしれないので、
そのときはputtygenを使って、秘密鍵を変換します。

gitのリモートリポジトリを作る

適当に作ります。test.gitにしました。

cd ~
mkdir test.git
cd test.git
git --bare init

別クライアントからつなぐ

msysgitな環境から行ってみます。(ほとんど同じっぽい)
~/.ssh/id_rsaを作成し、先ほど作ったgit鯖の秘密鍵を置いておきます。
次に~/.ssh/configを作成し、こんな感じのファイルを作ります。

host localgit
    user git
    hostname 192.168.XXX.XXX
    port 22
    identityfile ~/.ssh/id_rsa

userにはGitサーバのログインユーザ名を指定します。
hostnameにはGitサーバのIPを指定します。
portはportです。
identityfileはSSH秘密鍵の場所を指定します。
作り終えたらいよいよ作業です。


まず、自分が誰か設定しておきます。

git config --global user.name "Ryozi"
git config --global user.email xxx@hotmail.co.jp

自分のプロジェクトを作りましょう。

mkdir myproject
cd myproject
git init

git statusで現在のリポジトリの状態を取得できます。中身はまだ空です。

$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

プロジェクトで使う(と想定した)ファイルを作成してみましょう

touch hello.txt

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# hello.txt
nothing added to commit but untracked files present (use "git add" to track)

hello.txtが追加されたようですが、追跡対象になっていません。


作成したファイルをgitに追跡してもらうために追加します。

git add hello.txt

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: hello.txt
#

新規追加ファイルとして認識してくれました。
コミットしてみましょう。

git commit -m 'initial commit'

$ git commit -m 'initial commit'
[master (root-commit) 7c744a1] initial commit
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.txt

$ git status
# On branch master
nothing to commit (working directory clean)

コミットされたことで、変更がリポジトリに反映されました。


次にこの反映をリモート(さっき作成したgit鯖のtest.git)にも反映させます。

git remote add origin ssh://localgit/home/git/test.git
git push origin master

大体こんな感じになります。

$ git push origin master
Enter passphrase for key '/e/Users/ryozi/.ssh/id_rsa': # パスワード入力
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://localgit/home/git/test.git
* [new branch] master -> master

これでサーバへのプッシュが完了しました。
次にサーバからcloneしてみましょう。
clone先はtestにしました。
中にはhello.txtがあるはずです。

cd ~
git clone ssh://localgit/home/git/test.git test

$ git clone ssh://localgit/home/git/test.git test
Cloning into 'test'...
Enter passphrase for key '/e/Users/ryozi/.ssh/id_rsa': # パスワード入力
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

ls -l test

$ ls -l test
total 0

  • rw-r--r-- 1 ryozi Administ 0 Jan 19 01:37 hello.txt

うまくサーバから取得できたようです。

おまけ:秘密鍵を受け渡す

ぶっちゃけ、ここまでコンソールでやっているので、lessで覗いてコピペでもいい気がします。

less .ssh/id_rsa
cd test.git


USBメモリのマウントから秘密鍵の書き出しまで。
コンソール上からUSBメモリをマウントするにはrootしかできないっぽい?ので、とりあえずrootでやります。
dmesgを使うとロードされたデバイスとかの情報がわかります。最新は一番下っぽいのでその辺りを見ます。

[root@localhost git-1.7.8.3]# dmesg
usb-storage: device found at 3
usb-storage: waiting for device to settle before scanning
usb-storage: device scan complete
scsi 4:0:0:0: Direct-Access TDKMedia Trans-It Drive PMAP PQ: 0 ANSI: 0 CCS
sd 4:0:0:0: Attached scsi generic sg2 type 0
sd 4:0:0:0: [sdc] 7823360 512-byte logical blocks: (4.00 GB/3.73 GiB)
sd 4:0:0:0: [sdc] Write Protect is off
sd 4:0:0:0: [sdc] Mode Sense: 23 00 00 00
sd 4:0:0:0: [sdc] Assuming drive cache: write through
sd 4:0:0:0: [sdc] Assuming drive cache: write through
sdc: sdc1
sd 4:0:0:0: [sdc] Assuming drive cache: write through
sd 4:0:0:0: [sdc] Attached SCSI removable disk

/devでそれっぽいデバイスを探してみると、
/dev/sbcと/dev/sbc1があるのでどちらかをマウントすれば良さそうです。
/dev/sbcでは失敗だったので、/dev/sbc1を選びました。

cd ~
mkdir /mnt/usb
mount -t vfat /dev/sdc1 /mnt/usb
cp /home/git/.ssh/id_rsa /mnt/usb
umount /mnt/usb