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

Digital Ocean Ubuntu Droplet中SSH密钥无法永久加载的原因及解决方法咨询

Digital Ocean Ubuntu Droplet中SSH密钥无法永久加载的原因及解决方法咨询

Hey there! Let's figure out why your SSH key keeps needing manual setup every time you log in, and fix it for good—this is a super common gotcha, so you're not alone.

为什么密钥没法永久加载?

The core issue is that ssh-agent is a session-specific process. Every time you log into your droplet via SSH, you're starting a fresh shell session. The ssh-agent you started in your last session is tied to that old session, so your new shell can't access it. And the keys you added to that agent only exist in its memory—once the session ends, they're gone too. That's why you have to re-run those commands every time.

解决方法:两种简单方案选其一

方案1:让shell自动启动ssh-agent并添加密钥

We can add a little script to your shell's startup file so it checks for a running ssh-agent every time you log in, and starts one + adds your key if it's missing.

  • Open your bash startup file (Ubuntu uses ~/.bashrc by default) with a text editor:
    nano ~/.bashrc
    
  • Scroll to the very end and paste this code:
    # Auto-start ssh-agent and add key if needed
    if [ -z "$SSH_AUTH_SOCK" ]; then
        # Start ssh-agent and save its environment variables
        eval "$(ssh-agent -s)"
        # Add your specific SSH key
        ssh-add ~/.ssh/my_key
    fi
    
  • Save and exit (for nano: press Ctrl+O, hit Enter, then Ctrl+X).
  • Make the changes take effect immediately without logging out:
    source ~/.bashrc
    

Next time you log in, this script will handle the agent and key setup automatically.

方案2:直接让SSH自动使用你的密钥(更简单)

If you only need this key for GitHub, you can skip the agent entirely by configuring SSH to automatically use your key when connecting to GitHub.

  • Create or edit the SSH config file:
    nano ~/.ssh/config
    
  • Add these lines to the file:
    Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/my_key
    
  • Save and exit nano as before.

Now, whenever you run git pull or any GitHub-related SSH command, SSH will automatically use your my_key file—no need to mess with ssh-agent at all.

重要注意事项:权限检查

SSH is picky about file permissions for security reasons. Make sure your SSH directory and key file have tight permissions to avoid issues:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/my_key

If permissions are too open (like readable by other users), SSH will refuse to use the key, even if everything else is set up right.

备注:内容来源于stack exchange,提问作者kontur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 10:50:28