All Products
Search
Document Center

Elastic Compute Service:Configure a custom Linux image

Last Updated:May 15, 2026

Add a parsing script to a Linux image that uses an OS version unsupported by ECS and cannot use cloud-init, enabling automatic configuration on first boot.

Note

A Linux image with an unsupported OS version is treated as a non-standard platform image. These images are based on standard operating systems but have system files, environments, or applications that do not meet platform requirements. Select one of the following OS Version options when importing:

  • Others Linux: ECS does not process instances created from this image type. You must manually configure the IP address, routes, password, and other settings after instance creation.

  • Customized Linux: Requires the configuration described in this topic before import.

Prerequisites

The image must meet the following requirements:

  • The first partition (typically /dev/sda1 or /dev/vda1) has write permissions.

  • The first partition uses FAT32, EXT2, EXT3, EXT4, or UFS as its file system.

    Note

    Run blkid /dev/sdXn to check the file system type. Replace /dev/sdXn with the actual first partition path, such as /dev/sda1.

  • The image virtual size exceeds 5 GiB.

    Note

    Run df -h /dev/sdXn to check the partition space. Replace /dev/sdXn with the actual first partition path, such as /dev/sda1.

Procedure

  1. Log on to the source virtual machine as the root user.

  2. Create a directory named aliyun_custom_image in the root directory of the first partition.

    mkdir /aliyun_custom_image

    On first boot, Alibaba Cloud writes instance information, such as the hostname, root user password, and DNS server addresses, to the os.conf file in the aliyun_custom_image directory. The system creates os.conf automatically if it does not exist.

    Sample os.conf content:

    hostname=<yourHostName>
    password=<yourPassword>
    eth0_ip_addr=10.0.0.2
    eth0_mac_addr=00:xx:xx:xx:xx:23
    eth0_netmask=255.255.255.0
    eth0_gateway=10.0.0.1
    eth0_route="0.0.0.0/0 10.0.0.1"
    dns_nameserver="7.7.X.X 8.8.8.8"

    Parameters in the sample:

    Parameter

    Description

    hostname

    The instance hostname.

    password

    The password of the root user.

    eth0_ip_addr

    The IP address of the eth0 NIC.

    eth0_mac_addr

    The MAC address of the eth0 NIC.

    eth0_netmask

    The subnet mask of the eth0 NIC.

    eth0_gateway

    The default gateway IP address of eth0.

    eth0_route

    Internal routes for eth0. Space-separated.

    dns_nameserver

    DNS server addresses. Space-separated.

  3. Create a parsing script, such as a service file named customized-config.service, to read and apply system configurations from the os.conf file.

    This script runs on first boot to configure the hostname, root user password, DNS server addresses, and network settings.

    Sample script:

    #!/bin/bash
    
    ### BEGIN INIT INFO
    # Provides:          os-conf
    # Required-Start:    $local_fs $network $named $remote_fs
    # Required-Stop:
    # Should-Stop:
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: The initial os-conf job, config the system.
    ### END INIT INFO
    
    first_partition_dir='/boot/'
    os_conf_dir=${first_partition_dir}/aliyun_custom_image
    os_conf_file=${os_conf_dir}/os.conf
    
    load_os_conf() {
        if [[ -f $os_conf_file ]]; then
            . $os_conf_file
            return 0
        else
            return 1
        fi
    }
    
    cleanup() {
        # ensure $os_conf_file is deleted, to avoid repeating config system
        rm $os_conf_file >& /dev/null
        # ensure $os_conf_dir exists
        mkdir -p $os_conf_dir
    }
    
    config_password() {
        if [[ -n $password ]]; then
            password=$(echo $password | base64 -d)
            if [[ $? == 0 && -n $password ]]; then
                echo "root:$password" | chpasswd
            fi
        fi
    }
    
    config_hostname() {
        if [[ -n $hostname ]]; then
            sed -i "s/^HOSTNAME=.*/HOSTNAME=$hostname/" /etc/sysconfig/network
            hostname $hostname
        fi
    }
    
    config_dns() {
        if [[ -n $dns_nameserver ]]; then
            dns_conf=/etc/resolv.conf
            sed -i '/^nameserver.*/d' $dns_conf
            for i in $dns_nameserver; do
                echo "nameserver $i" >> $dns_conf
            done
        fi
    }
    
    is_classic_network() {
        # vpc: eth0
        # classic: eth0 eth1
        grep -q 'eth1' $os_conf_file
    }
    
    config_network() {
        /etc/init.d/network stop
        config_interface eth0 ${eth0_ip_addr} ${eth0_netmask} ${eth0_mac_addr}
        config_route eth0 "${eth0_route}"
        if is_classic_network ; then
            config_interface eth1 ${eth1_ip_addr} ${eth1_netmask} ${eth1_mac_addr}
            config_route eth1 "${eth1_route}"
        fi
        /etc/init.d/network start
    }
    
    config_interface() {
        local interface=$1
        local ip=$2
        local netmask=$3
        local mac=$4
        interface_cfg="/etc/sysconfig/network-scripts/ifcfg-${interface}"
        cat << EOF > $interface_cfg
    DEVICE=$interface
    IPADDR=$ip
    NETMASK=$netmask
    HWADDR=$mac
    ONBOOT=yes
    BOOTPROTO=static
    EOF
    }
    
    config_default_gateway() {
        local gateway=$1
        sed -i "s/^GATEWAY=.*/GATEWAY=$gateway/" /etc/sysconfig/network
    }
    
    config_route() {
        local interface=$1
        local route="$2"
        route_conf=/etc/sysconfig/network-scripts/route-${interface}
        > $route_conf
        echo $route | sed 's/;/\n/' | \
            while read line; do
                dst=$(echo $line | awk '{print $1}')
                gw=$(echo $line | awk '{print $2}')
                if ! grep -q "$dst" $route_conf 2> /dev/null; then
                    echo "$dst via $gw dev $interface" >> $route_conf
                fi
                if [[ "$dst" == "0.0.0.0/0" ]]; then
                    config_default_gateway $gw
                fi
            done
    }
    
    ################## sysvinit service portal ####################
    
    start() {
        if load_os_conf ; then
            config_password
            config_network
            config_hostname
            config_dns
            cleanup
            return 0
        else
            echo "not load $os_conf_file"
            return 0
        fi
    }
    
    RETVAL=0
    
    case "$1" in
        start)
            start
            RETVAL=$?
        ;;
        *)
            echo "Usage: $0 {start}"
            RETVAL=3
        ;;
    esac
    
    exit $RETVAL
  4. Enable the script to run on system startup.

    The following commands are for Ubuntu. Adjust for your operating system.

    sudo systemctl daemon-reload
    sudo systemctl enable customized-config.service
    Note

    Custom images created from a Customized Linux instance inherit this startup script. Alibaba Cloud applies os.conf settings only on first boot.

References

After configuration, select Customized Linux as the OS Version when you import the custom image.