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密钥是否可以成功登录到远程主机

相关推荐
爱说实话8 小时前
腾讯云Ubuntu远程无法ssh登录
云计算·ssh·腾讯云
木子欢儿10 小时前
Intel 架构的 MacBook Pro 运行 Linux 发热量高解决
linux·运维·服务器
Liekkas Kono12 小时前
Codex(ChatGPT 桌面版)支持SSH连接远程服务器
chatgpt·ssh·swhl
清水迎朝阳13 小时前
客户端软件 — 用户统计方案
服务器·c++·客户端·用户统计
会编程的土豆13 小时前
Go 模板初识
linux·运维·服务器
腾讯蓝鲸智云13 小时前
标杆客户实践提炼:CFlow 版本价值流模板实战指南
运维·服务器·自动化·云计算·devops
阿标的博客14 小时前
Active Directory 组织单位和用户
服务器
Deryck_德瑞克14 小时前
【Nginx】配置差异分析
服务器·前端·nginx
前端 贾公子14 小时前
第06章:结构化输出 (上)
java·服务器·前端
regret~15 小时前
【记录】网络路由、转发与网卡隔离
linux·服务器·网络