Say goodbye to passwords and disconnects: Alibaba Cloud ECS SSH password-free login and heartbeat preservation practice
Every time
ssh root@xxxHave to enter the password? After connecting to the server for ten minutes, it gets stuck without operating it? This article shares a complete set of solutions, so that you can log in to Alibaba Cloud ECS with one-click password-free in Ubuntu terminal, and at the same time keep the connection and never freeze.
Background: Two annoying questions
As a developer who often connects to Alibaba Cloud ECS in the Ubuntu terminal, I do it every day:
ssh root@121.40.248.29
Then enter a long list of passwords.

What’s even worse is that sometimes I pour a glass of water, read a document, and come back to find that the terminal is ‘dead’ – press Ctrl+C No response, press Enter There was no response, so I had no choice but to close the window, and it also prompted ‘some processes are still running’.

On Alibaba Cloud Hangzhou ECS (Alibaba Cloud Linux 3), I found two simple and effective solutions through fumble:
- SSH key password-free login – Farewell to enter the password
- Configure the heartbeat to keep alive – Farewell to the connection stuck
Below is the complete practice process, with real screenshots of the console and terminal.
Step 1: Create a key pair in the Alibaba Cloud console
Log in Alibaba Cloud ECS Console, found in the left navigation bar ‘Network and Security’ > ‘Key Pair’.

Click on ‘Create a key pair’and pop up the form:
- key pair name: Enter an easy-to-remember name, such as
Ubuntu-26.04 - create type: select ‘Automatically create key pairs’(Recommendation, the system will generate the public key and host it automatically, and the private key will be stored and stored by you)
- resource group: Optional, generally do not need to choose, just skip it

Click on Determine After that, the browser will automatically download a .pem file (private key).This is the only download opportunity, please save it properly. my filename is ubuntu-26.04.pem.
Step 2: Bind the key pair to the ECS instance
Find just created in the key pair list Ubuntu-26.04, click ‘Bind key pair’.

Check your target ECS instance (ip is 121.40.248.29 of the one), click OK. The system will prompt ‘Restart the instance is required to take effect’. In the low peak period, confirm the restart instance.
💡 Restart can be operated in the console, or you can log in to the instance to execute
reboot. After restarting, the key pair binding will only take effect.
Step 3: Configure the private key to the SSH client locally
Back to your Ubuntu local terminal (I am ThinkPad T570 + Ubuntu 26.04).
1. Move the private key file and set permissions
Mine ubuntu-26.04.pem Downloaded ~/download/ Directory (Chinese path). Create first .ssh Directory, then move and rename:
mkdir -p ~/.ssh
mv ~/下载/Ubuntu-26.04.pem ~/.ssh/aliyun.pem
chmod 600 ~/.ssh/aliyun.pem # 必须!否则 SSH 会报权限过宽的错误
⚠️ If downloaded
~/downloads/, please adjust the path accordingly.
2. Configure the SSH client (simplify the login command)
Edit (or create)~/.ssh/config file:
nano ~/.ssh/config
Add the following:
Host aliyun
HostName 121.40.248.29
User root
IdentityFile ~/.ssh/aliyun.pem
ServerAliveInterval 60 # 心跳保活,每60秒发一个空包
save exit (ctrl+x, and then y, again Enter).
host aliyunis a custom alias, only laterssh aliyuncan.ServerAliveInterval 60It is the key to solving ‘no response after 10 minutes’.
3. Test password-free login
ssh aliyun
As can be seen from the screenshot, I have executed continuously mv,chmod,nano, finally ssh aliyun In one go, the welcome message and the last login time are directly displayed, without any password to inquire.

Step 4: Solve the principle of connection stuck
The ‘no response after 10 minutes’ that appeared before is because network intermediate devices (such as firewalls, NAT) think that the idle connection has no data transmission, so it is disconnected.ServerAliveInterval 60 The function is to let the local SSH client automatically send an empty ‘heartbeat’ to the server every 60 seconds to maintain the active state of the connection. Even if you are in a daze for an hour, the connection will not be interrupted.
Security advice: Restrict security group source IP
In order to further improve security, it is recommended to configure the security group in the Alibaba Cloud console, and only allow your local public network IP to access port 22:
- perform locally
curl ifconfig.meGet the current public network IP. - Go to ECS Instance Details → Security Groups → Configuration Rules → Enter Direction.
- Find the ssh(22) rule, the authorization object will be
0.0.0.0/0ToYour IP address/32.
In this way, even if the private key is accidentally leaked, others will not be able to log in to your server from other IPs.
Summary
Through the above steps, I have completely said goodbye:
- ❌ Enter the password each time
- ❌ Stuck for no reason after connecting for ten minutes
Now just need to knock at the terminal ssh aliyun You can log in in seconds, do other things back, and the terminal still responds smoothly. And the whole process does not use any third-party software (such as putty), pure native SSH configuration, which runs perfectly under the Ubuntu terminal.
If you are also trapped in the same troubles, you might as well follow this article, I believe it will be done within ten minutes.
Final reminder: Please keep the download .pem Private key files, do not upload to the public code repository. If you need multiple computers to log in without a password, you can reuse the same key pair (copy the private key to another computer’s ~/.ssh/ directory and set permissions).
Practice environment: Ubuntu 26.04 (local) + Alibaba Cloud Linux 3 (ECS, Hangzhou node)
If you have any questions, please leave a message.