如果我想 ssh 到 `localhost`,我的authorized_keys 檔案應該在哪裡?

如果我想 ssh 到 `localhost`,我的authorized_keys 檔案應該在哪裡?

我有我的公鑰/私鑰設置,但我不知道它應該放在哪裡。應該~/.ssh/為我的用戶放置這兩個文件嗎?

答案1

私鑰留在家裡。公鑰傳播:

  • 將您的私鑰放入~/.ssh(本機主機)。通常會是~/.ssh/id_rsa
  • 將您的公鑰放入~/.ssh/authorized_keys(遠端主機)。

雖然第一步通常在建立金鑰時自動完成,但第二步可以透過以下方式完成:

$ ssh-copy-id ~/.ssh/id_rsa.pub user@remotehost

如果您有任何有關遠端主機的殘留配置,您可能會遇到一些麻煩(身份驗證失敗,或者“身份驗證失敗次數過多”)。若要解決此問題,您可以強制ssh-copy-id使用密碼驗證:

$ ssh-copy-id -o"PubkeyAuthentication no" ~/.ssh/id_rsa.pub user@remotehost

當然,一旦您的鑰匙正確放置,就不再需要密碼了:

$ ssh user@remotehost

您可能還想添加一些 SSH 配置以~/.ssh/config使其更加簡單:

Host [custom name for the remote machine]
    Hostname [remote hostname or IP]
    User [remote username]
    IdentityFile /home/[your local user]/.ssh/id_rsa

因此,您只需鍵入以下內容即可登入遠端主機:

$ ssh [custom name for the remote machine]

編輯:如果兩台機器(本地和遠端)相同,您可以透過以下方式簡化流程:

$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

這會將您的公鑰附加到authorized_keys文件中。當然,在本機電腦上以其他人身分登入的另一種方法是使用:

$ su [another user]

這將為您節省可能不需要的 SSH 交易。su可以使用以下方式設定無密碼sudo:

$ sudo -iu [another user]

相關內容