linux初始化脚本1.1
admin
2023-03-02 01:21:52
0

version:1.1
des:主要针对redhat系列操作系统虚拟机的初始化配置
更新以下内容:

  • 添加网络检测
  • 添加网卡名称判断
  • 添加主机变量配置
  • 精简yum和.bashrc配置文件生成,通过'EOF'在配置文件中直接引用变量,去掉了变量$前的\
  • 修复yum配置时移动命令报错不能移动bak目录本身
  • 修复未配置网关错误

#!/bin/bash
#====================================================
# Author: Mr.Song
# Create Date: 2019-10-27
# Description:
#====================================================
########################################################
set -x 
echo  'nameserver 119.29.29.29' >> /etc/resolv.conf 
ping -c 2   www.baidu.com  2>&1  >/dev/null  || 'echo -e '\033[31mNetwork test fail,please check network configuration \033[0m' && exit 1 '
########################################################

##start intial script
########################################################
NET_INETERFACE_NAME=`ip a|grep -v lo|egrep  ^[0-9] |cut -f 2 -d ':' |sed 's#[[:space:]]##'`
HOST_IP='192.168.10.11'
HOST_NETMASK='24'
HOST_GATEWAY='192.168.10.2'
HOST_DNS='119.29.29.29'
HOST_NAME='test'
########################################################

hostnamectl  set-hostname $HOST_NAME

########################################################
#disable firewalld and selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing$/disabled/g'  /etc/selinux/config
########################################################

########################################################
#yum config
mkdir -p /etc/yum.repos.d/bak
mv   /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
cat > /etc/yum.repos.d/CentOS-163.repo  <<- 'EOF'
#CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - 163.com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - 163.com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - 163.com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - 163.com
baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
EOF

#install common software
yum install -y vim nano wget gcc chrony lrzsz bash-completion net-tools psmisc
#yum install epel-release
#yum install -y atop htop iftop
########################################################

########################################################
#bash配置
cat >> ~/.bashrc  <<- 'EOF'
alias cls='clear'   #DOS风格的清空
alias h='history | tail'
alias hg='history | grep'
alias hl='history | less'
#stty erase ^H        #清除退格 (这个很有必要)
export PS1="[\[\e[0;36m\]\u\[\e[m\]@\[\e[0;32m\]\h \[\e[0;35m\]\W\[\e[m\]]\\$"
#export PS1="[\[\e[0;36m\]\u\[\e[m\]@\[\e[0;32m\]\h \[\e[0;35m\]\W\[\e[m\]]\\\\$"
EOF
#########################################################
#vim配置:行号、快捷键输入文本、中文支持
cat >> ~/.vimrc <<-EOF
set autoindent
set nu
set paste
syntax on
set tabstop=4
set shiftwidth=4
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
function AddTitle()
call setline(1,"#!/bin/bash")
call append(1,"#====================================================")
call append(2,"# Author: Mr.Song")
call append(3,"# Create Date: " . strftime("%Y-%m-%d"))
call append(4,"# Description: ")
call append(5,"#====================================================")
endf
map  :call AddTitle()
EOF
#########################################################
#openssh优化:禁用DNS查询
sed -i  -e '/#UseDNS/a\UseDNS no' /etc/ssh/sshd_config
systemctl restart sshd
#########################################################

#########################################################
#ntp config
sed -i 's#0.centos.pool.ntp.org#s2b.time.edu.cn#;s#1.centos.pool.ntp.org#ntp1.aliyun.com#;s#server 2.#\#server 2.#;s#server 3.#\#server 3.#'  /etc/chrony.conf
systemctl restart chronyd
#########################################################

#########################################################
#network config
sed -i  "s/ONBOOT=no/ONBOOT=yes/;s/BOOTPROTO=dhcp/BOOTPROTO=static/;/ONBOOT/a\IPADDR=$HOST_IP\nPREFIX=$HOST_NETMASK\nGATEWAY=$HOST_GATEWAY\nDNS1=$HOST_DNS"  /etc/sysconfig/network-scripts/ifcfg-$NET_INETERFACE_NAME
systemctl restart network
#########################################################

相关内容

热门资讯

邮轮暴发汉坦病毒疫情,世卫确认... 据凤凰卫视报道,大西洋邮轮“洪迪厄斯”号暴发汉坦病毒疫情,世界卫生组识5月7日证实,船上共有5宗确诊...
内蒙古赤峰两村半个多世纪的土地... 澎湃新闻记者 王选辉争议土地大北叉沟。本文图均为 受访者 提供备受社会关注的“赤峰两村半个多世纪的土...
冥王星5月8日开启逆行 持续约... 被“逐出”太阳系大行星序列的冥王星5月8日将结束顺行,开启为期约5个月的逆行。这也是2026年冥王星...
夸大AI功能被起诉 苹果2.5... 当地时间5月5日,因夸大AI系统“苹果智能”(Apple Intelligence)的能力,误导数百...
学习机直播乱象调查:主播打北大... 极目新闻记者 刘闪 何佳仪打开短视频平台,各类学习机测评直播间随处可见。一些自诩自己是“老师”的主播...
即日起,郑州“错峰加油”有优惠... 进入5月,又到了郑州臭氧污染频发的季节,记者从郑州市生态环境局移动源污染监管处了解到,为减少臭氧污染...
汗水写热爱 篮球致青春 成人组1VS1比赛。河南日报全媒体记者 黄晖 摄“非常高兴能够参加这种全民赛事,也希望以后能够有更多...
荷兰六六民主党全国办公室遭爆炸... 荷兰警方7日说,荷兰执政党之一的六六民主党位于海牙的全国办公室当晚遭爆炸物袭击,造成建筑受损,无人受...
樱花树油烟机总是开不了怎么回事 电源问题。先检查电源是否未插上或是否电,若没有,将它重新插好,等待来电后测试,检查开不了机的问题是否...
燃气灶打火后一会就灭是什么原因 原因可能是电池没有电了,所以一开始能够打着火,但是燃烧了一会儿又熄灭了,这种情况下可以尝试更换一节新...