You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Linux系统的ssh-agent记住SSH密钥?

让Linux服务器的ssh-agent永久记住SSH部署密钥

方法1:通过shell启动脚本自动管理ssh-agent与密钥

  • 打开你的shell配置文件(bash用~/.bashrc,zsh用~/.zshrc)
  • 在文件末尾添加以下代码:
# 自动启动ssh-agent并保存进程信息
if ! pgrep -u "$USER" ssh-agent > /dev/null; then
    ssh-agent -s > "$HOME/.ssh-agent-info"
fi
# 加载已有的ssh-agent进程信息
if [[ -f "$HOME/.ssh-agent-info" ]]; then
    source "$HOME/.ssh-agent-info" > /dev/null
fi

# 检查密钥是否已在agent中,未加载则自动添加
ssh-add -l | grep -q id_ed25519 || ssh-add ~/.ssh/id_ed25519
  • 执行source ~/.bashrc(或对应shell的配置文件)使设置立即生效

方法2:通过SSH配置文件自动关联密钥

  • 在~/.ssh目录下创建或编辑config文件:
Host github.com
    IdentityFile ~/.ssh/id_ed25519
    AddKeysToAgent yes
    IdentitiesOnly yes
  • 这个配置会让SSH在连接GitHub时自动使用指定密钥,并且自动将密钥添加到ssh-agent中,无需手动执行ssh-add

内容的提问来源于stack exchange,提问作者Matt Welander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 11:14:52