# 使用 SSH 密钥密码

您可以保护 SSH 密钥并配置身份验证代理，这样您就不必在每次使用 SSH 密钥时重新输入密码。

## 关于 SSH 密钥的密码短语

使用 SSH 密钥时，如果攻击者获得你计算机的访问权限，便可以访问使用该密钥的每个系统。 要添加额外的安全层，可以向 SSH 密钥添加密码。 为避免每次连接时都输入密码，可以将密钥安全地缓存到 SSH 代理中。

## 添加或更改密码

通过输入以下命令，您可以更改现有私钥的密码而无需重新生成密钥对：

```shell
$ ssh-keygen -p -f ~/.ssh/id_ed25519
> Enter old passphrase: [Type old passphrase]
> Key has comment 'your_email@example.com'
> Enter new passphrase (empty for no passphrase): [Type new passphrase]
> Enter same passphrase again: [Repeat the new passphrase]
> Your identification has been saved with the new passphrase.
```

如果您的密钥已有密码，系统将提示您输入该密码，然后才能更改为新密码。

<div class="ghd-tool windows">

## 在 Git for Windows 上自动启动`ssh-agent`

可以在打开 bash 或 Git shell 时自动运行 `ssh-agent`。 复制以下行并将其粘贴到 Git shell 中的 `~/.profile` 或 `~/.bashrc` 文件中：

```bash
env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env
```

如果私钥没有存储在默认位置之一（如 `~/.ssh/id_rsa`），需要告知 SSH 身份验证代理其所在位置。 要将密钥添加到 ssh-agent，请输入 `ssh-add ~/path/to/my_key`。 有关详细信息，请参阅“[生成新的 SSH 密钥并将其添加到 ssh-agent](/zh/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)”。

> \[!TIP]
> 如果希望 `ssh-agent` 在一段时间后忘记密钥，可以通过运行 `ssh-add -t <seconds>` 对其进行配置。

现在，当您初次运行 Git Bash 时，系统将提示您输入密码：

```shell
> Initializing new SSH agent...
> succeeded
> Enter passphrase for /c/Users/YOU/.ssh/id_rsa:
> Identity added: /c/Users/YOU/.ssh/id_rsa (/c/Users/YOU/.ssh/id_rsa)
> Welcome to Git (version 1.6.0.2-preview20080923)
>
> Run 'git help git' to display the help index.
> Run 'git help <command>' to display help for specific commands.
```

`ssh-agent` 进程将继续运行，直到注销、关闭计算机或终止该进程。

</div>

<div class="ghd-tool mac">

## 在密钥链中保存密码

在 Mac OS X Leopard 到 OS X El Capitan 版本中，这些默认的私钥文件会自动处理。

* .ssh/id\_rsa
* .ssh/identity

初次使用密钥时，系统将提示您输入密码。 如果选择使用密钥链保存密码，则无需再次输入密码。

否则，您可在将密钥添加到 ssh-agent 时在密钥链中存储密码。 有关详细信息，请参阅“[生成新的 SSH 密钥并将其添加到 ssh-agent](/zh/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent)”。

</div>