➜ cd .ssh
➜ .ssh
複製代碼
➜ ssh-keygen -t rsa -C "youremail@email.com"
複製代碼
若是咱們 Mac 上面已經有了 ssh-key 再建立 ssh-key 的話,默認會在 ~/.ssh/ 目錄下生成 id_rsa 和 id_rsa.pub 兩個文件,若是不自定義,就會把原有的給覆蓋掉。爲了加以區分,咱們須要自定義一下生成的 key 的名字,後面的test_id_rsa爲你自定義的名字git
Enter file in which to save the key (/Users/liqiqi/.ssh/id_rsa): test_id_rsa
複製代碼
須要輸入兩次密碼,輸入密碼時是看不見的,這個密碼在你提交代碼到GitLab時會用到【注意:記住這個密碼,最簡單的方式就是設置的和gitLab帳戶登入密碼同樣,容易記住,固然也能夠不設置,直接回車 Enter】bash
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
複製代碼
Your identification has been saved in test_id_rsa.
Your public key has been saved in test_id_rsa.pub.
The key fingerprint is:
SHA256:mEqlhy9Ld4whw2VYRZPeT+VoVPFsfd/QfYQKcsGipq4 youremail@email.com
The key's randomart image is: +---[RSA 2048]----+ | .o+o.. .oo.| | o +.+ . o=o| | . +o = o =. X| | . *oo. . = .o=| | Bo= S + o| | ..* + . | | .+ + o | | ..+ . | | E.. | +----[SHA256]-----+ 複製代碼
(1)到上面這一步咱們已經建立好了 ssh-key,此時還須要將新的 ssh-key 添加到ssh agent ,由於默認只讀 id_rsa,首先查看一下已經添加進去的 ssh-key,當出現下面 這種狀況是說明 ssh agent 裏面並無把咱們新生產的 ssh-key添加進去dom
➜ ssh-add -l
The agent has no identities.
複製代碼
**(2)**能夠選擇把咱們生成的 ssh-key 添加進去,也能夠指定添加ssh
// 指定添加
➜ .ssh ssh-add ~/.ssh/test_id_rsa
Identity added: /Users/liqiqi/.ssh/test_id_rsa (/Users/liqiqi/.ssh/test_id_rsa)
//所有添加
ssh-add
Identity added: /Users/liqiqi/.ssh/test_id_rsa (/Users/liqiqi/.ssh/test_id_rsa)
複製代碼
**(3)**這時輸入下面指令就能看見咱們添加進去的 ssh-keyide
ssh-add -l
2048 SHA256:mEqlhy9Ld4whw2VYRZPeT+VoVPFsfd/QfYQKcsGipq4 /Users/liqiqi/.ssh/test_id_rsa (RSA)
複製代碼
在 ~/.ssh/ 目錄下會生成 test_id_rsa 和 test_id_rsa.pub 私鑰和公鑰。 咱們將 test_id_rsa.pub 中的內容複製到剪切板gitlab
➜ pbcopy < ~/.ssh/test_id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC0T+k7jtJ7qrJ1KORMHlW8AnXD3fVw5t3iK7FcOA1Nq6qLWkVEB8MXa/j9lmZzMEpOVjxAgmV7KhuMIG5EqiZndWcVr7PVaTX4s+CVLPP9HqzEOublAi1w+yx+KasgHq630XpfRpfIT1ZaCdGePU+jxLFw4bJIMK4+n523uANVZNnos2JpmgKZg54pLMqGxCpphmrvUqIVUndSMln+IXZb4ayt6JRxE9dSbNwJoftEoSsKabPICd/rkwBD+GYBdDEfSDJ1Epg3jtnyPQJiK/vp01QH0ZGUJkpzUzoUP959R9QZN1VLeKu1BzJR7D5lKhgINIhouDk5a2ZXjYjkK/BJbeIRtSi26lOs+sSXS3my7sO33WsP8HSbSr+F5wTnRQW0HiYLkm/YcHU+WKZful7kNl7j1C7eDNwtPR1t2z5OfJMQba+khlY/VakffC8ESr5S057eqDZtnZqP9qBE3wenSfngxqC6BP3AW+ObaamByB9Pv99WJEelmoCvILBaIhUDnnePxH9Keyt5OFmzKnnF4wCz6EWKfIIXC9eJxRJ0QtAaL8tBCW/mQZROtX57vQykXSLlRKHG2/8iquNp+GMMJZPeUeOupvVM9dknGnXQ74ZSpOzxjddUKXjfDczL309wP7pez5BTFnkVrYSQnR9SU6sa6KFeQ1oyDcy8sbv4qw==youremail@email.com
複製代碼
➜ ssh -T git@gitlab.youaredomain.work
Welcome to GitLab, xxx!
複製代碼