自己搭建git 代码服务器

使用git服务器的工程师都需要生成一个ssh的公钥

~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
……………………
……………………
然后一律按回车,完成后会在home目录下的.ssh目录中生成两个文件id_rsa(私有密钥)和id_rsa.pub(公钥)。
将生成的公钥id_rsa.pub发给git仓库的管理者。
 
然后在服务器端:
sudo adduser git
su git
cd ~
mkdir .ssh
在git用户新建一个.ssh的目录,将开发者的SSH公钥添加到git这个用户的authorized_keys这个文件当中。如有zhangsan,lisi两个开发者。
cat /tmp/id_rsa_zhangsan.pub >> ~/.ssh/authorized_keys
cat /tmp/id_rsa_lisi.pub >> ~/.ssh/authorized_keys
 
建立一个空的仓库
cd work
mkdir project.git
cd project.git
git --bare init
使用--bare会初始化一个不带任何工作目录的仓库,不会看到工程源码。
 
开发者端:
若没有仓库的情况下,需要首先新建一个仓库。在需要建立仓库的,目录下执行以下的命令。
cd my_project
git init
git add . (将所有代码加入工程管理)
git commit -m "xxx project initial commit"
然后执行:git push origin master
如果出现以下错误:

The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is eb:e3:e2:36:84:64:23:f2:b8:7b:a0:4c:ef:d4:9e:7d.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.

执行一下:$ssh localhost -o StrictHostKeyChecking=no

(用ssh登录一下就好使,就不会弹出The authenticity of host 'localhost (::1)' can't be established.的错误了。)

然后再退出ssh登录。

然后:

a@ubuntu:~/linux-2.6$ git push origin master
Counting objects: 56913, done.
Compressing objects: 100% (56081/56081), done.
Writing objects: 100% (56913/56913), 150.96 MiB | 4.08 MiB/s, done.
Total 56913 (delta 4433), reused 0 (delta 0)
To git@localhost:/home/git/work/project.git
* [new branch] master -> master
a@ubuntu:~/linux-2.6$ ls

就好了。

 

小组内开发者b下载代码时:

b@ubuntu:~$ git clone git@localhost:/home/git/work/project.git
Initialized empty Git repository in /home/b/project/.git/
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is eb:e3:e2:36:84:64:23:f2:b8:7b:a0:4c:ef:d4:9e:7d.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
fatal: The remote end hung up unexpectedly
b@ubuntu:~$

同样执行一下:
b@ubuntu:~$ ssh localhost -o StrictHostKeyChecking=no

然后:

b@ubuntu:~$ git clone git@localhost:/home/git/work/project.git
Initialized empty Git repository in /home/b/project/.git/
git@localhost's password:
remote: Counting objects: 56913, done.
remote: Compressing objects: 100% (51648/51648), done.
remote: Total 56913 (delta 4433), reused 56913 (delta 4433)
Receiving objects: 100% (56913/56913), 150.96 MiB | 13.48 MiB/s, done.
Resolving deltas: 100% (4433/4433), done.
Checking out files: 100% (53646/53646), done.
b@ubuntu:~$ ls
examples.desktop project
b@ubuntu:~$ ls
examples.desktop project
b@ubuntu:~$
b@ubuntu:~$
b@ubuntu:~$ cd project/
b@ubuntu:~/project$ ls
arch crypto include kernel net security
block Documentation init lib README sound
certs drivers ipc MAINTAINERS REPORTING-BUGS tools
COPYING firmware Kbuild Makefile samples usr
CREDITS fs Kconfig mm scripts virt

分析 ssh localhost -o StrictHostKeyChecking=no可能不会检查公钥的正确性就让登录了。

 
posted @ 2016-05-25 09:58  polo2013  阅读(823)  评论(0编辑  收藏  举报