使用 SSH 连接 Github

生成新的 SSH Key

1ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
2...
3Enter a file in which to save the key (/home/you/.ssh/algorithm): <key_name>
4Enter passphrase (empty for no passphrase): [输入密码]
5Enter same passphrase again: [再次输入密码]

将新的 SSH Key 添加到 Github

  1. 将公钥文件的内容COPY出来
1cat ~/.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
 1cd ~/.ssh
 2# 设置访问权限
 3chmod 600 <key_name>
 4# 生成SSH配置文件
 5cat > config << EOF
 6Host github.com
 7  Hostname ssh.github.com
 8  Port 443
 9  IdentityFile ~/.ssh/<key_name>
10  UpdateHostKeys yes
11  Compression yes
12  User git
13EOF
  1. 连接测试
1ssh -T git@github.com
2...
3Are you sure you want to continue connecting (yes/no)? <yes>
4...
5Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

自动验证

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

 1# debian
 2apt install -y keychain
 3# centos
 4yum install -y keychain
 5# alpine linux
 6apk add keychain
 7
 8# debian or centos in ~/.bashrc or ~/.bash_profile
 9# alpine linux in /etc/profile.d/30user.sh
10echo "eval `keychain --eval ~/.ssh/<key_name>`" >> ~/.bashrc
11...
12 * Adding 1 ssh key(s): /home/<username>/.ssh/<key_name>
13 * ssh-add: Identities added: /home/<username>/.ssh/<key_name>

相关专栏文章