1 . 在Unix、Linux系统上产生SSH key。
(1)实验环境:
主机名称: C7201 --------Gitlab服务器
C7202 ---------模拟Gitlab服务器客户端
(2)实验步骤:
1)首先来检查一下是否已经存在ssh key。 执行以下命令来检查是否有文件id_rsa.pub或者id_dsa.pub.
ls ~/.ssh
2) 执行以下命令来产生SSH-KEY文件。
[root@c7202 ~]# ssh-keygen -t rsa -C "Comment for key" Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:zihLgXaDZXunp3BJP7uWH+gep1s0fMEnZXV9dzDcqlM Comment for key The key's randomart image is: +---[RSA 2048]----+ | .=++| | . o..B| | o + ..+| | = . . +E | | + = o S + .o | | . . = O o oo | | + = O.+ . | | . = +oB . | | . .oB+. |
2. 在Windows 主机上产生SSH-Key文件。
(1)在以下网址进行下载https://git-scm.com/downloads,安装,具体步骤(略)。
(2)打开Git命令行窗口。
(3)执行以下命令产生SSH-KEY文件
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/mike/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/mike/.ssh/id_rsa. Your public key has been saved in /c/Users/mike/.ssh/id_rsa.pub. The key fingerprint is: SHA256:us30pESKeTghRu8MfEMnX+WZJ4M+FjA5sqNrWuIKKIA mike@win10-001 The key's randomart image is: +---[RSA 2048]----+ | o. . | | . oo + o | | . oo..+ * . | |.o oo+ o . + | |E =.=.. S | |o..* * = . | |= ..B + o . | |+oo o * + | |++ . + . |
3 . 添加SSH key文件到Gitlab服务器
(1)找到如下位置。
(2)把我们第二步或者第三步产生的密钥公钥放在此处。
LINUX上查看公钥内容:
[root@c7202 ~]# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCugkcsdX4qe4cc5HjryGY7judYrqV4B688z3As1NYE/Pbn/RjFRmCzgKSzWRp7EN3SSMoN5yfdq84DUJ3lzzUC0k6+x7lL8Ma3uNMbpzLwdfGLWjMo0wB6ZIBuNEEgyfrfOg1xI9MWhaZKRlEKAW8ibhrx5GPojWbZHPDxy1y9a8Qsnufkqu1NgM9vnu8y2NWqFXGrZwbl8LAsCGwYr9JIqR5DJNklnNV5oWCZpdVtjeRljUPlPmF7d6+k7mrEdxIdhr7vtM6d1G1wBVqYv7DpMBZGy49xBT+V3fM66lOMO0wfTM4gTQQDvysWtchv2RzP1ViYuSrI94851OENxd2r Comment for key
WINDOWS 查看公钥内容:
$ cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4xHkRoc9QuEwdojNtB7Q1yRVF2PxsHeNdfAuMKnl2mqxoFKE3XXvKMyAm6ZKWxMeeuVB81lVB0xqkUKfOCCioMF/dtSglpnCWW4sxWVPSRPLD5NbY3Em3By7a/QuaYwe1O32kD6QdlPZrBuddbCh1gbUw5F46ta1X2qD3QWRC0J1ohv0SzH96b3U3bxyl/foCarLl7ZWIhe27+n/JX6KPDi/Hnmp7+Tz2w9wbnbcY31TDVgz9gg1d8UrO0ARramW+Ba5d4SPeV5z8HJwttxUjtf8HNBwKWZUgXOrUsMmj/+ZI5oYge3U2YWGUdqJxFcZRwOB/Lrhf4h6XBguv7cg1 mike@win10-001
4. 在本地创建第一个git 项目。
(1)打开windows下git的窗口终端或者LINUX下的终端。
(2)创建一个super-git文件夹,然后切换到该文件夹。
mike@win10-001 MINGW64 ~$ mkdir super-git mike@win10-001 MINGW64 ~$ cd super-git/
(3)使该文件夹作为Git项目 ,需要告诉Git去监控该文件夹。执行以下命令。
$ git init Initialized empty Git repository in C:/Users/mike/super-git/.git/
(4)创建一个文件并把它添加到库中,该文件名字命名为README.md
mike@win10-001 MINGW64 ~/super-git (master) $ echo "HELLO README" > README.md
(5)添加新文件到Git,执行以下命令。
$ git add README.md
(6)提交文件到本地库。
mike@win10-001 MINGW64 ~/super-git (master) $ git commit -m "Our first commit" [master (root-commit) d6c9c4a] Our first commit 1 file changed, 1 insertion(+) create mode 100644 README.md
6. 刻隆仓库和推送代码到仓库
(1)找到想要刻隆仓库的地址,样式如下:
(2)使用以下命令进行刻隆。
mike@win10-001 MINGW64 ~/cookbook $ git clone [email protected]:root/cookbook.git Cloning into 'cookbook'... The authenticity of host 'gitlab.aishangwei.net (192.168.2.201)' can't be established. ECDSA key fingerprint is SHA256:HvXTteW8UQ0MtxDAlMhL9XNOElAVV69d8nDsWjixrXI. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'gitlab.aishangwei.net,192.168.2.201' (ECDSA) to the list of known hosts. warning: You appear to have cloned an empty repository.
(3)进入我们刻隆的文件夹,可以通过以下命令查看是否是GIT文件夹。
$ git statusOn branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
(4)创建一个文件。
mike@win10-001 MINGW64 ~/cookbook/cookbook (master) $ echo "Hello Gitlab" >> README.md
(5)添加这个文件到stage状态。
mike@win10-001 MINGW64 ~/cookbook/cookbook (master) $ git add README.md warning: LF will be replaced by CRLF in README.md. The file will have its original line endings in your working directory.
(6)执行以下命令,提交这个改变。
mike@win10-001 MINGW64 ~/cookbook/cookbook (master) $ git commit -m "Added readme file" [master (root-commit) 53ec2ca] Added readme file 1 file changed, 1 insertion(+) create mode 100644 README.md
(7)把该文件推送到gitlab服务器上。
mike@win10-001 MINGW64 ~/cookbook/cookbook (master) $ git push -u origin master Counting objects: 3, done. Writing objects: 100% (3/3), 225 bytes | 112.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To gitlab.aishangwei.net:root/cookbook.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.