主要针对以下几点进行系统初始化以及优化
1.修改主机名
2.配置网卡设备名为eth0
3.配置静态IP
4.优化Linux系统参数
5.设置阿里云yum源
6.时间同步设置
7.安装常用的运维软件、网络软件
8.创建运维账号
function COLOR_RED() {
echo -e "\033[1;31m$1\033[0m"
}
function COLOR_GREEN() {
echo -e "\033[1;32m$1\033[0m"
}
function COLOR_YELLOW() {
echo -e "\033[1;33m$1\033[0m"
}
function echo_check() {
echo -e "$1 [\033[32m √ \033[0m]"
}
function log_success() {
COLOR_GREEN "[SUCCESS] $1"
}
function log_error() {
COLOR_RED "[ERROR] $1"
}
function log_info() {
COLOR_YELLOW "[INFO] $1"
}
date_time=$(date +"%Y-%m-%d-%H:%M:%S")
yum_dir="/etc/yum.repos.d/"
aliyun_url="https://mirrors.aliyun.com/"
network_url="/etc/sysconfig/network-scripts/"
host_name () {
read -p "请修改你的主机名:" hostname
hostnamectl set-hostname "$hostname"
log_info "您的主机名为:"$hostname" "
}
set_ethc() {
sed -ri.bak-${
date_time} '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0 biosdevname=0"@' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/null
}
set_ipc() {
ETHNAME=`ip addr | sed -nr '/^2/s#^.* ([a-z]+[0-9]+).*#\1#p' | head -n1`
cp ${
network_url}"ifcfg-${ETHNAME}" /tmp/ifcfg-${
ETHNAME}.bak-${
date_time}
mv ${
network_url}"ifcfg-${ETHNAME}" ${
network_url}"ifcfg-eth0"
read -p "请输入你想配置的IP地址: " IPA
read -p "请配置此IP地址的网关: " GATE
read -p "请配置此服务器的DNS地址:" DNS
read -p "请配置此服务器的DEVICE名称:" DEVI
read -p "请配置此服务器的NAME名称:" NM
cat > /etc/sysconfig/network-scripts/ifcfg-${
NM} << EFO
DEVICE=${
DEVI}
NAME=${
NM}
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR="$IPA"
NETMASK=255.255.255.0
GATEWAY="$GATE"
DNS1=$DNS
EFO
log_success "IP地址修改成功,重新启动服务器后生效!"
}
system_optimization() {
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65535
EOF
echo ""
echo ""
mv /usr/lib/systemd/system/ctrl-alt-del.target /usr/lib/systemd/system/ctrl-alt-del.target.bak
echo ""
echo ""
\cp /etc/ssh/sshd_config /etc/ssh/sshd_config."${date_time}"
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%g' /etc/ssh/sshd_config
service sshd restart
echo ""
echo ""
echo 'export PS1="[ \033[01;33m\u\033[0;36m@\033[01;34m\h \033[01;31m\w\033[0m ]\033[0m \n#"' >>/etc/profile
echo ""
echo ""
cat >>/root/.vimrc <<EOF
syntax enable
syntax on
set ruler
set number
set cursorline
set cursorcolumn
set hlsearch
set incsearch
set ignorecase
set nocompatible
set wildmenu
set paste
set expandtab
set tabstop=2
set shiftwidth=4
set softtabstop=4
set gcr=a:block-blinkon0
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions-=R
highlight CursorLine cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE
highlight CursorColumn cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE
EOF
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
echo ""
echo ""
systemctl stop firewalld && systemctl disable firewalld
echo ""
echo ""
systemctl stop postfix.service && systemctl disable postfix.service
a=`service NetworkManager status &> /dev/null`
if [ $? -gt 0 ];then
log_info "NetworkManager已关闭"
else
log_error "NetworkManager未关闭,即将关闭NetworkManager"
systemctl disable NetworkManager && systemctl stop NetworkManager &> /dev/null
log_success "NetworkManager已关闭"
fi
}
set_repo() {
if [ -f $yum_dir"CentOS-Base.repo"]; then
mkdir -p ${
yum_dir}old && mv ${
yum_dir}* ${
yum_dir}old/
if curl -o $yum_dir"CentOS-Base.repo" $aliyun_url"repo/Centos-7.repo" && curl -o ${
yum_dir}epel-7.repo $aliyun_url"repo/epel-7.repo";then
log_success "CentOS-Base.repo、epel-7.repo下载成功"
yum clean all && yum makecache
else
log_error "CentOS-Base.repo、epel-7.repo下载失败请检查!"
fi
else
log_info "CentOS-Base.repo non-existent"
curl -o $yum_dir"CentOS-Base.repo" $aliyun_url"repo/Centos-7.repo" && curl -o ${
yum_dir}epel-7.repo $aliyun_url"repo/epel-7.repo"
yum clean all && yum makecache
fi
}
install_softwares() {
yum install -y nc telnet net-tools wget lsof vim lrzsz jq bash-completion zip unzip epel-release tree ntpdate gcc gcc-c++ make cmake yum-utils
}
time_lock () {
a=`rpm -qa | grep ntpdate`
if [ $? -eq 0 ];then
log_info "ntpdate已经安装"
else
log_info "ntpdate没有安装,即将进行安装"
yum install -y ntpdate &> /dev/null
log_success "ntpdate安装完成"
fi
ntpdate time1.aliyun.com &> /dev/null
log_success "时间同步完成"
systemctl start ntpdate && systemctl enable ntpdate &> /dev/null
}
user_add() {
GROUP="/etc/group"
group_admin=$(cat ${
GROUP} | grep admin -c)
if [ "$group_admin" -ge 1 ]; then
echo "the group admin is already "
else
groupadd admin
echo "%admin ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
useradd ops -g admin
echo "ops:Ops@1234" | chpasswd
fi
}
reboot_init () {
reboot
}
action=$1
function usage() {
echo "$(gettext 'System Initialization Script')"
echo
echo "Usage: "
echo " ./initial_system.sh [COMMAND] [ARGS...]"
echo " ./initial_system.sh --help"
echo "System Initialization: "
echo " 1) $(gettext '修改主机名')"
echo " 2) $(gettext '配置网卡设备名')"
echo " 3) $(gettext '配置IP')"
echo " 4) $(gettext '系统参数优化')"
echo " 5) $(gettext '设置阿里yum源')"
echo " 6) $(gettext '时间同步设置')"
echo " 7) $(gettext '安装常用软件')"
echo " 8) $(gettext '创建运维账号')"
echo " 9) $(gettext '重启服务器')"
}
main () {
while true
do
read -p "输入你的序号(按Q键退出):" action
if [[ "${action}" == "help" || "${action}" == "h" || "${action}" == "-h" || "${action}" == "--help" ]]; then
usage
else
case $action in
1)
clear
host_name
sleep 2
;;
2)
clear
set_ethc
sleep 2
;;
3)
clear
set_ipc
sleep 2
;;
4)
clear
system_optimization
sleep 2
;;
5) clear
set_repo
sleep 2
;;
6)
clear
time_lock
sleep 2
;;
7)
clear
install_softwares
sleep 2
;;
8)
clear
user_add
sleep 2
;;
9)
clear
reboot_init
sleep 2
;;
Q)
exit
;;
*)
log_info "No such command: ${action}"
esac
fi
done
}
main