全部產品
Search
文件中心

Elastic Compute Service:雲市場鏡像規範(Linux)

更新時間:Jun 19, 2024

鏡像服務商ISV(Independent Software Vendor)在雲市場發布鏡像前,需要確保已製作的鏡像滿足與ECS執行個體規格適配的規範要求,並通過與ECS執行個體規格的適配測試。本文為您介紹在製作Linux系統的雲市場鏡像過程中需遵循的規範要求。

磁碟分區

您在製作雲市場鏡像過程中對磁碟分割時,需滿足如下要求。

必備的軟體和工具

  • 安裝virtio驅動

    發布鏡像前,需確保鏡像已安裝virtio驅動且已將virtio驅動添加到臨時檔案系統,virtio驅動可以確保鏡像能夠在阿里雲執行個體上正常使用。具體操作,請參見安裝virtio驅動

  • 安裝NVMe驅動

    部分執行個體規格類型系列支援基於NVMe協議掛載雲端硬碟(例如ecs.g7se、ecs.c7se、ecs.r7se等),如果您需要在該類執行個體規格類型系列上使用鏡像啟動ECS執行個體,且需要保證執行個體穩定運行,則需要在對應的作業系統中進行NVMe相關的配置操作。具體操作,請參見Linux自訂鏡像如何適配NVMe系統硬碟?

  • 安裝cloud-init

    您在製作雲市場鏡像時需要安裝cloud-init,以保證運行該鏡像的ECS執行個體能成功完成初始化配置。具體操作,請參見安裝cloud-init

    說明

    使用cloud-init修改密碼要求作業系統的核心支援CONFIG_FW_CFG_SYSFS特性。Linux社區核心4.6版本之後預設支援該特性,CentOS的核心在3.10.0-826.el7版本之後預設支援該特性。您可以在該鏡像對應的伺服器內運行grep -nr CONFIG_FW_CFG_SYSFS /boot/config-$(uname -r)命令,如果回顯結果中包含CONFIG_FW_CFG_SYSFS=y資訊,則說明該鏡像中的核心已支援CONFIG_FW_CFG_SYSFS特性。

  • 安裝雲助手

    您在製作雲市場鏡像時需要安裝雲助手。雲助手是專為Elastic Compute Service打造的原生自動化營運工具,免密碼、免登入、無需使用跳板機,即可批量執行命令(Shell、PowerShell、Bat等),實現自動化營運指令碼、輪詢進程、安裝卸載軟體、啟動或停止服務、安裝補丁或安裝安全更新等任務。具體操作,請參見安裝雲助手Agent

  • 對伺服器做安全防護

    在製作雲市場鏡像過程中,推薦您使用阿里雲的Security Center對伺服器做安全防護。Security Center可以為您提供警示通知、病毒查殺、網站後門查殺、用戶端自保護、鏡像安全掃描等安全能力,全方位保護您的雲上資產和本機伺服器安全。

系統配置

  • 配置時鐘

    鏡像硬體時鐘時區包括UTC和LocalTime,製作雲市場鏡像時需要使用標準的UTC模式。樣本命令如下:

    cat > $dst << EOF
    0.0 0 0.0
    0
    UTC
    EOF
    
    timedatectl set-local-rtc 0

    更多資訊,請參見Linux時間和時區說明

  • 配置SSHD服務

    在製作雲市場鏡像過程中,需要配置SSHD服務。

    SSHD服務是Linux系統中的一個服務,用於遠程登入和管理Linux系統。SSHD服務通常使用SSH協議(Secure Shell)進行加密通訊,以保證遠程登入的安全性。修改/etc/ssh/sshd_config中的配置樣本如下:

    UseDNS no
    AddressFamily inet 
    SyslogFacility AUTHPRIV 
    PermitRootLogin yes 
    PasswordAuthentication yes 
  • 配置Chrony/NTP服務

    在製作雲市場鏡像過程中,建議您配置Chrony服務或者NTP服務,配置其中一個服務即可。

    Chrony和NTP都是用於同步電腦時鐘的網路通訊協定,可以確保時鐘同步,以避免時間誤差導致的問題。樣本命令如下:

    Chrony服務

    chrony_file=/etc/chrony.conf
    cat >$chrony_file <<EOF
    # Use Alibaba NTP server
    # Public NTP
    # Alicloud NTP
    
    
    server ntp.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp.aliyun.com minpoll 4 maxpoll 10 iburst
    server ntp1.aliyun.com minpoll 4 maxpoll 10 iburst
    server ntp1.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp10.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp11.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp12.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp2.aliyun.com minpoll 4 maxpoll 10 iburst
    server ntp2.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp3.aliyun.com minpoll 4 maxpoll 10 iburst
    server ntp3.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp4.aliyun.com minpoll 4 maxpoll 10 iburst
    server ntp4.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp5.aliyun.com minpoll 4 maxpoll 10 iburst
    server ntp5.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp6.aliyun.com minpoll 4 maxpoll 10 iburst
    server ntp6.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp7.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp8.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    server ntp9.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
    
    # Ignore stratum in source selection.
    stratumweight 0.05
    
    # Record the rate at which the system clock gains/losses time.
    driftfile /var/lib/chrony/drift
    
    # Enable kernel RTC synchronization.
    rtcsync
    
    # In first three updates step the system clock instead of slew
    # if the adjustment is larger than 10 seconds.
    makestep 10 3
    
    # Allow NTP client access from local network.
    #allow 192.168/16
    
    # Listen for commands only on localhost.
    bindcmdaddress 127.0.0.1
    bindcmdaddress ::1
    
    # Disable logging of client accesses.
    noclientlog
    
    # Send a message to syslog if a clock adjustment is larger than 0.5 seconds.
    logchange 0.5
    
    logdir /var/log/chrony
    #log measurements statistics tracking
    EOF

    NTP服務

    ntp_conf=/etc/ntp.conf
        cat > $ntp_conf << EOF
    driftfile  /var/lib/ntp/drift
    pidfile    /var/run/ntpd.pid
    logfile    /var/log/ntp.log
    
    
    # Access Control Support
    restrict    default kod nomodify notrap nopeer noquery
    restrict -6 default kod nomodify notrap nopeer noquery
    restrict 127.0.0.1
    restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap nopeer noquery
    restrict 172.16.0.0 mask 255.240.0.0 nomodify notrap nopeer noquery
    restrict 100.64.0.0 mask 255.192.0.0 nomodify notrap nopeer noquery
    restrict 10.0.0.0 mask 255.0.0.0 nomodify notrap nopeer noquery
    
    
    # local clock
    server 127.127.1.0
    fudge  127.127.1.0 stratum 10
    
    restrict ntp.aliyun.com nomodify notrap nopeer noquery
    restrict ntp.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp1.aliyun.com nomodify notrap nopeer noquery
    restrict ntp1.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp10.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp11.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp12.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp2.aliyun.com nomodify notrap nopeer noquery
    restrict ntp2.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp3.aliyun.com nomodify notrap nopeer noquery
    restrict ntp3.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp4.aliyun.com nomodify notrap nopeer noquery
    restrict ntp4.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp5.aliyun.com nomodify notrap nopeer noquery
    restrict ntp5.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp6.aliyun.com nomodify notrap nopeer noquery
    restrict ntp6.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp7.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp8.cloud.aliyuncs.com nomodify notrap nopeer noquery
    restrict ntp9.cloud.aliyuncs.com nomodify notrap nopeer noquery
    
    
    server ntp.aliyun.com iburst minpoll 4 maxpoll 10
    server ntp.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
    server ntp1.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp10.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp11.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp12.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
    server ntp2.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp3.aliyun.com iburst minpoll 4 maxpoll 10
    server ntp3.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp4.aliyun.com iburst minpoll 4 maxpoll 10
    server ntp4.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp5.aliyun.com iburst minpoll 4 maxpoll 10
    server ntp5.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp6.aliyun.com iburst minpoll 4 maxpoll 10
    server ntp6.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp7.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp8.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    server ntp9.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
    EOF
  • 配置MOTD服務

    在製作雲市場鏡像過程中,建議您配置MOTD(Message of the Day)服務。

    MOTD服務是Linux系統中的一項功能,它可以在使用者登入到系統後顯示一條歡迎資訊或者系統公告。樣本命令如下:

    echo -e "\nWelcome to Alibaba Cloud Elastic Compute Service ! \n" > /etc/motd
  • 關閉Firewalld服務

    在製作雲市場鏡像過程中,建議您關閉Firewalld服務。Firewalld是一種在Linux系統中管理防火牆規則的服務,雲上有安全性群組做安全防護,您可以關閉防火牆。樣本命令如下:

    systemctl stop firewalld
    systemctl disable firewalld
  • 配置PIP源

    如果系統存在PIP源配置,一般為~/.pip/pip.conf檔案,推薦使用阿里雲的PIP源地址,便於從阿里雲的源中擷取到最新的軟體包。

    [global]
    index-url=http://mirrors.cloud.aliyuncs.com/pypi/simple/
    
    [install]
    trusted-host=mirrors.cloud.aliyuncs.com
  • 配置網路

    如果您使用NetworkManager或者network網路服務,需配置/etc/sysconfig/network-scripts/ifcfg-eth0檔案內容如下:

    DEVICE=eth0
    BOOTPROTO=dhcp
    ONBOOT=yes

    其他網路服務配置,請參見配置輔助彈性網卡

核心參數配置

  • 配置kernelopt

    在製作雲市場鏡像過程中,您需要配置GRUB的kernelopt核心參數,以確保系統能夠正常啟動。以Debian系統為例,樣本命令如下:

    1. /etc/default/grub檔案GRUB_CMDLINE_LINUX=一行中,添加如下資訊。

      • x86_64架構

        net.ifnames=0 console=tty0 console=ttyS0,115200n8
      • ARM64架構

        net.ifnames=0 console=tty0 console=ttyAMA0,115200n8 
    2. 運行sudo update-grub2命令,使GRUB配置生效。

      說明

      不同作業系統GRUB設定檔路徑不同,因此GRUB配置生效的命令不同。更多資訊,請參見Linux自訂鏡像如何適配NVMe系統硬碟?中關於GRUB配置生效的操作命令。

  • 配置sysctl

    在製作雲市場鏡像過程中,建議您配置sysctl參數,便於調整和最佳化Linux核心的運行參數。

    1. 修改/etc/sysctl.conf內容如下。

      vm.swappiness = 0
      kernel.sysrq = 1
      
      net.ipv4.neigh.default.gc_stale_time = 120
      
      net.ipv4.conf.all.rp_filter = 0
      net.ipv4.conf.default.rp_filter = 0
      net.ipv4.conf.default.arp_announce = 2
      net.ipv4.conf.lo.arp_announce = 2
      net.ipv4.conf.all.arp_announce = 2
      
      net.ipv4.tcp_max_tw_buckets = 5000
      net.ipv4.tcp_syncookies = 1
      net.ipv4.tcp_max_syn_backlog = 1024
      net.ipv4.tcp_synack_retries = 2
      net.ipv4.tcp_slow_start_after_idle = 0
    2. 運行sudo sysctl -p命令使核心參數修改生效。

清理鏡像資訊

為了提高系統的安全性,鏡像發布前,建議您鎖定root賬戶,修改ssh PasswordAuthentication許可權(執行個體第一次啟動如果配置密碼則會開啟該許可權)。

  sed -i 's/^AddressFamily.*//' $dst
  sed -i 's/^SyslogFacility.*//' $dst
  sed -i 's/^PermitRootLogin.*//' $dst
  sed -i 's/^PasswordAuthentication.*//' $dst

  echo UseDNS no >> $dst
  echo AddressFamily inet >> $dst
  echo SyslogFacility AUTHPRIV >> $dst
  echo PermitRootLogin yes >> $dst
  echo PasswordAuthentication yes >> $dst

	passwd -l root
	sed -i -e 's/root:[^:]*:/root:*:/g' /etc/shadow

發布鏡像前,各作業系統發布商可以根據自己本身情況進行清理記錄以及日誌等操作。