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

相关推荐
Shadow(⊙o⊙)几秒前
Shell进程替换,自定义Shell解释器——字符串库函数灵活操作!
linux·运维·服务器·开发语言·c++·学习
funnycoffee1232 分钟前
华为S5736交换机3层ECMP负载方式
linux·服务器·数据库
上海云盾-小余11 分钟前
内网终端安全管控:筑牢企业内部网络入侵防火墙
服务器·网络·安全
SparkleN.15 分钟前
/tmp/cargo/env:No such file or directory
运维·服务器
00后程序员张18 分钟前
Windows 下怎么生成 AppStoreInfo.plist?不依赖 Xcode 的方法
ide·macos·ios·小程序·uni-app·iphone·xcode
叶半欲缺40 分钟前
Linux初始化数据盘
java·linux·服务器
阳光九叶草LXGZXJ1 小时前
达梦数据库-学习-57-读写数据页超时告警排查(page[x,x,xxxxxx] disk write uses)-DSC集群版
linux·运维·服务器·数据库·sql·学习
lolo大魔王1 小时前
Linux监测磁盘空间
linux·运维·服务器
不仙5201 小时前
Rocky Linux 8.10 TigerVNC 安装配置指南
linux·服务器·网络
浮生若城1 小时前
Linux基础I/O(1)
linux·运维·服务器