手把手教你搭建 Git 服务器
https://gitbook.cn/books/5e81adf58d6af944d1f26356/index.html
1. 服務器端安裝 Git
切換至 root 賬戶:
su root
看一下服務器有沒有安裝 Git,如果出現下面信息就說明是有 Git 的:
[root@instance-5fcyjde7 ~]# git
usage: git [--version] [--help] [-c name=value][--exec-path[=<path>]] [--html-path] [--man-path] [--info-path][-p|--paginate|--no-pager] [--no-replace-objects] [--bare][--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]<command> [<args>]
The most commonly used git commands are:add Add file contents to the indexbisect Find by binary search the change that introduced a bugbranch List, create, or delete branchescheckout Checkout a branch or paths to the working treeclone Clone a repository into a new directorycommit Record changes to the repositorydiff Show changes between commits, commit and working tree, etcfetch Download objects and refs from another repositorygrep Print lines matching a patterninit Create an empty Git repository or reinitialize an existing onelog Show commit logsmerge Join two or more development histories togethermv Move or rename a file, a directory, or a symlinkpull Fetch from and merge with another repository or a local branchpush Update remote refs along with associated objectsrebase Forward-port local commits to the updated upstream headreset Reset current HEAD to the specified staterm Remove files from the working tree and from the indexshow Show various types of objectsstatus Show the working tree statustag Create, list, delete or verify a tag object signed with GPG
'git help -a' and 'git help -g' lists available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
如果沒有 Git,就安裝一下,yum 安裝的版本默認是 1.8.3.1:
yum install git
安裝成功之后,看一下自己安裝的版本:
git --version
2. 服務器端設置 Git 賬戶
創建一個 Git 的 Linux 賬戶,這個賬戶只做 Git 私服的操作,也是為了安全起見。
如果不新創建一個 Linux 賬戶,在自己的常用的 Linux 賬戶下創建的話,哪天手抖 來一個 rm -rf * 操作,數據可全沒了。
這里 Linux Git 賬戶的密碼設置的盡量復雜一些,我這里為了演示,就設置成為 ‘gitpassword’。
adduser git
passwd gitpassword
然后就要切換成 Git 賬戶,進行后面的操作了:
[root@instance-5fcyjde7 ~]# su - git
看一下自己所在的目錄,是不是在 git 目錄下面:
[git@instance-5fcyjde7 ~]$ pwd
/home/git
3. 服務器端密鑰管理
創建 .ssh 目錄,如果 .ssh 已經存在了,可以忽略這一項。
為啥用配置 SSH 公鑰呢,同學們記不記得我急使用 GitHub 上傳上傳代碼的時候也要把自己的公鑰配置上 GitHub 上。
這也是方面每次操作 Git 倉庫的時候不用再去輸入密碼。
cd ~/
mkdir .ssh
進入 .ssh 文件下,創建一個 authorized_keys 文件,這個文件就是后面就是要放我們客戶端的公鑰。
cd ~/.ssh
touch authorized_keys
別忘了 authorized_keys 給設置權限,很多同學發現自己不能免密登錄,都是因為忘記了給 authorized_keys 設置權限:
chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys
接下來我們要把客戶端的公鑰放在 Git 服務器上,我們在回到客戶端,創建一個公鑰。
在我們自己的電腦上,有公鑰和私鑰。兩個文件分別是:id_rsa 和 id_rsa.pub。
- 如果是 Windows 系統公鑰私鑰的目錄在 C:\Users\用戶名.ssh 下。
- 如果是 Mac 或者 Linux, 公鑰和私鑰的目錄這里
cd ~/.ssh/, 如果發現自己的電腦上沒有公鑰私鑰,那就自己創建一個。
創建密鑰的命令:
ssh-keygen -t rsa
創建密鑰的過程中,一路點擊回車就可以了。不需要填任何東西。把公鑰拷貝到 Git 服務器上,將我們剛剛生成的 id_rsa.pub,拷貝到 Git 服務器的 /home/git/.ssh/ 目錄。
在 Git 服務器上,將公鑰添加到 authorized_keys 文件中:
cd /home/git/.ssh/
cat id_rsa.pub >> authorized_keys
如何看我們配置的密鑰是否成功呢, 在客戶點直接登錄 Git 服務器,看看是否是免密登錄:
ssh git@git 服務器 ip
例如:
ssh git@127.0.0.1
如果可以免密登錄,那就說明服務器端密鑰配置成功了。
4. 服務器端部署 Git 倉庫
我們在登錄到 Git 服務器端,切換為 Git 賬戶。 如果是 root 賬戶切換成 Git 賬戶:
su - git
如果是其他賬戶切換為 Git 賬戶:
sudo su - git
進入 git 目錄下:
cd ~/git
創建我們的第一個 Git 私服的倉庫,我們叫它為 world 倉庫。那么首先創建一個文件夾名為 world.git,然后進入這個目錄。
有同學問,為什么文件夾名字后面要放 .git, 其實不這樣命名也是可以的。但是細心的同學可能注意到,我們平時在 GitHub 上 git clone 其他人的倉庫的時候,倉庫名字后面,都是加上 .git 的。
例如下面這個例子,其實就是 GitHub 對倉庫名稱的一個命名規則,所以我們也遵守 GitHub 的命名規則。
git clone https://github.com/youngyangyang04/NoSQLAttack.git
所以我們的操作是:
[git@localhost git]# mkdir world.git
[git@localhost git]# cd world.git
初始化我們的 world 倉庫:
git init --bare
如果我們想創建多個倉庫,就在這里創建多個文件夾并初始化就可以了,和 world 倉庫的操作過程是一樣一樣的。
現在我們服務端的 Git 倉庫就部署完了,接下來就看看客戶端,如何使用這個倉庫呢。
5. 客戶端連接遠程倉庫
我們在自己的電腦上創建一個文件夾也叫做 world 吧。
其實這里命名是隨意的,但是我們為了和 Git 服務端的倉庫名稱保持同步。 這樣更直觀我們操作的是哪一個倉庫。
mkdir world
cd world
進入 world 文件,并初始化操作:
cd world
git init
在 world 目錄上創建一個測試文件,并且將其添加到 Git 版本管理中:
touch test
git add test
git commit -m "add test file"
將次倉庫和遠端倉庫同步:
git remote add origin git@git 服務器端的 ip:world.git
git push -u origin master
此時這個 test 測試文件就已經提交到我們的 Git 遠端私服上了。
6. Git 私服安全問題
這里有兩點安全問題。
6.1 Linux Git 的密碼不要泄露出去
否則,別人可以通過 ssh git@git 服務器 IP 來登錄到你的 Git 私服服務器上。當然了,這里同學們如果買的是云廠商的云服務器,如果有人惡意想通過嘗試不同密碼鏈接的方式來鏈接你的服務器,重試三次以上,這個客戶端的 IP 就會被封掉,同時郵件通知我們可以 IP 來自哪里。
所以大可放心,密碼只要我們不泄露出去,基本上不會有人同時不斷嘗試密碼的方式來登上我們的 Git 私服服務器。
6.2 私鑰文件 id_rsa 不要給別人
如果有人得到了這個私鑰,就可以免密碼登錄我們的 Git 私服上了,我相信大家也不至于把自己的私鑰主動給別人吧。
總結
以上是生活随笔為你收集整理的手把手教你搭建 Git 服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022-2028年中国遇水膨胀橡胶行业
- 下一篇: 2022-2028年中国耐二甲醚橡胶密封