mac 使用ssh 密钥登录linux 服务器

本地操作

1. 生成SSH密钥对
bash 复制代码
# your_email@example.co 自行定义即可
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

这会提示你输入文件保存位置和密码(密码可以留空):

bash 复制代码
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_username/.ssh/id_rsa): /Users/your_username/.ssh/new_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
2. 添加公钥到远程主机

将生成的公钥 (new_id_rsa.pub) 添加到远程主机的 ~/.ssh/authorized_keys 文件中。(new_id_rsa.pub 默认在/Users/用户名/.ssh/ ) 目录下

复制公钥到剪贴板,然后,使用密码登录到远程主机,并将公钥粘贴到当前用户(如果当前用户没有.ssh/authorized_keys,则需要自己创建)的~/.ssh/authorized_keys 文件中:

bash 复制代码
# 在远程服务器上创建 .ssh 目录(如果不存在)
mkdir -p ~/.ssh

# 公钥追加到远程服务器上的 ~/.ssh/authorized_keys 文件中。your_copied_public_key 是你从本地复制的公钥内容
echo "your_copied_public_key" >> ~/.ssh/authorized_keys

# 将 ~/.ssh/authorized_keys 文件的权限设置为 600,即只有文件的拥有者有读取和写入权限。这样做可以确保文件的安全性。
chmod 600 ~/.ssh/authorized_keys

# 将 ~/.ssh 目录的权限设置为 700,即只有目录的拥有者有读取、写入和执行权限。这样做可以确保文件的安全性。
chmod 700 ~/.ssh

测试使用新的SSH密钥是否可以成功登录到远程主机

相关推荐
vi_h2 天前
在 macOS 上通过 Docker 安装并运行 Ollama(详细可执行教程)
macos·docker·ollama
茶杯梦轩3 天前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
服务器·后端·消息队列
YuMiao5 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
pe7er8 天前
macOS 应用无法打开(权限问题)解决方案
macos·mac
Sinclair8 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Rockbean9 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
埃博拉酱9 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
茶杯梦轩9 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
海天鹰10 天前
【免费】PHP主机=域名+解析+主机
服务器
不是二师兄的八戒10 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器