没有不值得去解决的问题,也没有不值得去学习的技术!

Say Goodbye to Passwords and Disconnects: SSH Passwordless Login and Keepalive Practices for Alibaba Cloud ECS

As shown in the screenshot, I ran mv, chmod, and nano in succession, and finally ssh aliyun in one smooth flow. It displayed the welcome message and the last login time immediately, without any password prompt.

Tired of entering your password every time you run ssh root@xxx? Does the terminal freeze after ten minutes of inactivity? This article shares a complete solution that lets you log in to Alibaba Cloud ECS from an Ubuntu terminal with a single command, no password required, while keeping the connection from ever freezing.

Background: Two Annoying Problems

As a developer who frequently connects to Alibaba Cloud ECS from an Ubuntu terminal, I run this command every day:

Bash
ssh root@121.40.248.29

Then I type a long password.

Then type a long password.

Even more frustratingly, sometimes I step away to grab a glass of water or read a document, only to come back and find the terminal “dead”—pressing Ctrl+C does nothing, pressing Enter does nothing either. I have no choice but to close the window, which then prompts “some processes are still running.”

Even more frustratingly, sometimes I step away to grab a glass of water or read a document, only to come back and find the terminal "dead"—pressing Ctrl+C does nothing, pressing Enter does nothing either. I have no choice but to close the window, which then prompts "some processes are still running."

On my Alibaba Cloud ECS instance in Hangzhou (Alibaba Cloud Linux 3), I figured out two simple and effective solutions through trial and error:

  1. SSH key-based passwordless login – say goodbye to entering passwords
  2. Configure keepalive heartbeat – say goodbye to frozen connections

Below is the complete walkthrough, accompanied by real screenshots from the console and terminal.


Step 1: Create a Key Pair in the Alibaba Cloud Console

Log in to the Alibaba Cloud ECS Console and find “Network & Security” > “Key Pairs” in the left navigation pane.

Figure 3: Key pair management entry

Click “Create Key Pair” to bring up the form:

  • Key Pair Name: Enter a memorable name, such as Ubuntu-26.04
  • Creation Type: Select “Auto-Create Key Pair” (recommended; the system will generate a public key and automatically host it, while you download and keep the private key)
  • Resource Group: Optional; generally, you do not need to select one and can just skip this
Figure 4: Fill in the key pair information

After clicking OK, the browser will automatically download a .pem file (the private key). This is your only chance to download it, so keep it safe. My file name is Ubuntu-26.04.pem.

Step 2: Bind the Key Pair to the ECS Instance

Find the Ubuntu-26.04 you just created in the key pair list, and click “Bind Key Pair”.

Figure 5: Select the instance to bind

Check your target ECS instance (the one with IP 121.40.248.29), and click OK. The system will prompt: “Restarting the instance is required for it to take effect”. Confirm the instance restart during an off-peak period.

💡 You can restart from the console, or log in to the instance and run reboot. The key pair binding only takes effect after the restart.

Step 3: Configure the Private Key and SSH Client Locally

Return to your local Ubuntu terminal (I am using a ThinkPad T570 + Ubuntu 26.04).

1. Move the Private Key File and Set Permissions

My Ubuntu-26.04.pem was downloaded to the ~/下载/ directory (a Chinese path). First, create the .ssh directory, then move and rename the file:

Bash
mkdir -p ~/.ssh
mv ~/下载/Ubuntu-26.04.pem ~/.ssh/aliyun.pem
chmod 600 ~/.ssh/aliyun.pem   # 必须!否则 SSH 会报权限过宽的错误

⚠️ If it was downloaded to ~/Downloads/, adjust the path accordingly.

2. Configure the SSH Client (Simplify the Login Command)

Edit (or create) the ~/.ssh/config file:

Bash
nano ~/.ssh/config

Add the following content:

Plaintext
Host aliyun
    HostName 121.40.248.29
    User root
    IdentityFile ~/.ssh/aliyun.pem
    ServerAliveInterval 60      # Keepalive heartbeat, sends an empty packet every 60 seconds

Save and exit (Ctrl+X, then Y, then Enter).

  • Host aliyun is a custom alias; from now on, you only need to run ssh aliyun.
  • ServerAliveInterval 60 is the key to solving the “no response after 10 minutes” issue.

3. Test Passwordless Login

Bash
ssh aliyun

As shown in the screenshot, I ran mv, chmod, and nano in succession, and finally ssh aliyun in one smooth flow. It displayed the welcome message and the last login time immediately, without any password prompt.

As shown in the screenshot, I ran mv, chmod, and nano in succession, and finally ssh aliyun in one smooth flow. It displayed the welcome message and the last login time immediately, without any password prompt.

Step 4: The Principle Behind Fixing the Frozen Connection

The “no response after 10 minutes” issue occurred earlier because intermediate network devices (such as firewalls and NAT) considered the idle connection to have no data transmission and disconnected it. The purpose of ServerAliveInterval 60 is to have the local SSH client automatically send an empty “heartbeat” packet to the server every 60 seconds, keeping the connection active. Even if you stare blankly for an hour, the connection will not drop.

Security Recommendation: Restrict Security Group Source IPs

To further enhance security, I recommend configuring a security group in the Alibaba Cloud console to allow only your local public IP to access port 22:

  1. Run curl ifconfig.me locally to get your current public IP.
  2. Go to ECS Instance Details → Security Groups → Configure Rules → Inbound.
  3. Find the SSH (22) rule, and change the authorized object from 0.0.0.0/0 to 你的IP地址/32.

This way, even if the private key is accidentally leaked, no one else can log in to your server from a different IP.

Summary

Through these steps, I have completely said goodbye to:

  • ❌ Entering a password every time
  • ❌ The connection freezing for no reason after ten minutes

Now I just type ssh aliyun in the terminal to log in instantly. When I come back after doing other things, the terminal still responds smoothly. Moreover, the entire process uses no third-party software (like PuTTY); it relies purely on native SSH configuration and runs perfectly in an Ubuntu terminal.

If you are also troubled by the same issues, give this guide a try. I believe you can get it done in under ten minutes.

Final reminder: Keep the downloaded .pem private key file safe, and do not upload it to public code repositories. If you need passwordless login from multiple computers, you can reuse the same key pair (just copy the private key to the ~/.ssh/ directory on the other computers and set the permissions).


Test environment: Ubuntu 26.04 (local) + Alibaba Cloud Linux 3 (ECS, Hangzhou node)
If you have any questions, feel free to leave a comment and discuss.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系