目录

有关vSphere模板的使用

1 废话

用了这么久的vsphere 从来没用过模板,感觉很麻烦,windows还要用sysprep重新封装,Linux就不要提了,转换完模板,从模板部署;自己用还好,给别人用每次都要修改密码,不清空udev条目,网卡不能启动,想想都尴尬。 今天高人指点,说了说他做linux模板的步骤, 受益匪浅,总结分享。

发现都是废话, 不想看直接跳到最后看干货

windows就不说了,sysprep重新封装就好,点点点,(推荐使用自定义规范,SID才会出问题) 说下linux的模板(仅使用rhel衍生版,6之前的可以使用,7已废弃)

  • 首先拿来一个iso
  • 然后安装一个系统,(最小化安装,没用过图形,见笑)
  • 密码,配置,分区,balabala.
  • 完事了。
  • 登录 cp一份ifcfg
1
cp /etc/sysconfig/network-scripts/ifcfg-eth0 ~/

先不用配ip 没用

  • 注释两条。mac和uuid
1
 sed  -i 's/^\(HWADDR\|UUID\)/#&/' /etc/sysconfig/network-scripts/ifcfg-xxx   (XXX为需要修改的网卡)
  • 清空udev规则

Centos6 大概路径 自行tab /etc/udev/rules.d/70-persistent-net.rules

1
2
3
4
5
如果你喜欢vim打开然后dd 也可以,
不打开的话就sed

------------------------------------------------------------------------------------------------------------------
sed -i '/^SUBSYSTEM/d' /etc/udev/rules.d/70-persistent-net.rules (Centos6 )

或者你简单粗暴点

1
2
3
cat /dev/null > 你要清空的那个udev文件
或者
echo "" >  你要清空的那个udev文件

直接清空文件, 注释也没了。。

2 安装tools

挂载tools mount

1
mount /dev/sr0 /mnt

解压&&安装

1
2
3
tar -zxvf /mnt/VMwareTools-10.1.6-5214329.tar.gz -C /tmp/
/tmp/vmware-tools-distrib/vmware-install.pl -d
umount

3 重点来了。

懒得改密码,

1
2
3
4
5
6
7
8
yum -y install setuptool ntsysv system-config-securitylevel-tui system-config-network-tui authconfig-gtk system-config-keyboard

touch /.unconfigured

rm -rf /etc/ssh/ssh_host_*


init 0

建议封装前,清空下历史命令记录和日志

1
2
3
4
echo > /var/log/wtmp
echo > /var/log/btmp
history -c
echo > ~/.bash_history

好了,可以直接转换模板了, 演示效果, 我们开机看下有什么不同

https://blog-image.nos-eastchina1.126.net/CALB2AEBDc.png?imageslim https://blog-image.nos-eastchina1.126.net/d90fHfa281.png?imageslim https://blog-image.nos-eastchina1.126.net/Cf24lEeL1g.png?imageslim 好了,

4 补充

技巧
上述处理方式目前看非常残废,其实只需要把新安装的系统清理一下即可,下面我列出常见的清理方式。

4 RHEL系

Cleaning all audit logs

1
2
3
4
5
6
7
8
9
if [ -f /var/log/audit/audit.log ]; then
 cat /dev/null > /var/log/audit/audit.log
fi
if [ -f /var/log/wtmp ]; then
 cat /dev/null > /var/log/wtmp
fi
if [ -f /var/log/lastlog ]; then
 cat /dev/null > /var/log/lastlog
fi

Cleaning persistent udev rules

1
2
3
if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then
  rm /etc/udev/rules.d/70-persistent-net.rules
fi

Cleaning the /tmp directories

1
2
3
rm -rf /tmp/*
rm -rf /var/tmp/*
rm -rf /var/cache/dnf/*

Cleaning the Red Hat Subscription Manager logs

1
rm -rf /var/log/rhsm/*

Cleaning the SSH host keys

1
rm -f /etc/ssh/ssh_host_*

Cleaning the machine-id

1
2
3
4
5
6
# redhat-release <= 8
truncate -s 0 /etc/machine-id
rm /var/lib/dbus/machine-id
ln -s /etc/machine-id /var/lib/dbus/machine-id
# redhat-release >= 9 
truncate -s 0 /etc/machine-id

Cleaning the shell history

1
2
3
4
unset HISTFILE
history -cw
echo > ~/.bash_history
rm -fr /root/.bash_history

Running a sync

1
sync && sync

Ubuntu

Cleaning all audit logs

1
2
3
4
5
6
7
8
9
if [ -f /var/log/audit/audit.log ]; then
cat /dev/null > /var/log/audit/audit.log
fi
if [ -f /var/log/wtmp ]; then
cat /dev/null > /var/log/wtmp
fi
if [ -f /var/log/lastlog ]; then
cat /dev/null > /var/log/lastlog
fi

Cleaning persistent udev rules

1
2
3
if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then
rm /etc/udev/rules.d/70-persistent-net.rules
fi

Cleaning the /tmp directories

1
2
rm -rf /tmp/*
rm -rf /var/tmp/*

Cleaning the SSH host keys

1
rm -f /etc/ssh/ssh_host_*

Cleaning the machine-id

1
2
3
truncate -s 0 /etc/machine-id
rm /var/lib/dbus/machine-id
ln -s /etc/machine-id /var/lib/dbus/machine-id

Cleaning the shell history

1
2
3
4
unset HISTFILE
history -cw
echo > ~/.bash_history
rm -fr /root/.bash_history