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
相关推荐
森之鸟16 分钟前
2026年AI编程工具全景图:GitHub Copilot vs Cursor vs Codeium,我如何选择?
github·copilot·ai编程
wAIxiSeu2 小时前
Github开源项目推荐
开源·github
无限进步_5 小时前
面试题 02.02. 返回倒数第 k 个节点 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
CoderJia程序员甲5 小时前
GitHub 热榜项目 - 日榜(2026-01-31)
ai·开源·大模型·github·ai教程
飞乐鸟9 小时前
Github 16.8k Star!推荐一款开源的高性能分布式对象存储系统!
分布式·开源·github
CCC:CarCrazeCurator10 小时前
IDE 与编程语言区分介绍
git·github
Go_Zezhou10 小时前
render快速部署网站和常见问题解决
运维·服务器·开发语言·python·github·状态模式
玉梅小洋16 小时前
Git 使用技巧——查看 Commit 修改文件的概要
git·github
矢志航天的阿洪1 天前
从GitHub到本地:Python IGRF库环境配置完全指南
开发语言·python·github
chunmiao30321 天前
了解github上,watch,fork,star代表啥
github