windows git 使用的一些问题和解决方案

邱秋 • 2021年11月17日 • 阅读:518 • git github windows

错误场景1

Permissions for "xxx" are too open

就是私钥权限太大的问题,在linux里面只要给定对应的权限即可

For Mac/Linux

chmod 755 ~/.ssh/  
chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub   
chmod 644 ~/.ssh/known_hosts  

For Window

如有以下提示说明ssh 服务没启动

Error connecting to agent: No such file or directory
# OR
Could not open a connection to your authentication agent

启动ssh 服务(管理员权限运行)

Set-Service -Name ssh-agent -StartupType automatic
Start-Service ssh-agent

解决办法 (去掉特定权限)

  • 选中私钥文件,alt+enter打开属性
  • 选择安全选项卡,点击高级,将所有者改为自己的账号并禁用继承(从此对象中删除所有已继承的权限),点击确定。

但是感觉图形化操作,各种权限一堆的,导致执行不下去。

所以可以执行命令,亲测可行

In CMD

# 设置 Key 文件变量(路径可以修改):
Set Key="%UserProfile%\.ssh\id_rsa"

# 移除继承:
Icacls %Key% /c /t /Inheritance:d

# 将所有权设置为当前用户:
# Key's within %UserProfile%:
Icacls %Key% /c /t /Grant %UserName%:F

# Key's outside of %UserProfile%:
TakeOwn /F %Key%
Icacls %Key% /c /t /Grant:r %UserName%:F

# 删除除所有者之外的所有用户:
Icacls %Key% /c /t /Remove:g "Authenticated Users" BUILTIN\Administrators BUILTIN Everyone System Users

# 验证:
Icacls %Key%

# 删除变量:
set "Key="

In PowerShell:

# 设置 Key 文件变量(路径可以修改):
New-Variable -Name Key -Value "$env:UserProfile\.ssh\id_rsa"

# 移除继承:
Icacls $Key /c /t /Inheritance:d

# 将所有权设置为当前用户:
# Key's within $env:UserProfile:
Icacls $Key /c /t /Grant ${env:UserName}:F

# Key's outside of $env:UserProfile:
TakeOwn /F $Key
Icacls $Key /c /t /Grant:r ${env:UserName}:F

# 删除除所有者之外的所有用户:
Icacls $Key /c /t /Remove:g Administrator "Authenticated Users" BUILTIN\Administrators BUILTIN Everyone System Users

# 验证:
Icacls $Key

# 删除变量:
Remove-Variable -Name Key

错误场景2

Permission denied (publickey)

说白了就是私钥没有生效,我TM明明已经ssh-add了呀

前提

  • 当然已经生成了公钥和私钥,
  • 并且已经在各大平台添加了对应的密钥key,
  • 已经执行了ssh-add(其实是没有生效的)
  • bash 下是没问题的,因为我非要折腾window10自带的 windows terminal

尝试了各种姿势, 但是这个逼,无动于衷...

执行

ssh -vT git@github.com

看到他在尝试使用默认的路径下的私钥:

debug1: Will attempt key: C:\\Users\\chuchur/.ssh/id_rsa
debug1: Will attempt key: C:\\Users\\chuchur/.ssh/id_dsa
debug1: Will attempt key: C:\\Users\\chuchur/.ssh/id_ecdsa
debug1: Will attempt key: C:\\Users\\chuchur/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\chuchur/.ssh/id_xmss

没这个习惯都把私钥叫id_rsa这个名字, 通常也不会放在 ~/.ssh目录下 , 但是他默认就在那儿=>C盘,我非要放D盘怎么弄?

解决方法

方案一

老老实实的把私钥名字改成 id_rsa,然后copy 到 ~/.ssh 目录下 ,那我多个私钥,怎么办不可能都叫 id_rsa,

在.ssh 目录下新建 config文件,内容如下

#公司的git地址
Host git.***.com  
User git
Hostname git.***.com  #公司的git地址
IdentityFile ~/.ssh/id_rsa  #访问公司git的SSH KEY
Port   ***  #公司的git端口

Host github.com
User git
Hostname github.com #github的地址
IdentityFile ~/.ssh/id_rsa_github  #访问github的SSH KEY

方案二

解决我非要放D盘,修改系统环境变量 win+R = > sysdm.cpl => Enter => 高级=> 环境变量 => 新建=>

变量名:HOME 变量值:D:\User\chuchur

确定,完成 ,那么你的.ssh 就跑到了D盘。

方案三

ssh-add 之后,切换目录就不行了,在~/ 目录新建文件 .profile, 内容如下:

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

重启terminal 之后再 ssh-add ,这个时候是全局生效的。

垃圾 windows :(

我,秦始皇,打钱!

相关文章

禅境花园小程序

关注前沿技术开发.