ECS server custom script

The ECS custom script, that is, user data, is a capability provided by ECS for users to customize the startup behavior of an instance. Users can enter custom scripts to set their own user data when purchasing an instance. User-defined scripts are usually executed after the instance is purchased.


User data can solve many problems. For example, when creating instances, the user wants to personalize the configuration for these instances.
User does not want to use the default root user
Users do not want to use Alibaba Cloud's yum source/NTP service
User wants to automatically deploy an app on creation
What other scripts can do....
User data is fully compatible as long as the script can do it. Such as the following script:


The first function is to modify the DNS configuration


#Modify DNS

echo "nameserver 8.8.8.8"|tee /etc/resolv.conf


The second is to modify the yum source

modify yum repo and update



rm -rf /etc/yum.repos.d/*


touch myrepo.repo


echo "[base]"|tee -a /etc/yum.repos.d/myrepo.repo


echo "baseurl=http://mirror.centos.org/centos"|tee -a /etc/yum.repos.d/myrepo.repo


Click me to receive Alibaba Cloud 2,000 yuan voucher, (the role of Alibaba Cloud coupons: when purchasing Alibaba Cloud products, when the final payment is settled, Alibaba Cloud
Coupon can be used to deduct part of the fee.

echo "gpgcheck=0"|tee -a /etc/yum.repos.d/myrepo.repo


echo "enabled=1"|tee -a /etc/yum.repos.d/myrepo.repo


yum update -y


The third is to modify the NTP service


modify NTP Server

echo "server ntp1.aliyun.com"|tee /etc/ntp.conf


systemctl restart ntpd.service


Next, we will demonstrate the process of using user data.


First, create an instance. If you don't have a server instance, you can click here to buy one. It should be noted that currently, user data only supports Linux systems under the VPC network.


Click me to receive Alibaba Cloud 2,000 yuan voucher, (the role of Alibaba Cloud coupons: when purchasing Alibaba Cloud products, when the final payment is settled, Alibaba Cloud
Coupon can be used to deduct part of the fee


You can see a user data box below. It should be noted that the first line of the input format must be the #!/bin/sh string, and then paste the prepared user data content into it.


This sets up a script for user data when the instance is created.


Let's take a look at this script mainly for three functions: the first is to modify the DNS configuration; the second is to modify yum; the third is to set the NTP service.


After the instance is started, use the SSH remote connection tool to go in and take a look

Then we use this command to test the user data. We can see that the user data has been configured successfully, and the three functions we expected before can be run normally.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00