这是我一直在寻找的答案,直到我尝试了有和没有密码短语才找到:
私钥加密后,每次需要使用时都必须输入密码。
然后我了解了“ssh-agent”守护进程/服务,它可以在每次登录时缓存一次凭据。当我经历这个时,我正在建立我的 Gitlab.com 连接。
这是我尝试两者的脚本。
Microsoft Windows [Version 10.0.19042.985]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Administrator>where ssh-keygen
C:\Windows\System32\OpenSSH\ssh-keygen.exe
C:\Users\Administrator>ssh-keygen -t rsa -b 2048 -C "XXXXXXXXXXXX"
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\Administrator/.ssh/id_rsa):
Created directory 'C:\Users\Administrator/.ssh'.
Enter passphrase (empty for no passphrase): <===== here I did NOT use a passphrase
Enter same passphrase again:
Your identification has been saved in C:\Users\Administrator/.ssh/id_rsa.
Your public key has been saved in C:\Users\Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:------------------------------------------- XXXXXXXXXXXX
The key's randomart image is:
+---[RSA 2048]----+
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
| xxxxxxxxxxxxx |
+----[SHA256]-----+
C:\Users\Administrator>cat %userprofile%/.ssh/id_rsa.pub | clip
NOTE:
NOTE: at this point I navigated to gitlab in my browser, and pasted the key
NOTE:
C:\Users\Administrator>ssh -T git@gitlab.com
The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
ECDSA key fingerprint is SHA256:-------------------------------------------.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitlab.com,172.65.251.78' (ECDSA) to the list of known hosts.
Welcome to GitLab, @<username>!
C:\Users\Administrator>ssh -T git@gitlab.com
Welcome to GitLab, @<username>!
C:\Users\Administrator>ssh-keygen -p
Enter file in which the key is (C:\Users\Administrator/.ssh/id_rsa):
Key has comment 'XXXXXXXXXXXX'
Enter new passphrase (empty for no passphrase): <===== here I DID use a passphrase
Enter same passphrase again:
Your identification has been saved with the new passphrase.
NOTE:
NOTE: now I'm prompted every time
NOTE:
C:\Users\Administrator>ssh -T git@gitlab.com
Enter passphrase for key 'C:\Users\Administrator/.ssh/id_rsa':
Welcome to GitLab, @<username>!
C:\Users\Administrator>ssh -T git@gitlab.com
Enter passphrase for key 'C:\Users\Administrator/.ssh/id_rsa':
Welcome to GitLab, @<username>!
NOTE:
NOTE: set up the service to run (it is disabled by default)
NOTE:
C:\Users\Administrator>sc config ssh-agent start= auto
[SC] ChangeServiceConfig SUCCESS
C:\Users\Administrator>sc start ssh-agent
SERVICE_NAME: ssh-agent
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
(STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PID : 9508
FLAGS :
NOTE:
NOTE: will still get prompt, until 'ssh-add' (which itself prompts)
NOTE:
C:\Users\Administrator>ssh -T git@gitlab.com
Enter passphrase for key 'C:\Users\Administrator/.ssh/id_rsa':
Welcome to GitLab, @<username>!
C:\Users\Administrator>ssh-add .ssh\id_rsa
Enter passphrase for .ssh\id_rsa:
Identity added: .ssh\id_rsa (XXXXXXXXXXXX)
NOTE:
NOTE: no more prompting until I reboot :D
NOTE:
C:\Users\Administrator>ssh -T git@gitlab.com
Welcome to GitLab, @<username>!
C:\Users\Administrator>echo Tada!
Tada!
C:\Users\Administrator>