Use OpenSSH (macOS/Windows) or Xshell (Windows) to connect to a Linux ECS instance with a password or key pair.
For a simpler browser-based experience with password-free logon, use Workbench instead.
Before you connect
-
The instance runs Linux.
-
The instance has a static public IP address or an Elastic IP Address (EIP).
Method 1: Use an OpenSSH client (command line)
OpenSSH is the standard SSH client built into macOS and modern Windows, enabling command-line connections to remote servers.
Prerequisites
-
Instance public IP address: In ECS console - Instances, open the instance details page and find Public IP Address in the Configuration Information section.
-
Logon credentials: Set a password or bind a key pair for the instance.
-
Security group: Add an inbound rule to allow SSH access on port 22 from your local IP address.
Procedure
Windows 10/11
Connect with a password
-
Open PowerShell.
Press
Win+R, enterpowershell, and then pressEnter. -
Connect to the instance.
ssh <instance_username>@<instance_public_IP_address>Example:
ssh root@47.98.xxx.xxx -
Verify the host fingerprint (first-time only).
On first connection, the SSH client displays the host key fingerprint for verification.
For security, get the host key fingerprint and compare it with the one displayed. If they do not match, you may be experiencing a man-in-the-middle attack. Switch to a secure network and try again.
After confirming the fingerprint, enter
yesand press Enter.The authenticity of host '47.98.xxx.xxx (47.98.xxx.xxx)' can't be established. ED25519 key fingerprint is SHA256:AbCdEf123456... This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? -
Enter the password.
No characters appear on screen as you type. Press
Enterwhen done.On success, a welcome message appears and the prompt changes to
[<username>@<hostname> ~]$.Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
Connect with a key pair
-
Open PowerShell.
Press
Win+R, enterpowershell, and then pressEnter. -
Connect to the instance.
ssh -i /path/to/private_key.pem <instance_username>@<instance_public_IP_address>Example:
ssh -i /path/to/private_key.pem root@47.98.xxx.xxx. In this command,/path/to/private_key.pemis the path to your private key file, such asC:\Users\Administrator\Downloads\private_key.pem. -
Verify the host fingerprint (first-time only).
On first connection, the SSH client displays the host key fingerprint for verification.
For security, get the host key fingerprint and compare it with the one displayed. If they do not match, you may be experiencing a man-in-the-middle attack. Switch to a secure network and try again.
After confirming the fingerprint, enter
yesand pressEnter.The authenticity of host '47.98.xxx.xxx (47.98.xxx.xxx)' can't be established. ED25519 key fingerprint is SHA256:AbCdEf123456... This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? -
Authenticate and access the instance.
On success, a welcome message appears and the prompt changes to
[<username>@<hostname> ~]$.Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
macOS
Connect with a password
-
Open Terminal.
-
Connect to the instance.
ssh <instance_username>@<instance_public_IP_address>Example:
ssh root@47.98.xxx.xxx -
Verify the host fingerprint (first-time only).
On first connection, the SSH client displays the host key fingerprint for verification.
For security, get the host key fingerprint and compare it with the one displayed. If they do not match, you may be experiencing a man-in-the-middle attack. Switch to a secure network and try again.
After confirming the fingerprint, enter
yesand pressEnter.The authenticity of host '47.98.xxx.xxx (47.98.xxx.xxx)' can't be established. ED25519 key fingerprint is SHA256:AbCdEf123456... This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? -
Enter the password.
No characters appear on screen as you type. Press
Enterwhen done.On success, a welcome message appears and the prompt changes to
[<username>@<hostname> ~]$.Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
Connect with a key pair
-
Open Terminal.
-
Connect to the instance.
# chmod 400: Sets read-only permissions for the owner of the private key file. This is a security requirement of the SSH client. chmod 400 /path/to/private_key.pem ssh -i /path/to/private_key.pem <instance_username>@<instance_public_IP_address>Example:
ssh -i /path/to/private_key.pem root@47.98.xxx.xxx. In this command,/path/to/private_key.pemis the path to your private key file. -
Verify the host fingerprint (first-time only).
On first connection, the SSH client displays the host key fingerprint for verification.
For security, get the host key fingerprint and compare it with the one displayed. If they do not match, you may be experiencing a man-in-the-middle attack. Switch to a secure network and try again.
After confirming the fingerprint, enter
yesand pressEnter.The authenticity of host '47.98.xxx.xxx (47.98.xxx.xxx)' can't be established. ED25519 key fingerprint is SHA256:AbCdEf123456... This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? -
Authenticate and access the instance.
On success, a welcome message appears and the prompt changes to
[<username>@<hostname> ~]$.Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
Method 2: Use the Xshell client (Windows only)
Xshell is a Windows SSH client for managing Linux servers.
Prerequisites
-
Xshell: Download and install the latest client from the official Xshell website.
-
Instance public IP address: In ECS console - Instances, open the instance details page and find Public IP Address in the Configuration Information section.
-
Logon credentials: Set a password or bind a key pair for the instance.
-
Security group: Add an inbound rule to allow SSH access on port 22 from your local IP address.
Procedure
-
Start Xshell and create a session.
-
Open the Xshell application.
-
In the Sessions window that appears, click New. Alternatively, from the menu bar, select .
-
-
Configure the connection.
In the left navigation pane, click Connection and configure:
-
Name: A descriptive session name, such as My-Web-Server.
-
Protocol: Default SSH.
-
Host: The instance's public IP address.
-
Port Number: Default 22.
-
-
Configure user authentication.
In the left navigation pane, click Authentication.
Connect with a password
-
Method: Select Password.
-
User Name: The logon username, such as root.
-
Password: The logon password.
Connect with a key pair
-
User Name: The logon username, such as root.
-
Method: Select Public Key and configure the user key as follows:
-
Click Settings....
-
Select the Key File option. Click ... next to User Key, click Import..., and then select the
.pemprivate key file from your local storage. -
After import, select the key and click OK.
-
(Optional) If your key file is password-protected, enter the Password.
-
-
-
Connect to the instance.
Click Connect.
-
Verify the host key (first-time only).
On first connection, Xshell displays an SSH Security Warning with the host key fingerprint.
For security, get the host key fingerprint and compare it with the one displayed. If they do not match, you may be experiencing a man-in-the-middle attack. Switch to a secure network and try again.
After verifying, click Accept and Save.
-
Access the instance.
The command prompt indicates a successful connection.
Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
Apply in production
Harden your SSH connections for production environments.
-
Verify the host fingerprint to prevent man-in-the-middle attacks
On first connection, verify the host key fingerprint to confirm you are connecting to the correct instance.
-
Disable password-based logon and enforce key pair authentication
Key pair authentication is more secure than passwords and reduces the risk of brute-force attacks.
-
Bind a key pair to your instance.
-
Log on to the instance, edit
/etc/ssh/sshd_config, and setPasswordAuthenticationtono. Restart the SSH service.
-
-
Change the default SSH port
Changing port 22 to a non-standard port (such as 2222) reduces exposure to automated scans.
-
Allow the new port: Add an inbound rule to allow traffic on the new port.
-
Change the SSH port: Log on to the instance, edit
/etc/ssh/sshd_config, and change#Port 22toPort 2222. Restart the SSH service. -
Connect with the new port: Specify the port with
-p, for example:ssh -p 2222 username@instance_ip.
-
-
Allow access only from trusted IP addresses
Modify security group rules to allow SSH access only from trusted IP addresses.
FAQ
-
How do I configure a security group rule for port 22?
In the instance's security group, add a rule with these settings:
Action
Protocol
Source
Destination (This Instance)
Allow
Custom TCP
Your local client's public IP address.
ImportantUsing
0.0.0.0/0allows any IP address to access the port, posing a security risk.SSH(22)
If you changed the SSH port, use the new port number.
-
How do I verify the instance's host key fingerprint?
On first connection, the SSH client prompts you to verify the host key fingerprint.
In the console
-
Go to ECS console - Instances. Select a region and resource group.
-
Find the instance and click . Find
BEGIN SSH HOST KEY FINGERPRINTSto view the fingerprints.
Verify that the fingerprint displayed by your SSH client exactly matches one in the output. A mismatch may indicate a man-in-the-middle attack.
If this section is missing, log on to the instance to view the fingerprint.
In the instance
Log on to the instance using Workbench and run the following command:
for f in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -l -f "$f"; doneSample output:
1024 SHA256:9C******co root@Connect-Instance-Example (DSA) 256 SHA256:u6******SU root@Connect-Instance-Example (ECDSA) 256 SHA256:iQ******jg root@Connect-Instance-Example (ED25519) 3072 SHA256:8R******64 root@Connect-Instance-Example (RSA)Verify that the fingerprint displayed by your SSH client exactly matches one in the output. A mismatch may indicate a man-in-the-middle attack.
-
-
How can I simplify the connection command with an SSH config file?
Create an SSH
configfile on your local machine to define server aliases.-
Find or create the config file.
Windows 10/11
Default path:
C:\Users\YourUsername\.ssh\config. Create the file if it does not exist.Replace
YourUsernamewith your current Windows username.macOS
Default path:
~/.ssh/config. Create the file if it does not exist. -
Edit the config file and add instance information.
Open the
configfile and add aHostblock for each server.# Configure an alias "web-server" for the web server Host web-server HostName 47.98.xxx.xxx User root Port 22 # (Optional) If you use a key pair to log on, specify the private key path. Ignore this if you use a password. IdentityFile /path/to/your/private_key.pem # You can add more configurations for other servers Host other-server HostName 8.123.xxx.xxx User ecs-user Port 2222 IdentityFile ~/.ssh/another_key.pemParameter description:
-
Host: A custom alias.
-
HostName: The public IP address.
-
User: The logon username.
-
Port: The SSH port (default: 22).
-
IdentityFile: The private key file path.
-
-
Connect using the alias.
Save the
configfile. Connect using the alias:# Connect directly using the alias. SSH automatically reads the IP address, username, and key information from the config file. ssh web-server
-
-
Why do I get a
Connection timed outerror?The client failed to reach the server. Check the following:
-
The public IP address is correct.
-
The security group allows traffic on the required port.
-
The instance is Running.
-
Use the ECS console - Self-service Troubleshooting tool to diagnose any issues.
-
-
Why do I get a
Permission denied, please try againerror?The server rejected your password. Check the following:
-
Reset the password in the console and try again.
-
Use the ECS console - Self-service Troubleshooting tool to diagnose any issues.
-
-
Why do I get a
Permission denied (publickey)error?The server rejected your key. Check the following:
-
Bind the key pair again in the console and retry.
-
The path to the private key file is correct and matches the key pair bound to the instance.
-
(On macOS) The private key file permissions are
400or600. -
Use the ECS console - Self-service Troubleshooting tool to diagnose any issues.
-
-
Why do I get a
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!error?This SSH security feature triggers when the server's host key changes. This can happen after a system disk change, OS reinstallation, or host key file deletion.
Solution: Verify the host key fingerprint. If correct, remove the outdated fingerprint:
ssh-keygen -R <instance_public_IP_address>