ssh 远程pc如何不用每次都输入密码

使用 SSH 密钥对认证

生成 SSH 密钥对,将公钥上传到远程服务器,实现免密码登录。在本地终端运行以下命令生成密钥对:

bash 复制代码
ssh-keygen -t rsa -b 4096

生成的密钥默认保存在 ~/.ssh/id_rsa(私钥)和 ~/.ssh/id_rsa.pub(公钥)。将公钥复制到远程服务器:

bash 复制代码
ssh-copy-id username@remote_host

配置 SSH 配置文件

在本地 ~/.ssh/config 文件中添加服务器配置,简化登录命令。示例配置:

bash 复制代码
Host myserver
    HostName remote_host
    User username
    IdentityFile ~/.ssh/id_rsa

配置后只需运行 ssh myserver 即可登录,无需输入密码。

使用 ssh-agent 管理密钥

启动 ssh-agent 并添加私钥,避免每次使用密钥时输入密码短语:

bash 复制代码
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

检查远程服务器配置

确保远程服务器的 /etc/ssh/sshd_config 文件包含以下配置:

bash 复制代码
PubkeyAuthentication yes
PasswordAuthentication no

修改后重启 SSH 服务:

bash 复制代码
sudo systemctl restart sshd

调试连接问题

如果仍然需要输入密码,检查权限设置。确保本地 .ssh 目录权限为 700,密钥文件权限为 600:

bash 复制代码
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
相关推荐
OpenTiny社区2 小时前
从零开发 AI 聊天页要两周?试试这款 Vue3 垂直对话组件库 TinyRobot,直接开箱即用
前端·vue.js·github
逛逛GitHub2 小时前
2 万多 Star!Google 开源了这个神级 GitHub 项目。
github
逛逛GitHub3 小时前
免费 Token 烧掉 5 万亿之后,他们出了个一站式创作平台。
github
用户805533698034 小时前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
inhere5 小时前
eget:不用等中央仓库,直接安装 GitHub 和任意下载站的工具
程序员·开源·github
YuePeng1 天前
写了五年注解的低代码框架,2.0 决定让你连注解都不用写了
github·产品
小白ai1 天前
从"能 ping 通吗"到"为什么上不了网"——我写了一个网络故障诊断引擎
github
徐小夕1 天前
jitword 协同文档3.2发布:打造浏览器中最强word编辑器
前端·架构·github
齐翊1 天前
分享一个在 Claude Code 里 [同时] 用多个 ApiKey 的方法
程序员·github·agent
A_Lonely_Cat1 天前
记一次 GitHub 幽灵协作者大清洗:强制重写 Git 历史与穿透 CDN 缓存实践
git·github