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
相关推荐
James_WangA13 小时前
我给 AOI 设备装了一个 Agent,然后发现工具注册才是最难写的
架构·github
James_WangA13 小时前
产线上跑 Agent:LLM 挂了不是 500 错误,是停线
架构·github
Hommy8817 小时前
【开源剪映小助手】字幕接口
开源·github·aigc·剪映小助手·视频剪辑自动化
一袋米扛几楼9819 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
尘埃落定wf20 小时前
# GitHub CLI:告别繁琐的 Git 命令,让开发更高效
git·github
zh_xuan20 小时前
上传api调试工具到github
github
AIMath~1 天前
彻底删除 GitHub 仓库中特定文件夹的完整指南
github
DogDaoDao1 天前
【GitHub】OpenClaw:开源个人AI助手的新标杆
人工智能·深度学习·开源·大模型·github·ai编程·opeclaw
一点一木1 天前
🚀 2026 年 4 月 GitHub 十大热门项目排行榜 🔥
人工智能·github
宝桥南山1 天前
AI - 在命令行中尝试一下ACP(Agent Client Protocol)通信
microsoft·微软·github·aigc·copilot