就是如何搭建git服务端(centos7)和客户端(mac)
服务端安装git,先远程仓库(remote),在centos7上操作
在 https://www.kernel.org/pub/software/scm/git/ 找一个合适的,下载和编译
wget https://www.kernel.org/pub/software/scm/git/git-2.9.4.tar.gz
tar -zvxf git-2.9.4.tar.gz
cd git-2.9.4
./configure
maker && maker install
git config username "";
git config email "";
如果在maker的时候报错了,先执行一下安装,按报错提示装就就好
yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel
设置用户,就是git的登录账户cross
adduser cross
passwd 密码
找个地方创建项目目录,我建在/var下,并给cross加上权限
cd /var
mkdir githome
chown cross githome/
cd githome
开始创建git服务端,加权限
git init --bare my-project.git
chown -R cross my-project.git
服务端先就这么多
创建本地仓库(Repository),mac上操作
在mac创建一个存放代码的,
mkdir githome
cd githome
mkdir my-project
cd my-project
设置远程仓库地址
git init
git remote add origin cross@服务器ip:/var/githome/my-porject.git
测试执行
git remote -v
,显示如下就正常了
origin cross@服务器IP:/var/githome/server_sword.git (fetch)
origin cross@服务器IP:/vorgiar/githome/server_sword.git (push)
如果写错了怎么办?先删再加,删除如下
git remote rm origin
提交代码
git add .
git commit -m '备注'
git push origin master
然后输入登录密码提交完成.再以后就要看git的命令了.
接下来的问题是,如何能提交时候不写密码
使用ssh-keygen生成公钥与密钥,将本机的公钥复制到远程机器的authorized_keys文件中,然后ssh user@ip直接登录
1 在本机使用ssh-keygen生成公钥密钥对
➜ .ssh ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wxj/.ssh/id_rsa): // 直接enter
Enter passphrase (empty for no passphrase): // 直接enter
Enter same passphrase again: // 直接enter
Your identification has been saved in /Users/wxj/.ssh/id_rsa.
Your public key has been saved in /Users/wxj/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:EEyAb1rbqtktM493R10Tmjsnq4Z6X7pRYWg7VcskZ/o wxj@bogon
The key's randomart image is:
+---[RSA 2048]----+
| ..+o . = |
| . .. . O.. |
| . . o =oo. |
| + . . +ooo |
| + o S o..oE. |
| . . . .o= . |
| . o. .= |
| o=o. + o+. |
| o.o*++ +=o |
+----[SHA256]-----+
➜ .ssh ll -a
total 24
drwx------ 5 wxj staff 170B 5 26 18:35 .
drwxr-xr-x+ 157 wxj staff 5.2K 5 26 18:35 ..
-rw------- 1 wxj staff 1.7K 5 26 18:35 is_rsa
-rw-r--r-- 1 wxj staff 391B 5 26 18:35 is_rsa.pub
...
2 用ssh-copy-id将公钥复制到远程机器中
➜ .ssh ssh-copy-id root@59.110.230.99
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/wxj/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@59.110.230.99's password: // 登录密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@59.110.230.99'"
and check to make sure that only the key(s) you wanted were added.
3 直接就可以登录了
ssh root@59.110.230.99
总有操作错误的时候
1. 在服务端要先有文件 ~/.ssh/authorized_keys,没有就创建一个,在要isa登录的用户下,执行以下创建,切换用户 `su 用户名`
mkdir ~/.ssh && touch ~/.ssh/authorized_keys
然后一定要加权限
chmod 700 ~/.ssh/
chmod 600 ~/.ssh/authorized_keys
2. 在生成公钥密钥的时候输入了key=cross,那就上传的时候这样
.ssh ssh-copy-id -i ~/.ssh/cross.pub root@59.110.230.99
登录的时候
ssh -i ~/.ssh/root root@59.110.230.99
3. 可能有些错误是这里引起的,在服务器打开ssh的配置文件 `vim /etc/ssh/sshd_config`,检查开启以下内容
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
重启服务
systemctl restart sshd
4. 在生成公钥密钥的时候输入了密码,那登录的时候就需要再次输入一遍密码.就是这个设置的密码
那么,cross用户按上述一样的操作,就免去了每次push都要录入密码的痛苦.