https://gitbook.cn/books/5e81adf58d6af944d1f26356/index.html
1. 服务器端安装 Git
切换至 root 账户:
su root
看一下服务器有没有安装 Git,如果出现下面信息就说明是有 Git 的:
[root@instance-5fcyjde7 ~]# git
usage: git [--version] [--help] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
The most commonly used git commands are:
add Add file contents to the index
bisect Find by binary search the change that introduced a bug
branch List, create, or delete branches
checkout Checkout a branch or paths to the working tree
clone Clone a repository into a new directory
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
fetch Download objects and refs from another repository
grep Print lines matching a pattern
init Create an empty Git repository or reinitialize an existing one
log Show commit logs
merge Join two or more development histories together
mv Move or rename a file, a directory, or a symlink
pull Fetch from and merge with another repository or a local branch
push Update remote refs along with associated objects
rebase Forward-port local commits to the updated upstream head
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
show Show various types of objects
status Show the working tree status
tag Create, list, delete or verify a tag object signed with GPG
'git help -a' and 'git help -g' lists available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
如果没有 Git,就安装一下,yum 安装的版本默认是 1.8.3.1:
yum install git
安装成功之后,看一下自己安装的版本:
git --version
2. 服务器端设置 Git 账户
创建一个 Git 的 Linux 账户,这个账户只做 Git 私服的操作,也是为了安全起见。
如果不新创建一个 Linux 账户,在自己的常用的 Linux 账户下创建的话,哪天手抖 来一个 rm -rf *
操作,数据可全没了。
这里 Linux Git 账户的密码设置的尽量复杂一些,我这里为了演示,就设置成为 ‘gitpassword’。
adduser git
passwd gitpassword
然后就要切换成 Git 账户,进行后面的操作了:
[root@instance-5fcyjde7 ~]# su - git
看一下自己所在的目录,是不是在 git 目录下面:
[git@instance-5fcyjde7 ~]$ pwd
/home/git
3. 服务器端密钥管理
创建 .ssh 目录,如果 .ssh 已经存在了,可以忽略这一项。
为啥用配置 SSH 公钥呢,同学们记不记得我急使用 GitHub 上传上传代码的时候也要把自己的公钥配置上 GitHub 上。
这也是方面每次操作 Git 仓库的时候不用再去输入密码。
cd ~/
mkdir .ssh
进入 .ssh 文件下,创建一个 authorized_keys 文件,这个文件就是后面就是要放我们客户端的公钥。
cd ~/.ssh
touch authorized_keys
别忘了 authorized_keys 给设置权限,很多同学发现自己不能免密登录,都是因为忘记了给 authorized_keys 设置权限:
chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys
接下来我们要把客户端的公钥放在 Git 服务器上,我们在回到客户端,创建一个公钥。
在我们自己的电脑上,有公钥和私钥。两个文件分别是:id_rsa 和 id_rsa.pub。
- 如果是 Windows 系统公钥私钥的目录在 C:\Users\用户名.ssh 下。
- 如果是 Mac 或者 Linux, 公钥和私钥的目录这里
cd ~/.ssh/
, 如果发现自己的电脑上没有公钥私钥,那就自己创建一个。
创建密钥的命令:
ssh-keygen -t rsa
创建密钥的过程中,一路点击回车就可以了。不需要填任何东西。把公钥拷贝到 Git 服务器上,将我们刚刚生成的 id_rsa.pub,拷贝到 Git 服务器的 /home/git/.ssh/ 目录。
在 Git 服务器上,将公钥添加到 authorized_keys 文件中:
cd /home/git/.ssh/
cat id_rsa.pub >> authorized_keys
如何看我们配置的密钥是否成功呢, 在客户点直接登录 Git 服务器,看看是否是免密登录:
ssh git@git 服务器 ip
例如:
ssh [email protected]
如果可以免密登录,那就说明服务器端密钥配置成功了。
4. 服务器端部署 Git 仓库
我们在登录到 Git 服务器端,切换为 Git 账户。 如果是 root 账户切换成 Git 账户:
su - git
如果是其他账户切换为 Git 账户:
sudo su - git
进入 git 目录下:
cd ~/git
创建我们的第一个 Git 私服的仓库,我们叫它为 world 仓库。那么首先创建一个文件夹名为 world.git,然后进入这个目录。
有同学问,为什么文件夹名字后面要放 .git, 其实不这样命名也是可以的。但是细心的同学可能注意到,我们平时在 GitHub 上 git clone
其他人的仓库的时候,仓库名字后面,都是加上 .git 的。
例如下面这个例子,其实就是 GitHub 对仓库名称的一个命名规则,所以我们也遵守 GitHub 的命名规则。
git clone https://github.com/youngyangyang04/NoSQLAttack.git
所以我们的操作是:
[git@localhost git]# mkdir world.git
[git@localhost git]# cd world.git
初始化我们的 world 仓库:
git init --bare
如果我们想创建多个仓库,就在这里创建多个文件夹并初始化就可以了,和 world 仓库的操作过程是一样一样的。
现在我们服务端的 Git 仓库就部署完了,接下来就看看客户端,如何使用这个仓库呢。
5. 客户端连接远程仓库
我们在自己的电脑上创建一个文件夹也叫做 world 吧。
其实这里命名是随意的,但是我们为了和 Git 服务端的仓库名称保持同步。 这样更直观我们操作的是哪一个仓库。
mkdir world
cd world
进入 world 文件,并初始化操作:
cd world
git init
在 world 目录上创建一个测试文件,并且将其添加到 Git 版本管理中:
touch test
git add test
git commit -m "add test file"
将次仓库和远端仓库同步:
git remote add origin git@git 服务器端的 ip:world.git
git push -u origin master
此时这个 test 测试文件就已经提交到我们的 Git 远端私服上了。
6. Git 私服安全问题
这里有两点安全问题。
6.1 Linux Git 的密码不要泄露出去
否则,别人可以通过 ssh git@git 服务器 IP
来登录到你的 Git 私服服务器上。当然了,这里同学们如果买的是云厂商的云服务器,如果有人恶意想通过尝试不同密码链接的方式来链接你的服务器,重试三次以上,这个客户端的 IP 就会被封掉,同时邮件通知我们可以 IP 来自哪里。
所以大可放心,密码只要我们不泄露出去,基本上不会有人同时不断尝试密码的方式来登上我们的 Git 私服服务器。
6.2 私钥文件 id_rsa 不要给别人
如果有人得到了这个私钥,就可以免密码登录我们的 Git 私服上了,我相信大家也不至于把自己的私钥主动给别人吧。