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
相关推荐
赛博三把手20 分钟前
「2026 最新推荐」AI 大模型 API 中转站 | 国内直连 ChatGPT/Claude/Gemini 稳定优质的 API 接口服务
人工智能·github·ai编程
zzzzzz31021 分钟前
shadcn/improve:让最强模型当架构师,便宜模型当码农
机器学习·程序员·github
小雨青年1 小时前
GitHub Copilot CLI 命令行工具深度使用指南
github·copilot
阿萨德528号2 小时前
[特殊字符] CI/CD 流水线搭建实战指南:Spring Boot + GitHub Actions → 服务器自动部署
spring boot·ci/cd·github
csdn小瓯2 小时前
本周 GitHub 热门项目推荐:Headroom 和 CC Switch
人工智能·github·开源项目
Jurio.18 小时前
开源 Codex Sticky:在终端 Codex CLI 长对话中始终固定底部输入框
linux·rust·github·开源软件·codex·codex cli
半夜修仙19 小时前
RabbitMQ中如何保证消息的可靠性传输
java·分布式·中间件·rabbitmq·github·java-rabbitmq
旅之灵夫21 小时前
【GitHub项目推荐--Harness:一体化的开源 DevOps 平台】⭐
github
虾壳云智能1 天前
详解 OpenClaw 部署难点 绕过安全拦截与路径报错解决方案
人工智能·github·open claw教程·open claw一键部署
_codemonster1 天前
Codex中github cli未通过身份验证解决方法
github