edit-icon download-icon

Customize yum repository, NTP service, and DNS service

Last Updated: Oct 30, 2017

User-defined scripts are a type of script provided by Alibaba Cloud for users to customize the startup behaviors of ECS instances. For details, see User-defined data.

This example uses a Linux instance to demonstrate how to use a user-defined script to configure your own yum repository, NTP service, and DNS service when creating a Linux instance. User-defined scripts also enable you to configure NTP service and DNS service for a Windows instance.


When a Linux instance is started, Alibaba Cloud automatically configures a pre-defined yum repository, NTP service, and DNS service for the instance. However, if you want to have your own yum repository, NTP service, and DNS service, use user-defined scripts to implement them.


  • If you are using a custom yum repository, Alibaba Cloud does not provide support for it.
  • If you are using a custom NTP service, Alibaba Cloud does not provide time service.


To customize your yum repository, NTP service, and DNS service for a Linux instance when creating it, follow these steps:

  1. Log on to the ECS console and create an instance. Configure the instance as follows:

    • Network Type: Select VPC.
    • Instance Type: Select an I/O-optimized instance.
    • Operating System: Select CentOS 7.2 in Public Image tab.
    • Security Setup: Select one to meet your requirements.
  2. Enter the following script in the User Data box on the instance creation page.

    1. #!/bin/sh
    2. # Modify DNS
    3. echo "nameserver" | tee /etc/resolv.conf
    4. # Modify yum repo and update
    5. rm -rf /etc/yum.repos.d/*
    6. touch myrepo.repo
    7. echo "[base]" | tee /etc/yum.repos.d/myrepo.repo
    8. echo "name=myrepo" | tee -a /etc/yum.repos.d/myrepo.repo
    9. echo "baseurl=http://mirror.centos.org/centos" | tee -a /etc/yum.repos.d/myrepo.repo
    10. echo "gpgcheck=0" | tee -a /etc/yum.repos.d/myrepo.repo
    11. echo "enabled=1" | tee -a /etc/yum.repos.d/myrepo.repo
    12. yum update -y
    13. # Modify NTP Server
    14. echo "server ntp1.aliyun.com" | tee /etc/ntp.conf
    15. systemctl restart ntpd.service


    • The first line must be #!/bin/sh, with no leading space.
    • Do not add unnecessary spaces or carriage return characters in the full text.
    • You can customize URLs of your own DNS server, NTP Server, and yum repository based on the instance situations.
    • The preceding content applies to CentOS 7.2. If you are using other images, modify the scripts as needed.
    • You can also define the yum repository in the scripts of the Cloud Config type, but it is not recommended because it is not flexible enough to get adapted to Alibaba Cloud that may pre-configure some yum repository. Scripts of script type is recommended for changing the yum repository.
      ECS _ UserData _ NTPDNSYUM
  3. After you complete the configuration, click Buy Now and activate the instance following the instructions on the page.

After the instance is created, you can connect to the instance to view the implementation details, as shown in the following figure.
ECS _ UserData _ NTPDNSYUM Result

The preceding figure shows that you have successfully customized the DNS service, the NTP service, and the yum repository.

Thank you! We've received your feedback.