github 用 SSH + 配好密钥(自动免密登录)
如果你是在 Linux 上操作,添加 GitHub SSH 登录同样适用,以下是完整的 Linux 下 GitHub SSH 登录配置流程(适配 Ubuntu / CentOS / Debian 等系统):
🧰 一、生成 SSH 密钥对(如果还没有)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
说明:
-t rsa 表示生成 RSA 类型密钥。
-b 4096 是密钥长度。
-C 是注释(通常用邮箱)。
连续回车即可(默认保存在 ~/.ssh/id_rsa)。
🧾 二、查看公钥内容
cat ~/.ssh/id_rsa.pub
复制输出的整行文本(一般以 ssh-rsa 开头)。
🌐 三、登录 GitHub 添加 SSH 公钥
打开网页 https://github.com
登录账号
点击右上角头像 → Settings(设置)
左侧点击 SSH and GPG keys
点击右上角 New SSH key
填写 Title(自定义,如“Linux服务器”)
粘贴刚才复制的公钥
点击 Add SSH key
🧪 四、测试连接是否成功
ssh -T git@github.com
首次连接会提示:
The authenticity of host 'github.com (IP)' can't be established...
Are you sure you want to continue connecting (yes/no)?
输入:
yes
如果一切正确,会显示:
Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.
✅ 步骤 6(可选):避免 Git 默认使用 https 地址
你可以全局设置 Git 优先使用 ssh:
git config --global url."git@github.com:".insteadOf "https://github.com/"