1、如果没有GIT先安装GIT:
$ sudo apt-get install git
2、创建一个git
用户,用来运行git
服务:
$ sudo adduser git
3、创建证书登录:
收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub
文件,把所有公钥导入到/home/git/.ssh/authorized_keys
文件里,一行一个。
4、初始化Git仓库:
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gittest$ sudo git init --bare git.git
[sudo] zyf 的密码:
5、把所有者改为GIT用户:
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gittest$ sudo chown -R git:git git.git
[sudo] zyf 的密码:
6、禁用git用户shell登录:
sudo gedit /etc/passwd
将
git:x:1001:1001:z,z,z,z,z:/home/git:/bin/bash
修改为
git:x:1001:1001:z,z,z,z,z:/home/git:/usr/bin/git-shell
7、创建客户端:
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gittest$ cd ..
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work$ mkdir gitclient
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work$ cd gitclient/
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git clone git@localhost:/gittest/git.git
正克隆到 'git'...
ssh: connect to host localhost port 22: Connection refused
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
问题一、ssh: connect to host localhost port 22: Connection refused
fatal: Could not read from remote repository.
原因:Ubuntu默认没有安装openssh-server
解决方法:
使用命令ps -e|grep ssh查看,如果 只有ssh-agent说明没有安装openssh-server,所以解决方法 是安装openssh-server
sudo apt-get install openssh-server
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
安装后再次使用ps -e|grep ssh查看:
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/6580pet/MTK6580_M_V2.34_base_pet$ ps -e|grep ssh
6329 ? 00:00:00 sshd
8、再次执行git clone
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git clone git@localhost:/gittest/git.git
正克隆到 'git'...
git@localhost's password:
Permission denied, please try again.
git@localhost's password:
fatal: '/gittest/git.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
问题二、
fatal: '/gittest/git.git' does not appear to be a git repository
原因:路径不对-_-
正确路径:git clone git@localhost:/home/zyf/work/gittest/git.git/
再次执行:
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git clone git@localhost:/home/zyf/work/gittest/git.git/
正克隆到 'git'...
git@localhost's password:
warning: 您似乎克隆了一个空仓库。
检查连接... 完成。
成功。
9、使用新建仓库:
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git remote -v
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ touch test.txt
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ echo test >> test.txt
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ cat test.txt
test
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git add test.txt
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git commit -m "first commit" test.txt
[master (根提交) e65ec15] first commit
1 file changed, 1 insertion(+)
create mode 100644 work/gitclient/test.txt
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git push
fatal: 没有配置推送目标。
或通过命令行指定 URL,或用下面命令配置一个远程仓库
git remote add <名称> <地址>
然后使用该远程仓库名执行推送
git push <名称>
10、添加远程仓库:
gitclient$ git remote add origin git@localhost:/home/zyf/work/gittest/git.git/
gitclient$ git remote -v
origin git@localhost:/home/zyf/work/gittest/git.git/ (fetch)
origin git@localhost:/home/zyf/work/gittest/git.git/ (push)
11、push到远程服务器
zyf@zyf-HP-Pavilion-m4-Notebook-PC:~/work/gitclient$ git push origin master
git@localhost's password:
对象计数中: 5, 完成.
写入对象中: 100% (5/5), 301 bytes | 0 bytes/s, 完成.
Total 5 (delta 0), reused 0 (delta 0)
To git@localhost:/home/zyf/work/gittest/git.git/
* [new branch] master -> master
12、附生成密钥
mkdir .ssh
cd .ssh
再创建 authorized_keys
touch authorized_keys
修改 .ssh 文件夹 和 authorized_keys 的权限
chmod 700 .ssh
chmod 600 authorized_keys
检查 配置文件
vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
生成密钥:
ssh-keygen -t rsa -C "xxx@xxx.com"
追加到 服务器 的 authorized_keys 文件中:
cat 个人id_rsa.pub >> /home/git/.ssh/authorized_keys