Amazon EC2にGitサーバーを構築してみた


この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので十分ご注意ください。

Gitを理解するために、EC2にGitサーバーを構築してみました。プロジェクトの要件でGitHubが使えず、プライベートなリモートリポジトリを利用しなきゃいけない場合に役立つかと思います。

 

 

GitのリモートレポジトリをEC2に構築してみた

■前準備

Gitサーバーやりとり

 

 

【用意するもの】

  • Gitサーバー用EC2
  • クライアント用EC2×2台(EC2ではなく、自身のローカルPCでも大丈夫です。)

 

セキュリティグループのインバウンドルールはSSHだけ開けています。

 

 

■手順①Gitクライアント1側準備

まずはクライアント側の設定を行っていきます。以下の順でコマンドを打っていきます。

 

sudo yum -y install git
cd .ssh
ssh-keygen -t rsa
chmod 600 .ssh/saitou-keypairs.pem
scp -i ./saitou-keypairs.pem gitkey.pub ec2-user@GitサーバーIPアドレス:/tmp
touch config
vi config
cd
chmod 600 ~/.ssh/config

 

 

コマンド結果は以下の通りです。

 

[ec2-user@ip-192-168-0-249 ~]$ sudo yum -y install git
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check

省略

Dependency Installed:
  git-core.x86_64 0:2.38.1-1.amzn2.0.1 git-core-doc.noarch 0:2.38.1-1.amzn2.0.1    perl-Error.noarch 1:0.17020-2.amzn2
  perl-Git.noarch 0:2.38.1-1.amzn2.0.1 perl-TermReadKey.x86_64 0:2.30-20.amzn2.0.2

Complete!
[ec2-user@ip-192-168-0-249 ~]$ cd .ssh
[ec2-user@ip-192-168-0-249 .ssh]$  ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): gitkey
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in gitkey.
Your public key has been saved in gitkey.pub.
The key fingerprint is:
SHA256:/OmJgNXiLDTBJpS6CJHsXlEjxlA1D4eIuw/UY0ngj4o ec2-user@ip-192-168-0-249.ap-                                       southeast-1.compute.internal
The key's randomart image is:
+---[RSA 2048]----+
|.+B=+*..         |
|++o*..*          |
|.+= *  .         |
|o+oO . o         |
|=o+.+ o S        |
|=+ . * . . .     |
|E o o +   o      |
|   . . . o .     |
|        . o      |
+----[SHA256]-----+
[ec2-user@ip-192-168-0-249 ~]$ chmod 600 .ssh/saitou-keypairs.pem
[ec2-user@ip-192-168-0-249 ~]$ cd .ssh
[ec2-user@ip-192-168-0-249 .ssh]$ scp -i ./saitou-keypairs.pem gitkey.pub ec2-user@GitサーバーIPアドレス:/tmp
gitkey.pub                                                                           100%  439   285.2KB/s   00:00
[ec2-user@ip-192-168-0-249 .ssh]$ touch config
[ec2-user@ip-192-168-0-249 .ssh]$ vi config

 

 

configファイルに以下のドキュメントを貼り付けてください。「GitサーバーIPアドレス」部分は変更してください。

 

Host gitserver
HostName GitサーバーIPアドレス
Port 22
User gitserver
IdentityFile ~/.ssh/gitkey

 

 

貼り付けが完了しましたら、Escを押し「:wq」で保存しましょう。
 

[ec2-user@ip-192-168-0-71 .ssh]$ scp -i ./saitou-keypairs.pem gitkey.pub ec2-user@GitサーバーIPアドレス:/tmp
gitkey.pub                                                                        100%  438   272.3KB/s   00:00
[ec2-user@ip-192-168-0-71 .ssh]$ touch config
[ec2-user@ip-192-168-0-71 .ssh]$ vi config
[ec2-user@ip-192-168-0-71 .ssh]$ cd
[ec2-user@ip-192-168-0-71 ~]$ chmod 600 ~/.ssh/config

 

 

■手順②Gitサーバー側準備

続いて、リモートリポジトリを用意します。コマンドは以下の順です。

 

 

sudo yum -y install git
sudo useradd gitserver
sudo passwd gitserver
su gitserver
cd
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
cat /tmp/gitkey.pub >> ~/.ssh/authorized_keys
mkdir git
cd git
mkdir testrepo.git
cd testrepo.git
git init --bare

 

 

コマンド結果は以下です。

 

[ec2-user@ip-10-0-1-190 ~]$ sudo yum -y install git
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:2.38.1-1.amzn2.0.1 will be installed

//省略

Dependency Installed:
  git-core.x86_64 0:2.38.1-1.amzn2.0.1                  git-core-doc.noarch 0:2.38.1-1.amzn2.0.1
  perl-Error.noarch 1:0.17020-2.amzn2                   perl-Git.noarch 0:2.38.1-1.amzn2.0.1
  perl-TermReadKey.x86_64 0:2.30-20.amzn2.0.2

Complete!
[ec2-user@ip-10-0-1-190 ~]$ sudo useradd gitserver
[ec2-user@ip-10-0-1-190 ~]$ sudo passwd gitserver
Changing password for user gitserver.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[ec2-user@ip-10-0-1-190 ~]$ su gitserver
Password:
[gitserver@ip-10-0-1-190 ec2-user]$ cd
[gitserver@ip-10-0-1-190 ~]$ mkdir .ssh
[gitserver@ip-10-0-1-190 ~]$ chmod 700 .ssh
[gitserver@ip-10-0-1-190 ~]$ touch .ssh/authorized_keys
[gitserver@ip-10-0-1-190 ~]$ chmod 600 .ssh/authorized_keys
[gitserver@ip-10-0-1-190 ~]$ cat /tmp/gitkey.pub >> ~/.ssh/authorized_keys
[gitserver@ip-10-0-1-190 ~]$ mkdir git
[gitserver@ip-10-0-1-190 ~]$ cd git
[gitserver@ip-10-0-1-190 git]$ mkdir testrepo.git
[gitserver@ip-10-0-1-190 git]$ cd testrepo.git
[gitserver@ip-10-0-1-190 testrepo.git]$ git init --bare
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/gitserver/git/testrepo.git/

 

 

■手順③Gitクライアント1側準備

Gitサーバの準備が完了しましたので、クライアント側にリポジトリをクローンします。コマンドは以下の通りです。

 

mkdir myproject
cd myproject
git init
git remote add origin gitserver@gitserver:/home/gitserver/git/testrepo.git
git clone gitserver@gitserver:/home/gitserver/git/testrepo.git
cd testrepo
touch testfile
vi testfile
git add testfile
git commit -m 'Add New File'
git push origin master

 

 

コマンド結果は以下です。

 

[ec2-user@ip-192-168-0-71 ~]$ mkdir myproject
[ec2-user@ip-192-168-0-71 ~]$ cd myproject
[ec2-user@ip-192-168-0-71 myproject]$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/ec2-user/myproject/.git/
[ec2-user@ip-192-168-0-71 myproject]$ git remote add origin gitserver@gitserver:/home/gitserver/git/testrepo.git
[ec2-user@ip-192-168-0-71 myproject]$ git clone gitserver@gitserver:/home/gitserver/git/testrepo.git
Cloning into 'testrepo'...
warning: You appear to have cloned an empty repository.
[ec2-user@ip-192-168-0-71 myproject]$ cd testrepo
[ec2-user@ip-192-168-0-71 testrepo]$ touch testfile
[ec2-user@ip-192-168-0-71 testrepo]$ vi testfile

 

 

作成したtestfileに以下のドキュメントを貼り付けます。

 

Test File

 

 

貼り付けが完了しましたら、Escを押し「:wq」で保存しましょう。

 

[ec2-user@ip-192-168-0-71 testrepo]$ git add testfile
[ec2-user@ip-192-168-0-71 testrepo]$ git commit -m 'Add New File'
[master (root-commit) 209b635] Add New File
 Committer: EC2 Default User <ec2-user@ip-192-168-0-71.ap-southeast-1.compute.internal>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 testfile
[ec2-user@ip-192-168-0-71 testrepo]$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To gitserver:/home/gitserver/git/testrepo.git
 * [new branch]      master -> master

 

 

クライアント1のローカルにローカルリポジトリが用意でき、pushコマンドでファイルをリモートリポジトリに送信しました。

 

 

■手順④Gitクライアント2側準備

クライアント2のGit環境を準備します。コマンドは以下の通りです。

 

 

sudo yum -y install git
mv saitou-keypairs.pem .ssh/
chmod 600 .ssh/saitou-keypairs.pem
cd .ssh
ssh-keygen -t rsa gitkey2
scp -i ./saitou-keypairs.pem gitkey2.pub ec2-user@GitサーバーIPアドレス:/tmp
touch config
vi config
cd
chmod 600 ~/.ssh/config
mkdir myproject
cd myproject
git init
git remote add origin gitserver@gitserver:/home/gitserver/git/testrepo.git
git clone gitserver@gitserver:/home/gitserver/git/testrepo.git
cd testrepo
ls
vi testfile
git add testfile
git commit -m 'Add Text'
git push origin master

 

 

コマンド結果は以下です。

 

[ec2-user@ip-192-168-0-173 ~]$ sudo yum -y install git
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check

//省略

Dependency Installed:
  git-core.x86_64 0:2.38.1-1.amzn2.0.1                      git-core-doc.noarch 0:2.38.1-1.amzn2.0.1
  perl-Error.noarch 1:0.17020-2.amzn2                       perl-Git.noarch 0:2.38.1-1.amzn2.0.1
  perl-TermReadKey.x86_64 0:2.30-20.amzn2.0.2

Complete!
[ec2-user@ip-192-168-0-173 ~]$ mv saitou-keypairs.pem .ssh/
[ec2-user@ip-192-168-0-173 ~]$ chmod 600 .ssh/saitou-keypairs.pem
[ec2-user@ip-192-168-0-173 ~]$ cd .ssh
[ec2-user@ip-192-168-0-173 .ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): gitkey2
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in gitkey2.
Your public key has been saved in gitkey2.pub.
The key fingerprint is:
SHA256:ABs7HkqWahI64twJIgMGQKujVRCZUdnDH70v+QWA2II ec2-user@ip-192-168-0-173.ap-southeast-1.compute.internal
The key's randomart image is:
+---[RSA 2048]----+
|=.+*+= o o       |
|. +oE=* + o      |
|oo+ * .+ . o     |
|== + o .. . .    |
|%oo .   S  o .   |
|X=o .     o . .  |
|.o o       o .   |
|            .    |
|                 |
+----[SHA256]-----+
[ec2-user@ip-192-168-0-173 .ssh]$ scp -i ./saitou-keypairs.pem gitkey2.pub ec2-user@18.142.237.1:/tmp
The authenticity of host '18.142.237.1 (18.142.237.1)' can't be established.
ECDSA key fingerprint is SHA256:Ej8vkIkkFQyte1TUvAarlJvoDsuIPjHsNuYVRNNr/Oo.
ECDSA key fingerprint is MD5:5f:6c:8a:a2:8b:86:0e:b7:57:64:24:70:5d:0b:ec:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '18.142.237.1' (ECDSA) to the list of known hosts.
gitkey2.pub                                                                     100%  439   316.1KB/s   00:00
[ec2-user@ip-192-168-0-173 .ssh]$ touch config
[ec2-user@ip-192-168-0-173 .ssh]$ vi config

 

 

condigファイルに以下のドキュメントを貼り付けましょう。「GitサーバーIPアドレス」部分は変更してください。

 

Host gitserver
HostName GitサーバーIPアドレス
Port 22
User gitserver
IdentityFile ~/.ssh/gitkey2

 

 

貼り付けが完了しましたら、以下のコマンドを打ちます。

cd
chmod 600 ~/.ssh/config

 

 

コマンド結果は以下です。

 

[ec2-user@ip-192-168-0-173 .ssh]$ cd
[ec2-user@ip-192-168-0-173 ~]$ chmod 600 ~/.ssh/config

 

 

■手順⑤Gitサーバー側準備

クライアント2から公開鍵をtmpディレクトリにコピーしましたので、authorized_keysに出力します。以下のコマンドを実行してください。

 

cd
cat /tmp/gitkey2.pub >> ~/.ssh/authorized_keys

 

 

■手順⑥Gitクライアント2側準備

GitサーバーとSSH接続が可能になったかと思いますので、クライアント2の環境にもリモートリポジトリをクローンしましょう。使用するコマンドは以下の通りです。

 

 

mkdir myproject
cd myproject
git init
git remote add origin gitserver@gitserver:/home/gitserver/git/testrepo.git
git clone gitserver@gitserver:/home/gitserver/git/testrepo.git
cd testrepo
ls
vi testfile

 

 

コマンド結果は以下です。

 

[ec2-user@ip-192-168-0-173 ~]$ mkdir myproject
[ec2-user@ip-192-168-0-173 ~]$ cd myproject
[ec2-user@ip-192-168-0-173 myproject]$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/ec2-user/myproject/.git/
[ec2-user@ip-192-168-0-173 myproject]$ git remote add origin gitserver@gitserver:/home/gitserver/git/testrepo.git
[ec2-user@ip-192-168-0-173 myproject]$ git clone gitserver@gitserver:/home/gitserver/git/testrepo.git
Cloning into 'testrepo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
[ec2-user@ip-192-168-0-173 myproject]$ cd testrepo
[ec2-user@ip-192-168-0-173 testrepo]$ ls
testfile
[ec2-user@ip-192-168-0-173 testrepo]$ vi testfile

 

 

リポジトリのクローンに成功し、クライアント1で作成した“testfile”がクライアント2でも確認できました。クライアント2でtestfileにテキストを追加し、pushしましょう。以下のドキュメントを貼り付けてください。

 

Test File
Hello

 

 

貼り付けが完了しましたら、保存し、以下のコマンド打ちましょう。

 

git add testfile
git commit -m 'Add Text'
git push origin master

 

 

コマンド結果は以下です。

 

[ec2-user@ip-192-168-0-173 testrepo]$ git add testfile
[ec2-user@ip-192-168-0-173 testrepo]$ git commit -m 'Add Text'
[master 96ef217] Add Text
 Committer: EC2 Default User <ec2-user@ip-192-168-0-173.ap-southeast-1.compute.internal>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
[ec2-user@ip-192-168-0-173 testrepo]$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 292 bytes | 292.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To gitserver:/home/gitserver/git/testrepo.git
   209b635..96ef217  master -> master

 

 

■確認

クライアント2でtestfileを編集し、リモートリポジトリにpushしました。クライアント1側でファイルをpullし、ファイルの中身を確認してみます。使用コマンドは以下の通りです。

 

 

git pull origin master
ls
vi testfile

 

 

ファイルの中身を確認するために“vi”コマンドを使用していますが、catコマンドでも十分かと思います。コマンド結果は以下です。

 

[ec2-user@ip-192-168-0-71 testrepo]$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 272 bytes | 272.00 KiB/s, done.
From gitserver:/home/gitserver/git/testrepo
 * branch            master     -> FETCH_HEAD
   209b635..96ef217  master     -> origin/master
Updating 209b635..96ef217
Fast-forward
 testfile | 1 +
 1 file changed, 1 insertion(+)
[ec2-user@ip-192-168-0-71 testrepo]$ ls
testfile
[ec2-user@ip-192-168-0-71 testrepo]$ vi testfile

 

 

testfile中身

 

クライアント2で編集した内容がクライアント1側でも確認することができました。

 

今回の構築は以上です。

 

 

まとめ:EC2にGitサーバーを構築してみた

Gitを理解するためにEC2を利用しGitサーバーを構築してみました。Gitのリモートリポジトリを用意するには、Gitの理解だけでは不十分で、sshや認証についても知っている必要があることに気づかされました。

 

今回構築したGitサーバーはまだまだ実用に耐えないレベルかと思いますので、ちょくちょくパワーアップさせていきます。

 

 

参考リンク:Git公式HP
 

 

↓ほかの協栄情報メンバーもエンジニアツールに関する記事を公開しています。ぜひ参考にしてみてください。

 

■Ansibleのオフラインインストール方法(umemoto)
https://cloud5.jp/ansible-offline/

 

■会議ツール(Zoom,Teams,Chimeなど)でAirPodsやopencommの音量が調整できない問題を解消(dapeng)
https://cloud5.jp/win-airpods-opencomm-volume/

 

■DatadogでAWSインテグレーションを作成してみました。(CloudFormation)(小林 剛)
https://cloud5.jp/datadog-aws-integration/

Last modified: 2023-10-28

Author