2016年2月6日 星期六

在AWS建立Git Server

目的:在AWS 上架設Git Server,讓使用者透過ssh協作
步驟:
我辦了四個AWS Server running on ubuntu
棕色表示User1~3 ,藍色的是git server
1.進入local machine 的.ssh forlder
>> cd ~/.ssh
2.產生public Key
>>ssh-keygen -f [username] //我取名為git
3.複製public Key
>>cat [username].pub



4.登入git server
5.另外生成git user
>>sudo adduser git --disabled-password
>>sudo su -- git

7.在/home/git下產生.ssh,用來設置public key
>>mkdir .ssh
8.設定public key
>>echo "貼上第三步產生的一大串密碼" >> ~/.ssh/authorized_keys

 9.產生git repo
>>mkdir git.git 
10進入git.git並初始化
>>cd git.git
>>git init --bare --shared

這樣就初步完成囉~

接著就可以在local machine pull & push
11.為了方便,可以在local machine設定 ssh config
>>cd ~/.ssh
>>nano config
12.開啟config後,加入以下文字
>>
Host [Git]  #取自己要的名字
Hostname [Server ip]
User git #使用者名稱,剛剛設定為git (第五步)

IdentityFile ~/.ssh/[username] #你的key(第三步)
13.上述步驟主要是為了要簡化ssh指令
也可以直接使用git 登入 AWS Server
>>ssh Git //第12步的Host name

14.成功使用git指令
>>git clone Git:/home/git/git.git 


備註:
1.記得在git server要用adduser建立新的系統使用者 git,adduser會在/home下建立/home/git,這樣可以把系統用戶與git用戶分離,比較安全
2.git server要記得切換用戶到git,接著才在/home/git下建立/home/git/.ssh(User的public key放在/home/git/.ssh/authorized_keys中),因為這樣其餘User要透過ssh連線時系統才會在正確地方比對Key,放錯地方會有登入權限不足的問題喔
3.git的repo有兩種,要仔細確認喔~
What is a bare git repository?
基本Git架設教學參考網址:Linux 架設使用 SSH 共享存取的 Git Server
較為完整的教學:Setting up an AWS EC2 Instance with LAMP and GIT

我當初想用git的主要原因只是要上傳server code :P,後續還會一些設定要做,繼續學習囉OTZ

沒有留言:

張貼留言