$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel $ yum install git
接下来咱们 建立一个git用户组和用户,用来运行git服务:
$ groupadd git $ useradd git -g git
收集全部须要登陆的用户的公钥,公钥位于id_rsa.pub文件中,把咱们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。git
若是没有该文件建立它:github
$ cd /home/git/ $ mkdir .ssh $ chmod 755 .ssh $ touch .ssh/authorized_keys $ chmod 644 .ssh/authorized_keys
首先咱们选定一个目录做为Git仓库,假定是/home/gitrepo/learngit.git,在/home/gitrepo目录下输入命令:shell
$ cd /home $ mkdir gitrepo $ chown git:git gitrepo/ $ cd gitrepo $ git init --bare learngit.git Initialized empty Git repository in /home/gitrepo/learngit.git/
以上命令Git建立一个空仓库,服务器上的Git仓库一般都以.git结尾。而后,把仓库所属用户改成git:
$ chown -R git:git learngit.git
$ git clone git@192.168.1.101:/home/gitrepo/learngit.git Cloning into 'learngit'... warning: You appear to have cloned an empty repository. Checking connectivity... done.
可能会提示错误 以下信息:
$ git clone git@192.168.8.34:/data/git/learngit.git Cloning into 'learngit'... The authenticity of host '192.168.8.34 (192.168.8.34)' can't be established. RSA key fingerprint is 2b:55:45:e7:4c:29:cc:05:33:78:03:bd:a8:cd:08:9d. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.8.34' (RSA) to the list of known hosts. git@192.168.8.34's password:
这里两点须要注意:第一,当你第一次使用Git的clone或者push命令链接GitHub时,会获得一个警告:缓存
The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established. RSA key fingerprint is xx.xx.xx.xx.xx. Are you sure you want to continue connecting (yes/no)?
这是由于Git使用SSH链接,而SSH链接在第一次验证GitHub服务器的Key时,须要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车便可。安全
Git会输出一个警告,告诉你已经把GitHub的Key添加到本机的一个信任列表里了:bash
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
这个警告只会出现一次,后面的操做就不会有任何警告了。
若是你实在担忧有人冒充GitHub服务器,输入yes前能够对照GitHub的RSA Key的指纹信息是否与SSH链接给出的一致。
第二,这里提示你输入密码才能clone,固然若是你知道密码,能够键入密码来进行clone,可是更为常见的方式,是利用SSH的公钥来完成验证。服务器
五、建立SSH Key
首先在用户主目录下,看看有没有.ssh目录,若是有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,若是已经有了,可直接跳到下一步。若是没有,打开Shell(Windows下打开Git Bash),建立SSH Key:app
$ ssh-keygen -t rsa -C "youremail@example.com"
你须要把邮件地址换成你本身的邮件地址,而后一路回车,使用默认值便可,因为这个Key也不是用于军事目的,因此也无需设置密码。ssh
若是一切顺利的话,能够在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,能够放心地告诉任何人。curl
六、Git服务器打开RSA认证
而后就能够去Git服务器上添加你的公钥用来验证你的信息了。在Git服务器上首先须要将/etc/ssh/sshd_config中将RSA认证打开,即:
1.RSAAuthentication yes 2.PubkeyAuthentication yes 3.AuthorizedKeysFile .ssh/authorized_keys
这里咱们能够看到公钥存放在.ssh/authorized_keys文件中。因此咱们在/home/git下建立.ssh目录,而后建立authorized_keys文件,并将刚生成的公钥导入进去。
而后再次clone的时候,或者是以后push的时候,就不须要再输入密码了:
Zhu@XXX/E/testgit/8.34 $ git clone git@192.168.8.34:/data/git/learngit.git Cloning into 'learngit'... warning: You appear to have cloned an empty repository. Checking connectivity... done.
七、禁用git用户的shell登录
出于安全考虑,第二步建立的git用户不容许登陆shell,这能够经过编辑/etc/passwd文件完成。找到相似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
最后一个冒号后改成:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样,git用户能够正常经过ssh使用git,但没法登陆shell,由于咱们为git用户指定的git-shell每次一登陆就自动退出。
经常使用命令:
git init // 初始化版本库
git add . // 添加文件到版本库(只是添加到缓存区),.表明添加文件夹下全部文件
git commit -m "first commit" // 把添加的文件提交到版本库,并填写提交备注
到目前为止,咱们完成了代码库的初始化,但代码是在本地,尚未提交到远程服务器,因此关键的来了,要提交到就远程代码服务器,进行如下两步:
git remote add origin git@192.168.1.101:/home/gitrepo/runoob.git (你的远程库地址)// 把本地库与远程库关联
git push -u origin master // 第一次推送时
git push origin master // 第一次推送后,直接使用该命令便可推送修改
把本地库的内容推送到远程。使用 git push命令,其实是把当前分支master推送到远程。执行此命令后会要求输入用户名、密码,验证经过后即开始上传。 说明:用户名密码须要经过命令 ssh-keygen -t rsa -C “xxxxxx@qq.com”进行建立,而且要把获得的秘钥(公钥)文件放到git服务器上,这样才有权限进行代码推送