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
相关推荐
砖厂小工1 天前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
程序员鱼皮1 天前
又一个新项目完结,我要出海了!
ai·github·开源项目
徐小夕1 天前
pxcharts-vue:一款专为 Vue3 打造的开源多维表格解决方案
前端·vue.js·github
Moment1 天前
想要长期陪伴你的助理?先从部署一个 OpenClaw 开始 😍😍😍
前端·后端·github
我叫黑大帅1 天前
前端如何利用 GitHub Actions 自动构建并发布到 GitHub Pages?
前端·面试·github
HelloGitHub1 天前
这个年轻的开源项目,想让每个人都能拥有自己的专业级 AI 智能体
开源·github·agent
刘发财1 天前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
sunny8652 天前
Claude Code 跨会话上下文恢复:从 8 次纠正到 0 次的工程实践
人工智能·开源·github
ZengLiangYi2 天前
Git Tag + Semver + CI/CD:从打标签到自动发布的完整实践
github