For me with those below steps on win11, when I add ssh private key with password, and reboot my pc, then launch the gitbash again, I do NOT need to input password again, so it is works fine for me! Hope it works for you too!
The significant step here is to set the ssh private key with appropriate permission, my OS is Win11 21H2, git version is git version 2.40.0.windows.1, the following steps works well for me. You can refer to it.
First of all, set the correct ssh private key with appropriate permission,
- select the file(ssh private key), right click, select file properties
- click securty tab, click the advanced button, in the pop up windows, then
- you may see many users has ssh private key access ability, then
- click disable inheritance button, in pop up window select first one, it will clean up the inheritanced from properties from the drive(like
E:\\
- add or add user, then select add body or add object in pop up, in put you pc UserName, click the right button checking name, the result will change to something like PcName\UserName, then click OK button to confirm
- delete all other users, just left the user name just add, which is PcName\UserName, then click OK button
Set up correct Windows OpenSSH for GitBash command, the windows openssh for windows 11 is located at C:\Windows\System32\OpenSSH\
(if not, search it)
edit ~/..profile
with the following line
export PATH="/C/Windows/System32/OpenSSH/":$PATH
Launch GitBash, then to make sure in GitBash you can get the windows system ssh commands: run the following command to check which ssh
, the output of command should shows up with(or something like that)
/c/Windows/System32/OpenSSH/ssh.exe
Then the next is quite simple, just do the normal thing:
ssh-add -l
To see if ssh-agent
is start or not, if not do it
ssh-add path/to/you/ssh/private/key
the key oper upper steps
ssh-add -l
do check again
ssh -T git@github.com
The following is the shell script i use, it works both for windows and linux. You can reference to it. Copy and set the SSH private key file path to yours, then save it to a file, like ~/.ssh/EnableGitHubSSH.sh, then add a line of code source ~/.ssh/EnableGitHubSSH.sh
to ~/.profile
, then done! Try luanch GitBash or terminal to do github SSH login checking!
is_windows=$(uname -s | grep '[MINGW|MSYS_NT|CYGWIN_NT]')
if [ -n "${is_windows}" ]; then
# 将系统 OpenSSH 查找路径提前
# 删除 $HOME/bin (Git为啥会有这个设置?)
# Windows 系统 PATH 修正,使用系统默认 OpenSSH, 不用每次重启后都输入密码
# NOTE: 使用系统默 OpenSSH 时, 密码加密保存在系统中,SSH 的配置目录在 C 盘用户目录下
XPATH=$(echo $PATH | sed "s#/c/Windows/System32/OpenSSH:##g" | sed "s#$HOME/bin:##g");
export PATH="/c/Windows/System32/OpenSSH":$XPATH;
unset XPATH;
fi
# 自动生成的文件,缓存Agent运行状态
tmpfile=~/.ssh/_SSHAgentEnvInfo.sh
# SSH私钥(GitHub免密登录认证)
sec_key=~/.ssh/YOUR-SSH-PRIVATE-KEY-ONE
test -f "$tmpfile" && source "$tmpfile" >| /dev/null
# agent_run_state:
# 0 = agent running with key
# 1 = agent running without key
# 2 = agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
# 添加 Git 免密登录 GitHub 使用的 SSH 私钥
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
if [ -z "${is_windows}" ]; then
# Linux 需要运行的命令
(umask 077; ssh-agent -s >| "$tmpfile")
source "$tmpfile" >| /dev/null
else
# Win 11 手动配置 OpenSSH 开机启动
# SSH_AUTH_SOCK 随便设置一个值,防止每次启动 GitBash 都加载私钥(需要输入密码)
echo "SSH_AUTH_SOCK=OK;" > "$tmpfile"
fi
ssh-add "$sec_key" 2>/dev/null
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add "$sec_key" 2>/dev/null
fi
unset tmpfile sec_key is_windows