Step 1: Generate SSH Key Pairs
# Check if there are any shh key pairs you can use
ls -al ~/.ssh
# Or generate SSH key on your own local system
ssh-keygen -t rsa -b 4096 -C "your comments of this key pairs"
Step 2: Add SSH Key to SSH Agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/[your_private_key_name]
# e.g., ~/.ssh/id_rsa
# kill $SSH_AGENT_PID
Step 3: Add the SSH Key to your GitHub Account
Go to GitHub webpage - Settings - SSH and GPG keys - New SSH key and copy your public key there. The public key file always comes with the suffix .pub
.
Step 4: Test Connection
ssh -T git@github.com
If you see “Hi XXX! You’ve successfully authenticated, but GitHub does not provide shell access.”, it means you succeed.
ssh-agent 到底做了什么?
ssh-agent -s
得到
SSH_AUTH_SOCK=/var/folders/py/30gp370n1nn9g010n9_s2mdr0000gn/T//ssh-aYulwYt7eCRH/agent.94613; export SSH_AUTH_SOCK;
SSH_AGENT_PID=94614; export SSH_AGENT_PID;
echo Agent pid 94614;
其实这一串东西就是 eval
命令执行的内容。即先设置了一些环境变量如 SSH_AUTH_SOCK
和 SSH_AGENT_PID
供后续使用,然后在屏幕上输出 pid。这两个环境变量应该是 ssh-add
需要用到的内容。
因此下面的命令
eval "$(ssh-agent -s)"
得到 Agent pid XXXXX
这样的输出,同时默默地执行了对环境变量的设置。
关于 GitHub 近期新增 Access Token
可以将之前的 HTTPS 仓库地址修改为 SSH 地址:
git remote rm origin
git remote add origin [url]
至于如何使用新的 Access Token 还没试过,之后尝试。