在 macOS 上设置 SSH 和 Git

在收到我的新 MacBook 后,我开始在 macOS 上配置开发环境。为了在 macOS 上配置 Git ,我在 MacBook 上生成了新的 SSH 密钥,并将其添加到我的 GitHub 账户中,以启用通过 SSH 进行 Git 操作的身份验证。

1. 生成新的 SSH 密钥

我打开终端,粘贴以下命令来生成新的 SSH 密钥:

bash 复制代码
ssh-keygen -t ed25519 -C "your_email@example.com"

输出如下:

bash 复制代码
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/shenlu/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/shenlu/.ssh/id_ed25519
Your public key has been saved in /Users/shenlu/.ssh/id_ed25519.pub

密钥指纹信息:

bash 复制代码
The key fingerprint is:
SHA256:kWkioRYSeKUUpNBRckvF/JGfJ+wfcu2FrtE6/TYgrr0 your_email@example.com

密钥的 randomart 图像:

bash 复制代码
+--[ED25519 256]--+
|=+B=*+.  .       |
|o+oB oo oo       |
|..+ o ..=+ .     |
| .   . o..= .    |
|        S. o . . |
|          o.ooo .|
|          .+o=o. |
|          ..oo+..|
|         ..E+o oo|
+----[SHA256]-----+

说明:

-t 参数指定加密算法为 ed25519。系统会提示你输入密码(可留空)。请将 "your_email@example.com" 替换为你自己的 GitHub 邮箱。-C 参数是给密钥添加注释,一般使用邮箱作为标识。

ed25519 是一种基于椭圆曲线的公钥签名算法,使用 EdDSACurve25519

2. 将 SSH 密钥添加到 GitHub 账户

将公钥复制到剪贴板:

bash 复制代码
pbcopy < ~/.ssh/id_ed25519.pub

这会将 id_ed25519.pub 文件的内容复制到剪贴板。如果 pbcopy 无法使用,你可以手动打开隐藏的 .ssh 文件夹,使用你喜欢的文本编辑器打开该文件并复制内容。

然后将公钥粘贴到 GitHub 设置中:

  1. 打开 GitHub 并登录你的账户
  2. 进入「Settings(设置)」页面
  3. 点击「SSH and GPG keys」
  4. 点击「New SSH key」
  5. 输入该密钥的标题(例如:MacBook)
  6. 将公钥粘贴到「Key」文本框中
  7. 点击「Add SSH Key」
  8. 如果系统提示,输入你的 GitHub 密码确认操作

3. Git 配置

如果你还没有安装 Homebrew,请先安装它。然后安装 Git

复制代码
brew install git

使用以下命令设置你的 Git 用户名和邮箱:

arduino 复制代码
git config --global user.name "Shen Lu"
git config --global user.email "shenlu89dev@gmail.com"

测试 Git 是否可以通过 SSH 正常连接到 GitHub

css 复制代码
ssh -T -p 443 git@ssh.github.com

首次连接时你会看到如下提示:

vbnet 复制代码
The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: github.com
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.
Hi shenlu89! You've successfully authenticated, but GitHub does not provide shell access.

这表明你已成功通过 SSH 认证,尽管 GitHub 不提供 shell 访问权限,但你现在可以通过 SSH 自由地进行 Git 操作。

参考资料

相关推荐
EXI-小洲8 分钟前
MacOS 上使用 IntelliJ IDEA 指定 Main 函数并打包为 JAR 文件
macos·intellij-idea·jar
三言老师10 小时前
秘诀-如何远程SSN访问家里的八台电脑(frp 实操方案)
linux·运维·ssh
柒号华仔15 小时前
「速通Shell」聚砖成墙,Shell函数封装之道
linux·ssh·bash
整点bug19 小时前
AI 运维该不该自动执行命令?
运维·ssh·openai
Albart5751 天前
Docker Desktop最新版安装踩坑全记录(Windows_Mac_Linux)【2026 4.74.0 终版】
linux·windows·macos·docker·环境搭建·踩坑记录
互联网中的一颗神经元1 天前
ZZ — Git 速查表
大数据·git·elasticsearch
进击的码力1 天前
win11 使用ssh 遇到权限too open 无法登录服务器的问题
运维·服务器·ssh
早安试言1 天前
git使用
git
燐妤1 天前
梦绘片场 ProjectDream故事总纲场景剧本篇:从项目到成片的 AI 漫剧工作台
人工智能·git·ai·项目开发
JackieDYH1 天前
Git 与 GitHub 新电脑配置指南(从零开始)
git·github·工具·使用教程