文档

云市场镜像规范(Linux)

更新时间:
一键部署

镜像服务商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特性。

  • 安装云助手

    您在制作云市场镜像时需要安装云助手。云助手是专为云服务器ECS打造的原生自动化运维工具,免密码、免登录、无需使用跳板机,即可批量执行命令(Shell、PowerShell、Bat等),实现自动化运维脚本、轮询进程、安装卸载软件、启动或停止服务、安装补丁或安装安全更新等任务。具体操作,请参见安装云助手Agent

  • 对服务器做安全防护

    在制作云市场镜像过程中,推荐您使用阿里云的云安全中心对服务器做安全防护。什么是云安全中心可以为您提供告警通知、病毒查杀、网站后门查杀、客户端自保护、镜像安全扫描等安全能力,全方位保护您的云上资产和本地服务器安全。

系统配置

  • 配置时钟

    镜像硬件时钟时区包括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

发布镜像前,各操作系统发布商可以根据自己本身情况进行清理历史记录以及日志等操作。

  • 本页导读 (0)
文档反馈