创建git用户
生成ssh公钥
许多 Git 服务器都使用 SSH 公钥进行认证。 为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份。
linux 可以在本机运行ssh-keygen -t rsa生成密钥,把.pub文件拷到服务器上。
[root@iZ25r8k6ifuZ ~]# su tesla[tesla@iZ25r8k6ifuZ root]$ cd ~[tesla@iZ25r8k6ifuZ ~]$ mkdir .ssh[tesla@iZ25r8k6ifuZ ~]$ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/home/tesla/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /home/tesla/.ssh/id_rsa.Your public key has been saved in /home/tesla/.ssh/id_rsa.pub.The key fingerprint is:13:bf:75:ba:67:7f:0e:a0:47:7a:fe:25:bc:81:85:c3 tesla@iZ25r8k6ifuZThe key's randomart image is:+--[ RSA 2048]----+| || || . || o . . || S . E o || . O || + = = .|| + .o.|| o+oo+|+-----------------+[tesla@iZ25r8k6ifuZ ~]$ cd .ssh/[tesla@iZ25r8k6ifuZ .ssh]$ cat id_rsa.pub >> ~/.ssh/authorized_keysexit
添加用户到sudoers文件
tesla用户现在对一些文件夹没有操作权限,修改/etc/sudoers文件来改变他的权限。最高管理员用户用下面命令打开。
[root@iZ25r8k6ifuZ ~]# visudo然后我们在vim中找到下面这行root ALL=(ALL) ALL按i键开始插入,回车一下在下面一行加上tesla ALL=(ALL) ALL接着按下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,
cd /homemkdir gitrepochown git:git gitrepo接下来,创建我们的第一个git仓库:sample.git,cd gitreposudo git init --bare sample.gitInitialized empty Git repository in /teslaRepo/teslaProject.git/最后一步,把仓库所属用户改为git。chown -R git:git sample.git到此,git服务器搭建完毕。
PS:
以后每创建一个新的仓库,记得最后一步操作: 修改仓库所属用户为git。**
本地测试使用
你可以直接在服务器上进行本地测试,也可以直接用你的电脑来测试。下面我是使用自己的MBP来进行的测试。
localhost:~ okay$ cd Desktop/git/localhost:git okay$ mkdir teslaRepolocalhost:git okay$ cd teslaRepo/localhost:teslaRepo okay$ git initInitialized empty Git repository in /Users/okay/Desktop/git/teslaRepo/.git/localhost:teslaRepo okay$ git remote add origin tesla@123.57.159.74:/teslaRepo/teslaProject.git
上面的命令在本地创建了一个文件夹并添加了服务器上的远程仓库
localhost:teslaRepo okay$ touch a.txtlocalhost:teslaRepo okay$ git add a.txtlocalhost:teslaRepo okay$ git commit -m "init commit"[master (root-commit) d14cd3b] init commit1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 a.txt
git add . 提交当前目录下全部文件
上面的命令在本地创建了一个a.txt并在本地提交了一次
localhost:teslaRepo okay$ git push origin mastertesla@123.57.159.74's password:Counting objects: 3, done.Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To tesla@123.57.159.74:/teslaRepo/teslaProject.git* [new branch] master -> master
上面命令表示,将本地的master分支推送到origin主机的master分支。如果后者不存在,则会被新建。
下面我们在本地clone一次看下是否正确
七 本地clone
localhost:git okay$ mkdir tttlocalhost:git okay$ cd tttlocalhost:ttt okay$ git clone tesla@123.57.159.74:/teslaRepo/teslaProject.gitCloning into 'teslaProject'...tesla@123.57.159.74's password:remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0)Receiving objects: 100% (3/3), done.Checking connectivity... done.
clone完成,
