說明:
- git服務器:git.xxx.com
- 認證方式:ssh
- ssh key:.ssh/id_rsa
筆者個人理解的 ssh {user}@{server} 過程:
// Host git.xxx.com
// User {other}
// IdentityFile ~/.ssh/id_rsa
// this is client logic
config = find_config(server)
// config: {"User": other, "IdentityStr": read("IdentityFile")} .etc
if config is not null:
body = {"to": user}.extend(config)
request(server, body)
body = receive(request)
user, other, IdentityStr = body["to"], config["User"], config["IdentityStr"]
// this is server logic
auth_keys = read("/home/$other/.ssh/authorized_keys")
if IdentityStr is not null && IdentityStr in auth_keys:
auth(IdentityStr, random())
else:
response(server_public_key, "please input password")
以下現象理解
未配置.ssh/config
-
ssh {user}@git.xxx.com
{user}@git.xxx.com’s password:
Client未找到git.xxx.com的配置信息,即發送空請求,Server將採取口令認證
-
[email protected]’s password:
Client未找到git.xxx.com的配置信息,即發送空請求,Server將採取口令認證
已配置config
-
ssh {user}@git.xxx.com
{user}@git.xxx.com’s password:
Client找到git.xxx.com的配置信息,併發送包含認證信息的請求
但是Server端沒有在/home/{user}/.ssh/authorized_keys中找到匹配的項,於是採取口令認證 -
PTY allocation request failed on channel 0
Welcome to GitLab, @{user}!
Connection to git.xxx.com closed.Client找到git.xxx.com的配置信息,併發送包含認證信息的請求
並且Server端在/home/git/.ssh/authorized_keys中找到匹配的項,於是公私鑰認證,最後認證通過
ssh 命令
ssh [-i identity_file] [-l login_name] [user@]host[:port]
注意點
當ssh出現異常時,通常注意檢查一下幾點
- local: config(登入配置):登入至哪個用戶,以什麼身份登入,登入時使用的認證私鑰
- server: authorized_keys(是否 允許 認證)
- local: know_hosts(服務器IP調整時)
gitlab使用時的配置
gitlab開始使用時 – 注意協議:SSH | HTTPS
gitlab 之 SSH
- ssh-keygen -t rsa -C “comment” -f filename
- 添加 xxx.pub 至 服務端(gitlab)上
- 配置 .ssh/config 添加 對應host的配置
- 使用
gitlab 之 HTTPS
- git config --global http.sslVerify “false”
- 正常使用