All Products
Search
Document Center

Elastic Compute Service:Connect to a Linux instance by using an SSH key pair from a third-party client

Last Updated:May 14, 2024

Alibaba Cloud provides the secure and convenient SSH key pair-based authentication method for logons to Elastic Compute Service (ECS) instances. The key pairs are used for authentication and encrypted communication over the SSH protocol. An SSH key pair consists of a public key and a private key and can be used only on Linux instances. This meets your business requirements for higher security, convenience, and automation capabilities. This topic describes how to use an SSH key pair to connect to a Linux instance from a Windows device or a device that supports SSH commands, such as a Linux client or MobaXterm for Windows.

Prerequisites

  • An SSH key pair is created, and the .pem private key file is downloaded. For more information, see Create an SSH key pair.

  • The instance to which you want to connect is in the Running state.

  • An SSH key pair is bound to the instance. For more information, see Bind an SSH key pair.

  • 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. For more information, see Add a security group rule.

    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.

    Important

    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

Connect to a Linux instance

You can use one of the following methods to connect to the Linux instance by using an SSH key pair based on the operating system of your device.

Use an SSH key pair to connect to a Linux instance from a Windows device

The following section describes how to convert the format of a private key file from .pem to .ppk and how to use an SSH key pair to connect to a Linux instance. PuTTYgen is used in this example.

  1. Download and install PuTTYgen and PuTTY.

    Download PuTTYgen and PuTTY from the following links:

  2. Convert the format of a private key file from .pem to .ppk.

    1. Start PuTTYgen.

      In this example, PuTTYgen 0.71 is used.

    2. Set Type of key to generate to RSA and click Load.

      windows_puttygen_1

    3. Select All Files (*.*).

      windows_puttygen_2

    4. Select the .pem private key file that you want to convert.

    5. In the dialog box that appears, click OK.

    6. Click Save private key.

    7. In the dialog box that appears, click Yes.

    8. Specify a name for the .ppk private key file and click Save.

  3. Start PuTTY.

  4. Configure the private key file used for authentication.

    1. Choose Connection > SSH > Auth > Credentials.

    2. In the Private key file for authentication: section, click Browse…

    3. Select the resulting .ppk private key file.

    windows_putty_3

  5. Configure required parameters to connect to the Linux instance.

    1. Click Session.

    2. In the Host Name (or IP address) field, enter the logon account and public IP address of the instance.

      Format: <Username>@<IP address of the instance>. Example: ecs-user@10.10.xx.xxx.

    3. In the Port field, enter 22.

    4. Set Connection type to SSH.

    windows_putty_4

  6. Click Open.

    If the following message appears, you are logged on to the instance by using an SSH key pair.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:

    Warning

    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

  • If you want to log on to a Linux instance by using an SSH key pair as a user that you created on the instance, you must copy the ~/.ssh/authorized_keys file to the. ssh directory of the user. For more information, see Log on to a Linux instance as a user-created user by using an SSH key pair.

  • 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 Connect to a Windows instance by using a password or key.

  • 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 log on to the Linux instance by using Workbench from the ECS console. For more information, see Connect to a Linux instance by using a password or key.