定制Centos7.9镜像


Ps:因为工作内容;有一部份是需要重装系统;系统版本镜像为centos7.9。可每次装完都需要下载一些基础包;最近因为设备过多;网卡名称太乱;导致做后续配置太繁琐;不规整;索性自己定制个系统;

 

搭建基础环境 将/root/iso做为定制镜像制作目录; 将/MISO作为挂载镜像的目录 ;安装制作镜像的命令及下载最小化镜像
# mkdir /root/iso
# mkdir /MISO
# yum install createrepo mkisofs isomd5sum squashfs-tools 
# wget https://mirrors.163.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso
  挂载镜像;并将/MISO目录下的所有内容copy到镜像制作目录;

# mount -o loop,rw CentOS-7-x86_64-Minimal-2009.iso /MISO
# cp -a /MISO/*   /root/iso/
  修改/root/iso/isolinux/isolinux.cfg文件,修改如下内
default vesamenu.timeout 100  
##安装系统时的默认引导时间; display boot.msg # Clear the screen when exiting the menu, instead of leaving the menu displayed. # For vesamenu, this means the graphical background is still displayed without # the menu itself for as long as the screen remains in graphics mode. menu clear menu background splash.png ###安装系统背景图片;更换背景界面;替换为需要的图片,保持文件名不变,图片大小640x480 menu title SinoCache ISO 7.9 ##可以定制欢迎标题 menu vshift 8 menu rows 18 menu margin 8 #menu hidden menu helpmsgrow 15 menu tabmsgrow 13 ……………………………………………… label linux menu label ^Install SinoCache ISO 7.9 ###定制系统标题 menu default ###默认引导选项 kernel vmlinuz append initrd=initrd.img ks=cdrom:/isolinux/ks.cfg inst.stage2=hd:LABEL=CentOS quiet net.ifnames=0 biosdevname=0 ##ks=cdrom:/isolinux/ks.cfg :创建自动化安装配置文件;可以用系统默认生成的anaconda-ks.cfg文件。 ##inst.stage2=hd:LABEL= :为安装介质位置,hd:LABEL为介质标签,例如:CentOS

##net.ifnames=0 biosdevname=0 :用于禁用centos7的”一致性网络设备命名法”;centos6系统网卡命名为ethX形式;centos7系统网卡名各种各样在工作中太无序了;很烦;使用这个参数可以将网卡名改为ethX ###可以将这个标签选项注销。 #label check # menu label Test this ^media & install SinoCache ISO 7.9 # kernel vmlinuz # append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet net.ifnames=0 biosdevname=0
  编辑isolinux/ks.cfg文件;也可以用/root/anaconda-ks.cfg # cp -a /root/anaconda-ks.cfg /root/iso/isolinux/ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
#使用CD-ROM安装介质
cdrom
# 使用图形化安装
graphical
# 在第一次启动时运行安装代理
firstboot --enable
ignoredisk --only-use=sda
# 键盘布局
keyboard --vckeymap=us --xlayouts='us'
# 设置语言
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=eth1 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=eth2 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=eth3 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=eth4 --onboot=off --ipv6=auto
network  --bootproto=dhcp --device=eth5 --onboot=off --ipv6=auto
#network  --device=Wan     --bondopts=mode=802.3ad,xmit_hash_policy=layer3+4 --bondslaves=eth0,eth3 --onboot=yes
network  --hostname=ngaa.com.cn

# 设置root密码
rootpw --iscrypted $6$sNStQWknxzURmZpv$PEFIX7tG1McnzRsGBUm4UnOyRr4Kg/R8CHcujTbPYMvZRfxVWd8aetfP7gWRHhHobKMay4rGQO6Uxq2CoZ7QF1
# System services
services --enabled="chronyd"
# 关闭 SELinux 
selinux --disabled
# 关闭防火墙
firewall --disabled
# 系统服务开机自启
services --enabled="zabbix_agentd,sshd"
# 设置时区
timezone Asia/Shanghai --isUtc
# 系统引导配置
bootloader --append="console=tty0 audit=0 selinux=0 net.ifnames=0 biosdevname=0 crashkernel=auto" --location=mbr --boot-drive=sda
# 清空主引导分区记录MBR
zerombr
# 清除分区信息
clearpart --all --initlabel

# 硬盘分区信息
part / --fstype="xfs" --ondisk=sda --size=102400
part /boot --fstype="xfs" --ondisk=sda --size=300
part swap --fstype="swap" --ondisk=sda --size=32768
part /data/cache1 --fstype="xfs" --ondisk=sda --size=92458

%packages
@^minimal
@core
chrony
kexec-tools
%end

%addon com_redhat_kdump --enable --reserve-mb='auto'
%end

%post
#配置sshd
/bin/sed -i '13a Port 7346' /etc/ssh/sshd_config; \\
/bin/sed -i '/PermitRootLogin /d' /etc/ssh/sshd_config; \\
/bin/sed -i '32a PermitRootLogin yes' /etc/ssh/sshd_config; \\
/bin/sed -i '59a PasswordAuthentication yes' /etc/ssh/sshd_config; \\
/bin/sed -i '/GSSAPIAuthentication /d' /etc/ssh/sshd_config; \\
/bin/sed -i '71a GSSAPIAuthentication no' /etc/ssh/sshd_config; \\
/bin/sed -i '/UsePAM /d' /etc/ssh/sshd_config; \\
/bin/sed -i '82a UsePAM yes' /etc/ssh/sshd_config; \\
/bin/sed -i '/UseDNS /d' /etc/ssh/sshd_config; \\
/bin/sed -i '98a UseDNS no' /etc/ssh/sshd_config; \\

#disable NetworkManager
systemctl stop NetworkManager
systemctl disable NetworkManager

#配置PAM会话检查
cat > /etc/pam.d/sshd << EOF
#%PAM-1.0
auth       required     pam_sepermit.so
auth       include      password-auth
account    required     pam_nologin.so
account    include      password-auth
password   include      password-auth
# pam_selinux.so close should be the first session rule
session    required     pam_selinux.so close
session    required     pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session    required     pam_selinux.so open env_params
session    optional     pam_keyinit.so force revoke
session    include      password-auth
EOF

#配置DNS
echo  'nameserver 114.114.114.114' > /etc/resolv.conf
%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

reboot
##重装完系统自动重启。
%pre 和 %post 部分 - 这两个部分的顺序不限,也不是必须的。

 %addon、%packages、%pre 和 %post 部分必须以 %end 结尾,否则安装程序会拒绝 Kickstart 文 件。

更改/etc/yum.conf;使用下载缓存;

[main]
cachedir=/var/cache/yum/   ##将缓存目录做修改;原来目录太深了;·
keepcache=1  ##将原来的值0改为1(表示安装后保留软件包;为的是下载一些新系统预安装的软件;如wget等)
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
安装一些预安装的软件;或者在修改yum缓存后;直接yum -y  update ;升级所有的系统包;在安装系统是均是最新的包;ssh有个坑;是/etc/pam.d/sshd文件认证登录;

# yum -y install mtr vim net-tools wget iptables-services tcpdump traceroute zabbix_agentd iptraf-ng bind-utils ipmitool pciutils rsync
###上面的软件rpm包(包括依赖包)都会在/var/cache/yum/*/packages/目录下; *代表所使用的yum源仓库名称;根据自己使用yum仓库来决定。
  需要预安装其他的包,将解决好依赖关系的包全部放入/root/iso/Packages/中,此目录下是系统最初安装的系统包。

[root@localhost packages]# pwd
/var/cache/yum/ali-updates/packages
[root@localhost packages]# ll | head -n5
total 359436
-rw-r--r--. 1 root root  1038100 Dec  1 09:13 bash-4.2.46-35.el7_9.x86_64.rpm
-rw-r--r--. 1 root root  1146744 Dec  1 09:13 bind-export-libs-9.11.4-26.P2.el7_9.8.x86_64.rpm
-rw-r--r--. 1 root root   161096 Dec  1 09:13 bind-libs-9.11.4-26.P2.el7_9.8.x86_64.rpm
-rw-r--r--. 1 root root  1179928 Dec  1 09:13 bind-libs-lite-9.11.4-26.P2.el7_9.8.x86_64.rpm
[root@localhost packages]# cp  -a ./* /root/iso/Package
  更新安装软件包xml文件;将所下载的软件包名称填写进去;在装系统的时候会自动安装好。

# cd /root/iso/repodata
# vim *-minimal-x86_64-comps.xml
<?xml version="1.0" encoding="UTF-8"?>


  
    core
    Core
    Kern
      …………………………
   核心
   Okuyikhona
   Smallest possible installation.
   ??????? ???????
      …………………………
   最小型安裝。
   false
   false
   
      audit
      basesystem
      bash
        ………………………………
      yum
      aic94xx-firmware
      alsa-firmware
        ………………………………      
      ql23xx-firmware
      rdma
      vim-enhanced
      net-tools
      tcpdump
      mtr
      iptabels-services
      traceroute
      ipmitool
      wget
      zabbix_agentd
      iptraf-ng
      ansible
      rsync
      pciutils
kernel-devel
kernel-headers
tboot



minimal
Minimal Install
??????? ??????
…………………………
基本功能。
基本功能。
5

core
core



###在default标签下写自己要下载的软件。前提是制作目录/root/iso/Package/下有相应的rpm及依赖包。

# cd /root/iso/
# createrepo -g repodata/*-c7-minimal-x86_64-comps.xml  .
###注意当有新包加入,或者更新*comps.xml文件,均需要重新生成repodata文件夹

 

修改安装界面图标背景 /media/usr/share/anaconda/pixmaps/sidebar-logo.png为该安装界面的图标,只需根据自己的需要替换即可,分辨率要跟原图保持基本一致,要不会出现图标过大的情况;图片大小131×115

unsquashfs /root/iso/LiveOS/squashfs.img    
#会在root家用户目录下;出现一个squashfs-root/
mount -o loop,rw squashfs-root/LiveOS/rootfs.img /media
#把解压后的文件进行挂载,然后操作

#将解压后的文件重新打包
#mksquashfs squashfs-root/   squashfs.img
并将生成的squashfs.img替换原来的/root/iso/LiveOS/squashfs.img  

 

制作ISO
# mkisofs -o /root/cnetos-7.9.iso -input-charset utf-8 -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -T -joliet-long -V CentOS /root/iso/ 
## 注意参数中的-V,和上面的isolinux.cfg文件有关。 

  

感谢这几位的博客及官网文档;

Kickstart官网文档 :https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/installation_guide/chap-installing-using-anaconda-x86

博客
https://www.cnblogs.com/yiy520/p/13307030.html

https://ieevee.com/tech/2016/08/20/kickstart.html

https://www.wumingx.com/linux/centos-iso.html

https://www.cnblogs.com/panyouming/p/8401038.html

https://www.jianshu.com/p/0edb49c3e9b4

https://blog.51cto.com/boytnt/2147184

https://www.cnblogs.com/yanh0606/p/10910808.html

 

 

 

  


 

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
Bing Webmaster Portal Back