On macOS or Windows 10/11, you can use the built-in OpenSSH command-line tool to connect to your Linux Elastic Compute Service (ECS) instance. Alternatively, you can use the Xshell client on Windows. Both methods support authentication using a password or a key pair.
We recommend using Workbench to connect to your Alibaba Cloud instances. This browser-based tool supports password-free logon and is more convenient than using OpenSSH or Xshell.
Usage notes
The instance runs a Linux operating system.
The instance has a static public IP address or an associated Elastic IP Address (EIP).
Method 1: Use an OpenSSH client (command line)
OpenSSH is the standard Secure Shell (SSH) client built into macOS and modern Windows operating systems, letting you connect quickly from the command line.
Before you connect
Instance public IP address: In the ECS console - Instances, find the target instance and open its details page. In the Configuration Information section, find the Public IP Address.
Instance Logon credentials: Set a password or bind a key pair for the instance.
Configure a security group: Add an inbound rule to the instance's security group 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
.Start the remote connection.
ssh <instance_username>@<instance_public_IP_address>
Example:
ssh root@47.98.xxx.xxx
(First-time connection) Verify the host fingerprint.
When you connect to a new ECS instance for the first time, a message similar to the following appears, asking you to verify the host key fingerprint.
This is an SSH security mechanism. For security, get the host key fingerprint of the instance 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 to connect again.
After confirming the host key fingerprint is correct, enter
yes
and 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 to access the instance.
When you enter the password, no characters appear on the screen. This is normal. Press
Enter
after you finish typing.Once authenticated, you will see a welcome message, and the command prompt changes to
[<username>@<hostname> ~]$
. This indicates a successful logon.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
.Start the remote connection.
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.pem
is the path to your private key file, such asC:\Users\Administrator\Downloads\private_key.pem
.(First-time connection) Verify the host fingerprint.
When you connect to a new ECS instance for the first time, a message similar to the following appears, asking you to verify the host key fingerprint.
This is an SSH security mechanism. For security, get the host key fingerprint of the instance 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 to connect again.
After confirming the host key fingerprint is correct, enter
yes
and 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 with the key to access the instance.
Once the key is authenticated, you will see a welcome message, and the command prompt changes to
[<username>@<hostname> ~]$
. This indicates a successful logon.Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
macOS
Connect with a password
Open Terminal.
Start the remote connection.
ssh <instance_username>@<instance_public_IP_address>
Example:
ssh root@47.98.xxx.xxx
(First-time connection) Verify the host fingerprint.
When you connect to a new ECS instance for the first time, a message similar to the following appears, asking you to verify the host key fingerprint.
This is an SSH security mechanism. For security, get the host key fingerprint of the instance 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 to connect again.
After confirming the host key fingerprint is correct, enter
yes
and 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 to access the instance.
When you enter the password, no characters appear on the screen. This is normal. Press
Enter
after you finish typing.After authentication, the system's welcome message appears, the command prompt changes to
[<username>@<hostname> ~]$
. This indicates a successful logon.Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
Connect with a key pair
Open Terminal.
Start the remote connection.
# 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.pem
is the path to your private key file.(First-time connection) Verify the host fingerprint.
When you connect to a new ECS instance for the first time, a message similar to the following appears, asking you to verify the host key fingerprint.
This is an SSH security mechanism. For security, get the host key fingerprint of the instance 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 to connect again.
After confirming the host key fingerprint is correct, enter
yes
and 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 the key and access the instance.
Once the key is authenticated, you will see a welcome message, and the command prompt changes to
[<username>@<hostname> ~]$
. This indicates a successful logon.Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
Method 2: Use the Xshell client (Windows only)
Xshell is an SSH client for Windows used to manage Linux servers.
Before you connect
Download and install Xshell: Go to the official Xshell website to download and install the latest client.
Instance Public IP address: In the ECS console - Instances, find the target instance, and open its details page. In the Configuration Information section, find the Public IP Address.
Instance Logon credentials: Set a password or bind a key pair for the instance.
Configure a security group: Configure an inbound rule to the instance's security group to allow SSH access on port 22 from your local IP address.
Procedure
Start Xshell and create a new 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 the following parameters:
Name: Enter a descriptive name for the session, for example, My-Web-Server.
Protocol: Keep the default, SSH.
Host: Enter the instance's public IP address.
Port Number: Keep the default, 22.
Configure user authentication.
In the left navigation pane, click Authentication.
Connect with a password
Method: Select Password.
User Name: Enter the logon name for the server, such as root.
Password: Enter the corresponding logon password.
Connect with a key pair
User Name: Enter the logon name for the server, 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
.pem
private 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.
(First-time connection) Verify the host key.
When you connect to a new ECS instance for the first time, Xshell displays an SSH Security Warning window with the host key fingerprint.
This is an SSH security mechanism. For security, get the host key fingerprint of the instance 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 to connect again.
After verifying the key, click Accept and Save.
Access the instance.
When the command prompt appears, you have successfully connected.
Welcome to Alibaba Cloud Elastic Compute Service ! [root@Connect-Instance-Example ~]#
Apply in production
To enhance the security of your remote connection in a production environment, follow these best practices.
Verify the host fingerprint to prevent man-in-the-middle attacks
When connecting to an instance for the first time, verify the host key fingerprint of the instance to ensure you are connecting to the correct instance, not an attacker's server.
Disable password-based logon and enforce key pair authentication
Key pair authentication is significantly more secure than password authentication and reduces the risk of brute-force attacks.
Bind a key pair to your instance.
Log on to the instance, edit the
/etc/ssh/sshd_config
configuration file, and change the value ofPasswordAuthentication
tono
. Restart the SSH service for the changes to take effect.
Change the default SSH port
Changing the default port 22 to a non-standard port (such as 2222) reduces exposure to automated scans.
Allow the new port: Add an inbound rule to the instance's security group to allow traffic on the new port.
Change the SSH service port: Log on to the instance, edit the
/etc/ssh/sshd_config
configuration file, and change#Port 22
toPort 2222
. Restart the SSH service for the changes to take effect.Connect using the new port: When connecting, you must specify the new port with the
-p
flag. 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 your local IP or other 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 the following settings:
Action
Protocol
Source
Destination (This Instance)
Allow
Custom TCP
Your local client's public IP address.
ImportantUsing
0.0.0.0/0
allows any IP address to access the port, which poses a security risk. Use it with caution.SSH(22)
If you changed the SSH port, use the new port number.
How do I verify the instance's host key fingerprint?
When you connect to an instance for the first time, you are prompted to verify the host key fingerprint.
In the console
Go to ECS console - Instances. In the upper-left corner, select a region and resource group.
Find the instance and click
. Then, findBEGIN SSH HOST KEY FINGERPRINTS
. The host fingerprints are displayed.Check if the fingerprint displayed by your SSH client exactly matches a fingerprint in the output. If they do not match, you may be experiencing a man-in-the-middle attack. Switch to a secure network and try to connect again.
If you cannot find this section, log on to the instance to view the host fingerprint.
In the instance
Log on to the instance using Workbench and run the following command to view the host key fingerprint:
for f in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -l -f "$f"; done
Sample 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)
Check if the fingerprint displayed by your SSH client exactly matches a fingerprint in the output. If they do not match, you may be experiencing a man-in-the-middle attack. Switch to a secure network and try to connect again.
How can I simplify the connection command with an SSH config file?
To simplify the connection command, create and configure an SSH
config
file on your local machine to set an alias for your server.Find or create the config file.
Windows 10/11
The default path of the config file is
C:\Users\YourUsername\.ssh\config
. If the file does not exist, create it manually.Replace
YourUsername
with your current Windows username.macOS
The default path of the config file is
~/.ssh/config
. If the file does not exist, create it manually.Edit the config file and add instance information.
Open the
config
file in a text editor and add aHost
block 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.pem
Parameter description:
Host: A custom alias for the server.
HostName: The instance's public IP address.
User: The logon username.
Port: The SSH port number (default is 22).
IdentityFile: The absolute path to the private key file.
Connect using the alias.
Save the
config
file. You can now 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 out
error?This error occurs when the client fails to establish a connection with 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 again
error?This error means 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?This error means the server rejected your key. Check the following:
Bind the key pair again in the console and retry.
The patch to the private key file is correct. The private key matches the key pair associated with the instance.
(On macOS) The private key file permissions are
400
or600
.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 fingerprint changes after your first connection. This can happen if you change the system disk, reinstall the operating system, or delete the host key files on the instance.
Solution: Verify the host key fingerprint of the instance. If it is correct, run the following command on your local machine to remove the outdated fingerprint.
ssh-keygen -R <instance_public_IP_address>