すべてのプロダクト
Search
ドキュメントセンター

Elastic Compute Service:サードパーティクライアントから SSH キーペアを使用して Linux インスタンスに接続する

最終更新日:Apr 08, 2025

Alibaba Cloud は、Elastic Compute Service (ECS) インスタンスへのログインに、セキュアで便利な SSH キーペアベースの認証方式を提供しています。キーペアは、SSH プロトコルを介した認証と暗号化通信に使用されます。SSH キーペアは公開鍵と秘密鍵で構成され、Linux インスタンスでのみ使用できます。この Topic では、SSH キーペアを使用して、Windows デバイス、または Linux クライアントや Windows 用の MobaXterm など、SSH コマンドをサポートするデバイスから Linux インスタンスに接続する方法について説明します。

Prerequisites

  • An SSH key pair is created, and the .pem private key file is downloaded.

  • The instance to which you want to connect is in the 実行中 state.

  • An SSH key pair is bound to the instance.

  • A public IP address or an elastic IP address (EIP) is associated with the instance. For more information, see Associate an EIP with an ECS instance.

  • A security group rule is added to the security groups of the instance to allow traffic on the required port. In this topic, traffic on the default SSH port 22 is allowed.

    Network type

    Rule direction

    Action

    Port range

    Priority

    Authorization object

    Virtual Private Cloud (VPC)

    Inbound

    Allow

    SSH(22)

    1

    The public IP address of the on-premises client. This public IP address is different from that of the ECS instance.

    重要

    You can specify 0.0.0.0/0 as the authorization object to allow inbound access from all IP addresses. However, this imposes your instance to security risks. Proceed with caution.

    Classic network

    Internet ingress

Linux インスタンスに接続する

デバイスのオペレーティングシステムに基づいて、SSH キーペアを使用して Linux インスタンスに接続するには、次のいずれかの方法を使用できます。

Windows デバイスから SSH キーペアを使用して Linux インスタンスに接続する

次のセクションでは、秘密鍵ファイルのフォーマットを .pem から .ppk に変換する方法と、SSH キーペアを使用して Linux インスタンスに接続する方法について説明します。この例では、PuTTYgen を使用します。

  1. PuTTYgen と PuTTY をダウンロードしてインストールします。

    次のリンクから PuTTYgen と PuTTY をダウンロードします。

  2. 秘密鍵ファイルのフォーマットを .pem から .ppk に変換します。

    1. PuTTYgen を起動します。

      この例では、PuTTYgen 0.71 を使用します。

    2. [生成するキーの種類][RSA] に設定し、[読み込み] をクリックします。

      windows_puttygen_1

    3. [すべてのファイル (*.*)] を選択します。

      windows_puttygen_2

    4. 変換する .pem 秘密鍵ファイルを選択します。

    5. 表示されるダイアログボックスで、[OK] をクリックします。

    6. [秘密鍵の保存] をクリックします。

    7. 表示されるダイアログボックスで、[はい] をクリックします。

    8. .ppk 秘密鍵ファイルの名前を指定し、[保存] をクリックします。

  3. PuTTY を起動します。

  4. 認証に使用する秘密鍵ファイルを構成します。

    1. [接続] > [SSH] > [認証] > [資格情報] を選択します。

    2. [認証用の秘密鍵ファイル:] セクションで、[参照...] をクリックします。

    3. 作成された .ppk 秘密鍵ファイルを選択します。

    windows_putty_3

  5. Linux インスタンスに接続するために必要なパラメータを構成します。

    1. [セッション] をクリックします。

    2. [ホスト名 (または IP アドレス)] フィールドに、インスタンスのログインアカウントとパブリック IP アドレスを入力します。

      フォーマット: [<ユーザー名>@<インスタンスの IP アドレス>]。例: ecs-user@10.10.xx.xxx。

    3. [ポート] フィールドに [22] と入力します。

    4. [接続の種類][SSH] に設定します。

    windows_putty_4

  6. [開く] をクリックします。

    次のメッセージが表示されたら、SSH キーペアを使用してインスタンスにログインしています。windows_putty_5

Use an SSH key pair to connect to a Linux instance from a device that supports SSH commands (configure information by using commands)

The following section describes how to use commands to configure the required settings on a device that supports SSH commands (such as a Linux client or MobaXterm for Windows) and then how to use SSH commands to connect to the instance from the device.

  1. Find the path of the .pem private key file on the on-premises device. Example: ~/.ssh/ecs.pem.

    The path and file name used are for reference only. You can modify the information in subsequent commands based on actual conditions.

  2. Run the following command to modify the attribute of the private key file:

    chmod 400 [Path of the .pem private key file on your on-premises device]

    Example:

    chmod 400 ~/.ssh/ecs.pem
  3. Run the following command to connect to the instance:

    ssh -i [Path of the .pem private key file on your on-premises device] username@[Public IP address]

    Example:

    ssh -i ~/.ssh/ecs.pem ecs-user@10.10.xx.xxx

Use an SSH key pair to connect to a Linux instance from a device that supports SSH commands (configure information by using the config file)

The following section describes how to use commands to configure the required settings on a device that supports SSH commands (such as a Linux client or MobaXterm for Windows) and then how to use SSH commands to connect to the instance from the device.

  1. Find the path of the .pem private key file on the on-premises device. Example: ~/.ssh/ecs.pem.

    The path and file name used are for reference only. You can modify the information in subsequent commands based on actual conditions.

  2. Run the following command to modify the attribute of the private key file:

    chmod 400 [Path of the .pem private key file on your on-premises device]

    Example:

    chmod 400 ~/.ssh/ecs.pem
  3. Run the following commands to go to the .ssh directory in the home directory and create a config file:

    cd ~/.ssh
    vim config
  4. In the config file, press the I key to enter the Insert mode and add the following configuration items:

    Sample code used to add configuration items for a single ECS instance:

    # Enter the alias of the ECS instance that you want to connect to over SSH. 
    Host ecs
    # Enter the public IP address of the instance. 
    HostName 121.196.**.**
    # Enter the port number. The default port number is 22. 
    Port 22
    # Enter the logon username. 
    User ecs-user
    # Enter the path of the .pem private key file on your on-premises device. 
    IdentityFile ~/.ssh/ecs.pem

    Sample code used to add configuration items for multiple ECS instances:

    # Enter the alias of the ECS instance that you want to connect to over SSH. 
    Host ecs1
    # Enter the public IP address of the instance. 
    HostName 121.196.**.**
    # Enter the port number. The default port number is 22. 
    Port 22
    # Enter the logon username. 
    User ecs-user
    # Enter the path of the .pem private key file on your on-premises device. 
    IdentityFile ~/.ssh/ecs.pem
    
    # Enter the alias of the ECS instance that you want to connect to over SSH. 
    Host ecs2
    # Enter the public IP address of the instance. 
    HostName 121.196.**.**
    # Enter the port number. The default port number is 22. 
    Port 22
    # Enter the logon username. 
    User ecs-user
    # Enter the path of the .pem private key file on your on-premises device. 
    IdentityFile ~/.ssh/ecs.pem

    After the configuration items are added, press the Esc key and enter :wq to save the config file.

  5. Run the following command to restart the SSH service:

    警告

    If the SSH service fails to be restarted, the SSH service may become unavailable and service interruptions may occur. We recommend that you restart the SSH service during off-peak hours.

    service sshd restart
  6. Run the following command to connect to the instance:

    ssh [Alias of the instance]

    Example:

    ssh ecs

References

  • The ECS console cannot be used to bind key pairs to Windows instances. If you want to use a key pair to log on to a Windows instance, you can enable the sshd service and configure a key pair inside the instance. For more information, see Use Workbench to connect to a Windows instance over RDP.

  • If you want to log on to an instance by using the password-based authentication method after you bind a key pair to the instance, reset the password for the instance. After you reset the password for the instance, use the key pair or the new password to log on to the instance. For more information, see Reset the logon password of an instance.

  • If you do not want to connect to a Linux instance by using an SSH key pair from an on-premises device, you can use Workbench from the ECS console. For more information, see Use Workbench to connect to a Linux instance over SSH.