Git中认识SSH

本文深入解析SSH认证机制,包括git服务器的SSH配置流程,从客户端逻辑到服务器认证过程,以及常见异常排查方法。涵盖SSH配置、公私钥生成、授权密钥设置及HTTPS与SSH协议在GitLab中的应用。

说明:
- 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将采取口令认证

  • ssh git@git.xxx.com

    git@git.xxx.com’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中找到匹配的项,于是采取口令认证

  • ssh git@git.xxx.com

    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

  1. ssh-keygen -t rsa -C “comment” -f filename
  2. 添加 xxx.pub 至 服务端(gitlab)上
  3. 配置 .ssh/config 添加 对应host的配置
  4. 使用

gitlab 之 HTTPS

  1. git config --global http.sslVerify “false”
  2. 正常使用
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值