创建git用户

useradd git

生成ssh公钥

许多 Git 服务器都使用 SSH 公钥进行认证。 为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份。
linux 可以在本机运行ssh-keygen -t rsa生成密钥,把.pub文件拷到服务器上。

  1. [root@iZ25r8k6ifuZ ~]# su tesla
  2. [tesla@iZ25r8k6ifuZ root]$ cd ~
  3. [tesla@iZ25r8k6ifuZ ~]$ mkdir .ssh
  4. [tesla@iZ25r8k6ifuZ ~]$ ssh-keygen -t rsa
  5. Generating public/private rsa key pair.
  6. Enter file in which to save the key (/home/tesla/.ssh/id_rsa):
  7. Enter passphrase (empty for no passphrase):
  8. Enter same passphrase again:
  9. Your identification has been saved in /home/tesla/.ssh/id_rsa.
  10. Your public key has been saved in /home/tesla/.ssh/id_rsa.pub.
  11. The key fingerprint is:
  12. 13:bf:75:ba:67:7f:0e:a0:47:7a:fe:25:bc:81:85:c3 tesla@iZ25r8k6ifuZ
  13. The key's randomart image is:
  14. +--[ RSA 2048]----+
  15. | |
  16. | |
  17. | . |
  18. | o . . |
  19. | S . E o |
  20. | . O |
  21. | + = = .|
  22. | + .o.|
  23. | o+oo+|
  24. +-----------------+
  25. [tesla@iZ25r8k6ifuZ ~]$ cd .ssh/
  26. [tesla@iZ25r8k6ifuZ .ssh]$ cat id_rsa.pub >> ~/.ssh/authorized_keys
  27. exit

添加用户到sudoers文件

tesla用户现在对一些文件夹没有操作权限,修改/etc/sudoers文件来改变他的权限。最高管理员用户用下面命令打开。

  1. [root@iZ25r8k6ifuZ ~]# visudo
  2. 然后我们在vim中找到下面这行
  3. root ALL=(ALL) ALL
  4. i键开始插入,回车一下在下面一行加上
  5. tesla ALL=(ALL) ALL
  6. 接着按下esc键,输入 :wq ,回车保存退出

客户端创建密钥并上传

看清楚了,要在客户端运行如下命令。
ssh-keygen -t rsa -C “your_email”
该命令会产生两个文件: id_rsa对应私钥,id_rsa.pub对应公钥。
将id_rsa.pub中的内容写到服务器的authorized_keys文件中。
如果有多个客户端,那么在authorized_keys文件中,一行保存一个客户端的公钥。

创建git仓库

为了方便管理,所有的git仓库都置于同一目录下,假设为/home/gitrepo,

  1. cd /home
  2. mkdir gitrepo
  3. chown git:git gitrepo
  4. 接下来,创建我们的第一个git仓库:sample.git
  5. cd gitrepo
  6. sudo git init --bare sample.git
  7. Initialized empty Git repository in /teslaRepo/teslaProject.git/
  8. 最后一步,把仓库所属用户改为git
  9. chown -R git:git sample.git
  10. 到此,git服务器搭建完毕。

PS:
以后每创建一个新的仓库,记得最后一步操作: 修改仓库所属用户为git。**

本地测试使用

你可以直接在服务器上进行本地测试,也可以直接用你的电脑来测试。下面我是使用自己的MBP来进行的测试。

  1. localhost:~ okay$ cd Desktop/git/
  2. localhost:git okay$ mkdir teslaRepo
  3. localhost:git okay$ cd teslaRepo/
  4. localhost:teslaRepo okay$ git init
  5. Initialized empty Git repository in /Users/okay/Desktop/git/teslaRepo/.git/
  6. localhost:teslaRepo okay$ git remote add origin tesla@123.57.159.74:/teslaRepo/teslaProject.git

上面的命令在本地创建了一个文件夹并添加了服务器上的远程仓库

  1. localhost:teslaRepo okay$ touch a.txt
  2. localhost:teslaRepo okay$ git add a.txt
  3. localhost:teslaRepo okay$ git commit -m "init commit"
  4. [master (root-commit) d14cd3b] init commit
  5. 1 file changed, 0 insertions(+), 0 deletions(-)
  6. create mode 100644 a.txt

git add . 提交当前目录下全部文件
上面的命令在本地创建了一个a.txt并在本地提交了一次

  1. localhost:teslaRepo okay$ git push origin master
  2. tesla@123.57.159.74's password:
  3. Counting objects: 3, done.
  4. Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
  5. Total 3 (delta 0), reused 0 (delta 0)
  6. To tesla@123.57.159.74:/teslaRepo/teslaProject.git
  7. * [new branch] master -> master

上面命令表示,将本地的master分支推送到origin主机的master分支。如果后者不存在,则会被新建。
下面我们在本地clone一次看下是否正确
七 本地clone

  1. localhost:git okay$ mkdir ttt
  2. localhost:git okay$ cd ttt
  3. localhost:ttt okay$ git clone tesla@123.57.159.74:/teslaRepo/teslaProject.git
  4. Cloning into 'teslaProject'...
  5. tesla@123.57.159.74's password:
  6. remote: Counting objects: 3, done.
  7. remote: Total 3 (delta 0), reused 0 (delta 0)
  8. Receiving objects: 100% (3/3), done.
  9. Checking connectivity... done.

clone完成,