瘦了就帅了 / 使用 SSH 连接 Github

Created Tue, 21 Jun 2022 19:20:04 +0800 Modified Tue, 01 Jul 2025 05:36:31 +0000

生成新的 SSH Key

ssh-keygen -t rsa -b 4096 -C "[email protected]"
...
Enter a file in which to save the key (/home/you/.ssh/algorithm): <key_name>
Enter passphrase (empty for no passphrase): [输入密码]
Enter same passphrase again: [再次输入密码]

将新的 SSH Key 添加到 Github

  1. 将公钥文件的内容COPY出来
cat ~/.ssh/<key_name>.pub
  1. Github.com -> “Settings” -> “SSH and GPG keys” -> “New SSH key”

Title : 为新密钥添加描述性标签 Key : 粘贴COPY的公钥内容

  1. “Add SSH key”

使用 SSH 连接到 Github

  1. ssh config
cd ~/.ssh
# 设置访问权限
chmod 600 <key_name>
# 生成SSH配置文件
cat > config << EOF
Host github.com
  Hostname ssh.github.com
  Port 443
  IdentityFile ~/.ssh/<key_name>
  UpdateHostKeys yes
  Compression yes
  User git
EOF
  1. 连接测试
ssh -T [email protected]
...
Are you sure you want to continue connecting (yes/no)? <yes>
...
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

自动验证

如果找不到软件,直接下载安装 或 更换软件源 Linux 版本库管理

# debian
apt install -y keychain
# centos
yum install -y keychain
# alpine linux
apk add keychain

# debian or centos in ~/.bashrc or ~/.bash_profile
# alpine linux in /etc/profile.d/30user.sh
echo "eval `keychain --eval ~/.ssh/<key_name>`" >> ~/.bashrc
...
 * Adding 1 ssh key(s): /home/<username>/.ssh/<key_name>
 * ssh-add: Identities added: /home/<username>/.ssh/<key_name>