CentOS7实验模板机搭建

CentOS7 实验模板机搭建部署

在学习实验过程中,很多情况下需要我们能迅速的克隆部署CentOS7的基础系统用来运行其他的应用。针对这一常态化的功能,本博客详细记录了自己搭建CentOS7的模板机详细过程,方便日常的工作学习实验。

一、准备工作

主机: Windows 7 + Virtualbox 5.2.30
ISO镜像: CentOS-7-x86_64-Minimal-1810.iso

CentOS-7-x86_64-Minimal-1810.iso 该镜像可以从centos官网下载,当然最快速的方法就是去国内的镜像网站上下载。
附: 阿里云 - CentOS-7-x86_64-Minimal-1810.iso [点击下载]

二、开始部署

新建虚拟机
分配内存
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述这里处理器的数量给推荐的最大个数,有利于加快模板机的安装
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
选择安装的CentOS7的ISO镜像

选择完CentOS7的minimal镜像
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述这里的语言选择英语
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述自己配置分区在这里插入图片描述
在这里插入图片描述添加/boot分区
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述取消勾选KDUMP

在这里插入图片描述
在这里插入图片描述忽略对IPv6的设置
在这里插入图片描述

CentOS7minimal镜像安装只需要设置红线框区域点击按钮"Begin Installation" ,开始安装CentOS7minimal系统。

在这里插入图片描述安装完重启系统安装完成

三、简单优化

  1. 关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/vmlinuz.*/& selinux=0/g' /boot/grub2/grub.cfg
setenforce 0
  1. 关闭防火墙
systemctl disable firewalld.service
systemctl disable iptables-services
  1. 优化sshd服务
# 避免ssh连接需要很久的问题
echo 'UseDNS no'>>/etc/ssh/sshd_config
echo 'PermitRootLogin yes'>>/etc/ssh/sshd_config
  1. 优化网卡配置文件
# 实际的网卡名称和ip地址根据自己的主机和ip设置
cat >/etc/sysconfig/network-scripts/ifcfg-enp0s3<<EOF
TYPE=Ethernet
BOOTPROTO=none
NAME=enp0s3
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.2.3
PREFIX=24
GATEWAY=192.168.2.1
DNS1=223.5.5.5
DNS=8.8.8.8
EOF
  1. 配置主机名和/etc/hosts文件
HOSTNAME=centos7
hostnamectl set-hostname "$HOSTNAME"
# 临时配置主机名
echo "$HOSTNAME">/etc/hostname
# 永久配置主机名
echo "$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "$(ip a|grep "inet "|grep -v 127|awk -F'[ /]' '{print $6}') $HOSTNAME">>/etc/hosts
  1. 配置网络yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 使用阿里云镜像站的网络yum源
yum clean all
yum makecache
yum -y install nc net-tools vim tree dstat ntpdate lrzsz
yum -y update
  1. 添加/etc/rc.local的开机执行脚本的权限
# CentOS7系统默认将/etc/rc.local的执行权限去掉了
# 写入到该文件中的一些想要在开机启动执行的脚本就无法正常执行
# 还原方法就是把权限改回去即可
# 该文件 /etc/rc.local 是 /etc/rc.d/rc.local 文件的软连接
# 因此要修改 /etc/rc.d/rc.local 的权限
chmod +x /etc/rc.d/rc.local
  1. 配置ntpdata时钟校正任务
ntpdate -u ntp1.aliyun.com
echo '/usr/sbin/ntpdate -u ntp1.aliyun.com'>>/etc/rc.local
crontab -l>/tmp/crontab.tmp
echo '#OS Time Sync'>>/tmp/crontab.tmp
echo '0 * * * * /usr/sbin/ntpdate -u ntp1.aliyun.com>>/root/ntpdate.log 2>&1;/sbin/hwclock -w'>>/tmp/crontab.tmp
cat /tmp/crontab.tmp |crontab
rm -rf /tmp/crontab.tmp
# 生产环境要考虑时钟服务器是否选择阿里云时钟服务器
  1. 开机启动项优化
systemctl disable dbus-org.freedesktop.NetworkManager.service
systemctl disable dbus-org.freedesktop.nm-dispatcher.service
systemctl disable NetworkManager-dispatcher.service
systemctl disable NetworkManager
systemctl disable postfix.service
# 关闭一些不需要的开机启动项
systemctl list-unit-files --type=service|grep 'enabled'
# 查看目前处于开机启动状态的系统服务
  1. 部署dstat脚本监控
#修改脚本,输出主机名
sed -i 's/plugin title/Hostname/g' /usr/share/dstat/dstat_helloworld.py
sed -i 's/counter/------------/g' /usr/share/dstat/dstat_helloworld.py
sed -i "s/'Hello world\!'/os.popen('hostname').readlines()[0].split()/g" \
/usr/share/dstat/dstat_helloworld.py
dstat --helloworld 1 3

#创建检测脚本并启动
mkdir -p /root/checkOS
cd /root/checkOS
cat >/root/checkOS/checkOS.sh<<EOF
#!/bin/bash
export DSTAT_TIMEFMT='%Y-%m-%d %H:%M:%S'
#kill dstat
for i in \$(ps -ef|grep '/usr/bin/dstat --helloworld'|grep -v grep|awk '{print \$2}');do kill -9 \$i;done
#start dstat
/usr/bin/dstat --helloworld -tlcp \\
--proc-count -y \\
--top-cpu-adv \\
--top-cputime-avg \\
--top-latency-avg \\
--top-bio-adv \\
--top-io-adv \\
--top-mem \\
--top-childwait \\
-mgsn --net-packets --tcp --udp \\
--fs --lock \\
-dr --aio --disk-tps --disk-util \\
--freespace  --noheaders 10 8640 >>/root/checkOS/checkOS.info_\$(date +%F_%H_%M_%S) &
/usr/bin/find /root/checkOS/checkOS.info_* -mtime +30 -exec rm -rf {} \;
EOF
chmod 700 /root/checkOS/checkOS.sh
#/root/checkOS/checkOS.sh &
echo '/root/checkOS/checkOS.sh &'>>/etc/rc.local

#设置自动任务每日启动监控脚本
crontab -l>/tmp/crontab.tmp
echo -e '\n#OS Check Dstat'>>/tmp/crontab.tmp
echo '0 0 * * * /bin/bash /root/checkOS/checkOS.sh'>>/tmp/crontab.tmp
cat /tmp/crontab.tmp |crontab
rm -rf /tmp/crontab.tmp

#生成脚本,输出昨日系统基本信息
cat >/root/checkOS/checkYesterday.sh<<EOF
head -1 /root/checkOS/checkOS.info_\$(date '+%F' -d '1 day ago')*|\\
awk 'BEGIN{OFS="|"}/system/{print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>/root/checkOS/result.txt
cat /root/checkOS/checkOS.info_\$(date '+%F' -d '1 day ago')*|grep -v '\----system----'|\\
awk 'BEGIN{FS="|";OFS="|"} !/system/ {print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>>/root/checkOS/result.txt
head -2 result.txt ;grep -v '|-' result.txt |sort -t'|' -nrk4|head -30
EOF
chmod +x checkYesterday.sh

#生成脚本,输出今日系统基本信息
cat >/root/checkOS/checkToday.sh<<EOF
head -1 /root/checkOS/checkOS.info_\$(date '+%F')*|\\
awk 'BEGIN{OFS="|"}/system/{print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>/root/checkOS/result.txt
cat /root/checkOS/checkOS.info_\$(date '+%F')*|grep -v '\----system----'|\\
awk 'BEGIN{FS="|";OFS="|"} !/system/ {print "|"\$1,\$2,\$3,\$4,\$5,\$6,\$15,\$16,\$17,\$18,\$24"|"}'>>/root/checkOS/result.txt
head -2 result.txt ;grep -v '|-' result.txt |sort -t'|' -nrk4|head -30
EOF
chmod +x checkToday.sh
  1. 重启
reboot

四、结尾工作

  1. 模板机关机,需要使用时,克隆该模板机,更改ip地址和hosts,重启网卡,即可使用。
  2. 下一步,如果感觉yum下载慢时,可以考虑使用多线程下载加速下载,后续等试验做完更新。
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值