iOS習慣了CocoaPods管理了,需要用到git服務器,鑒于公司的代碼不開源等原因,故只能是自己動手搭建一個git服務器了。
首先是在Ubuntu的系統下打開終端Terminal(注意:命令行不包括符號$
,實踐時需要把example@email.com改成你自己的郵箱.)
- 安裝git
$ sudo apt-get install git
- 安裝ssh
$ sudo apt-get install openssh-server
$ sudo apt-get install openssh-client
- 添加一個新用戶git管理服務器,在輸入的命令以后會提示輸入密碼,這個密碼是指你現在新創建的git用戶的密碼,而并非是你當前登錄的用戶的密碼。
$ sudo adduser git
當提示如下的信息的時候表示成功了:
parallels@ubuntu:~$ sudo adduser git
[sudo] password for parallels:
Adding user `git' ...
Adding new group `git' (1002) ...
Adding new user `git' (1002) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
parallels@ubuntu:~$
- 創建ssh證書認證文件:
$ sudo mkdir /home/git/.ssh
$ sudo touch /home/git/.ssh/authorized_keys
- 在客戶端創建證書(注意這個步驟在客戶端進行,我在這以macOS Sierra做示例):打開Mac的終端Terminal:
(1). 安裝git
macOS Sierra系統安裝了Xcode以后默認是安裝git的,這個步驟可以跳過了;
(2). 安裝OpenSSL
以上兩個步驟完成后,在終端中執行以下的命令:
$ ssh-keygen -t rsa -C "example@email.com"
輸入上面的命令后,只需要按兩次回車即可:
Apple-iMac-215-Inch: Apple$ ssh-keygen -t rsa -C "example@email.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Apple/.ssh/id_rsa): Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/Apple/.ssh/id_rsa.
Your public key has been saved in /Users/Apple/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:4gwZOEFc5uaEFeAOYy+COT20qM8jPijkTkclXZZZ04o example@email.com
The key's randomart image is:
+---[RSA 2048]----+
| o+o=. o+o. |
| ..B. oo .. |
|o.*.=o . . |
|oO.*oo E . |
|*.=o+ . S |
|ooo. + . |
|=. . o |
|==o |
|+++. |
+----[SHA256]-----+
Apple-iMac-215-Inch: Apple$
看到打印信息的提示,id_rsa.pub就是我們需要的公鑰(yào)了。
- 修改authorized_keys文件的權限,讓它能夠被編輯。把需要訪問git服務器的客戶端公鑰id_rsa.pub(就是上一步步驟生成的文件)的內容復制到authorized_keys文件,這個操作在步驟10測試中詳細講解。
$ sudo chmod 777 /home/git/.ssh/authorized_keys
- 修改文件的權限,歸服務器管理員git用戶所有:
$ sudo chmod 700 /home/git
$ sudo chmod 700 /home/git/.ssh
$ sudo chmod 600 /home/git/.ssh/authorized_keys
$ sudo chown -R git:git /home/git
$ sudo chown -R git:git /home/git/.ssh
$ sudo chown -R git:git /home/git/.ssh/authorized_keys
- 為了安全考慮禁止登錄git服務器的shell
(1).先找到git-shell的位置:在我電腦上為/usr/bin/git-shell
$ which git-shell
(2).修改passwd的權限
進入/etc文件夾下,鼠標右擊選擇Open inTerminal選項
$ sudo chmod 777 /etc/passwd
(3).修改git的shell
用:/usr/bin/git-shell
把:git:x:1004:1004:,,,:/home/git:/bin/bash
改成:git:x:1004:1004:,,,:/home/git:/usr/bin/git-shell
- 創建Git倉庫并初始化倉庫
(1). 創建倉庫,進入home目錄下,鼠標右擊,選擇Open in Terminal
選項,執行以下命令就會在home目錄下創建了一個名為git.repo的文件夾作為倉庫。
$ sudo mkdir git.repo
(2). 初始化倉庫:
$ sudo git init --bare sample.git
- 測試
在Mac終端中執行以下git命令,能夠將服務器中的sample.git克隆到Mac本地,如果能成功則說明git服務器搭建成功了。注意:記得把IP10.211.55.7
改成自己的Ubuntu服務器的IP地址哦??:
$ git clone git@10.211.55.7:/home/git.repo/sample.git
/************************************分割線*******************************/
- 如果此時你想刪除這個用戶的話只需輸入:
$ sudo userdel -r git
終端輸出以下信息,這樣,就能刪除剛才創建的git用戶了:
parallels@ubuntu:~$ sudo userdel -r git
userdel: git mail spool (/var/mail/git) not found
parallels@ubuntu:~$
我還是Linux新手,關于更多的Linux文件操作的終端命令,請自行Google,我就不再班門弄斧了。