秘密鍵を使用してリモートサーバーにSSHで接続する方法は?

秘密鍵を使用してリモートサーバーにSSHで接続する方法は?

2台のサーバーがあります。どちらのサーバーもCentOS 5.6を使用します。私が持っている秘密鍵(OpenSSH SSH-2秘密鍵)を使用して、サーバー1からサーバー2にSSHを実行したいと思います。

Unixではこれを行う方法がわかりません。しかし、WindowsでPuttyを使用して行うことは、OpenSSH秘密鍵をputty-genに提供し、PPK形式の秘密鍵を生成することです。

しかし、SSHを介してサーバー2でいくつかのコマンドを実行するサーバー1でbashスクリプトを生成します。

サーバー1の秘密鍵ファイルを使用してSSH経由でサーバー2に接続するにはどうすればよいですか?

ベストアンサー1

簡単に言うと:-i既存の秘密鍵をインラインで使用するには、次のようにパラメータを使用してIDファイルパスを選択する必要があります。

ssh -i '/path/to/keyfile' username@server

SSH公開鍵が必要で、SSH秘密鍵も必要です。キー生成を使用できますssh-keygen。秘密鍵はサーバー1に保管し、公開鍵はサーバー2に保管する必要があります。

これについてはopensshのマニュアルページで詳しく説明されているので、引用をたくさんします。 「認証」セクションをお読みください。また、openSSHマニュアルも非常に役立ちます:http://www.openssh.org/manual.html

SSHを使用するとサーバーのセキュリティに影響を与える可能性がありますのでご注意ください。

からman ssh

 ~/.ssh/identity
 ~/.ssh/id_dsa
 ~/.ssh/id_rsa
     Contains the private key for authentication.  These files contain
     sensitive data and should be readable by the user but not acces-
     sible by others (read/write/execute).  ssh will simply ignore a
     private key file if it is accessible by others.  It is possible
     to specify a passphrase when generating the key which will be
     used to encrypt the sensitive part of this file using 3DES.

 ~/.ssh/identity.pub
 ~/.ssh/id_dsa.pub
 ~/.ssh/id_rsa.pub
     Contains the public key for authentication.  These files are not
     sensitive and can (but need not) be readable by anyone.

これは、.sshのホームディレクトリに秘密鍵を保存できることを意味します。もう1つの可能性は、-iパラメータスイッチを介して特別なIDファイルを使用するようにsshに指示することです。また、以下からman ssh

 -i identity_file
     Selects a file from which the identity (private key) for RSA or
     DSA authentication is read.  The default is ~/.ssh/identity for
     protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for pro-
     tocol version 2.  Identity files may also be specified on a per-
     host basis in the configuration file.  It is possible to have
     multiple -i options (and multiple identities specified in config-
     uration files).

秘密鍵用です。これで、サーバー2から公開鍵をインポートする必要があります。再引用するにはman ssh

  ~/.ssh/authorized_keys
         Lists the public keys (RSA/DSA) that can be used for logging in
         as this user.  The format of this file is described in the
         sshd(8) manual page.  This file is not highly sensitive, but the
         recommended permissions are read/write for the user, and not
         accessible by others.

これを達成する最も簡単な方法は、ファイルをサーバー2にコピーしてAuthorized_keysファイルに追加することです。

scp -p your_pub_key.pub user@host:
ssh user@host
host$ cat id_dsa.pub >> ~/.ssh/authorized_keys

SSHデーモンは公開鍵による認証を許可する必要がありますman ssh_config。をご覧ください。通常、これは構成ファイルに次のステートメントを追加することによって実行できます。

PubkeyAuthentication yes

おすすめ記事