Help Center/ Elastic Cloud Server/ Best Practices/ Selection and Configuration/ Logging In to a Linux ECS Using a Created User and SSH Key Pair
Updated on 2025-08-06 GMT+08:00

Logging In to a Linux ECS Using a Created User and SSH Key Pair

Background

An SSH key pair is a secure and convenient login authentication method. It consists of a public key and a private key, supports only Linux ECSs, and meets your service requirements for higher security, convenience, and automation. You can use an SSH key pair to connect to a Linux ECS.

Prerequisites

Procedure

  1. Connect to the Linux ECS using a proper login mode. For details, see Login Overview (Linux).
  2. Create a user.

    1. Run the command below to create a user. <username> indicates the username.
      sudo useradd -m <username>     
    2. Run the following command to check whether the user is created:
      groups <username>

  3. Set a key pair for the user.

    1. Prepare the key pair.

      If you use your own key pair, you need to store the id_rsa.pub (public key) in the Linux ECS. is_rsa (private key) file will be used to log in to the Linux ECS.

      If no key pair is available, run the command below and press Enter repeatedly to generate a 2,048-bit RSA key pair. The public key whose name ends with id_rsa.pub and private key whose name ends with is_rsa are generated in the current path.

      ssh-keygen -t rsa -b 2048 -f "./id_rsa"
      Figure 1 Generating a key pair
    2. Configure the public key in the authorized_keys file.

      Copy the content of the public key file to the .ssh/authorized_keys file in the user root directory on ECS. You can use a file transfer tool, such as Xftp, to upload the file.

      1. Run the following command to create the .ssh folder in the user directory:
        sudo mkdir /home/<username>/.ssh
      2. Create the authorized_keys file in the directory created in step 3.b.i and copy the content of the public key file generated or prepared in step 3.a to this file. You can use the vim editor to create and modify the file.
      3. Change the permissions on the .ssh directory and the authorized_keys file.
        sudo chmod 700 /home/<username>/.ssh
        sudo chmod 600 /home/<username>/.ssh/authorized_keys
      4. Run vim commands to open the /etc/ssh/sshd_config file, enter the editing mode, and set PubkeyAuthentication and RSAAuthentication to yes.
        PubkeyAuthentication yes
        RSAAuthentication yes
      5. Run the following command to restart sshd for the configuration to take effect:
        sudo systemctl restart sshd
    3. Remotely log in to the ECS using a created user via PuTTY or Xshell. For details, see Logging In to a Linux ECS Using an SSH Key Pair.