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:
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.
Enter the following script in the User Data box on the instance creation page.
# Modify DNS
echo "nameserver 18.104.22.168" | tee /etc/resolv.conf
# Modify yum repo and update
rm -rf /etc/yum.repos.d/*
echo "[base]" | tee /etc/yum.repos.d/myrepo.repo
echo "name=myrepo" | tee -a /etc/yum.repos.d/myrepo.repo
echo "baseurl=http://mirror.centos.org/centos" | tee -a /etc/yum.repos.d/myrepo.repo
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
# Modify NTP Server
echo "server ntp1.aliyun.com" | tee /etc/ntp.conf
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 Configtype, 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
scripttype is recommended for changing the yum repository.
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.
The preceding figure shows that you have successfully customized the DNS service, the NTP service, and the yum repository.