您的位置:首页 > 运维架构 > Linux

Linux:对新机器进行初始化设置的脚本

2020-08-26 13:59 1341 查看

初始化

学习Linux过程中,经常要新建虚拟机,新建虚拟机后,又要重新配置yum源等。最好的解决办法当然是克隆已经配置好的虚拟机。以下是一些基础的配置,放到虚拟机直接执行即。

#!/bin/bash
echo "本次配置适用于Redhat7 CentOS7的物理机。公有云不可使用!其他版本系统慎用!"
#将IP地址修改为静态
read -p "请输入你的IP(最后一位即可,默认192.168.186.xxx): " ip
read -p "请输入你的主机名:" host_name

sed -i 's/\<dhcp\>/none/' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "$ a IPADDR=192.168.186.${ip}\nNETMASK=255.255.255.0\nDNS=223.6.6.6\nGATEWAY=192.168.186.2" /etc/sysconfig/network-scripts/ifcfg-ens33
ifdown ens33;ifup ens33
#添加DNS解析
sed -i '/^nameserver/d' /etc/resolv.conf
echo "nameserver 223.6.6.6" >> /etc/resolv.conf

#配置yum源

yum -y install wget > /dev/null
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache

#配置epel源
find /etc/yum.repos.d/ -name "epel*"|xargs -i mv {} {}.bak
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
yum clean all
yum makecache

#永久修改主机名
echo "${host_name}" >/etc/hostname

#永久关闭防火墙 SeLinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0 >> /dev/null
sed -i.bak 's/enforcing$/disabled/' /etc/selinux/config

#ntp对时
yum -y install ntp-4.2.6p5-29.el7.centos.2.x86_64
ntpdate ntp.aliyun.com

#是否重启
read -p "初始化完成,部分配置需重启生效。现在重启?[y]" reply
[ $reply == "y" ] && reboot || echo "请您稍后手动重启。【reboot】"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: