Centos 7 시스템 초기 화 스 크 립 트

19660 단어 linuxshell
Centos 7 시스템 초기 화 스 크 립 트\#
앞에서 말 했 듯 이 회사 업무 가 증가 하여 서버 가 계속 추 가 됩 니 다. 가끔 몇 대 씩 오 면 수 동 으로 한 대 씩 서버 초기 화 작업 에 너무 번 거 롭 습 니 다.따라서 초기 화 수요 에 따라 초기 화 스 크 립 트 를 통합 하여 대량 스 크 립 트 초기 화 작업 을 실현 합 니 다.
설명: 이 스 크 립 트 는 자신의 요구 에 따라 작성 되 었 으 며, Centos 7 서버 의 기본 초기 화 절 차 를 통합 하 였 습 니 다.
그 중 에는 다음 과 같은 기초 최적화 내용 이 포함 되 어 있다. 1) SELinux 종료;2) Firewalld 종료;3) Bash 환경 수정;4) Openfile 시스템 의 최대 파일 수 설정 열기;5) 시스템 커 널 파라미터 최적화 설정;6) 호스트 이름 변경;7) 역사 기록 설정;8) 개성 화 된 설정 등.메모: A) 스 크 립 트 가 실행 되면 서버 를 자동 으로 다시 시작 합 니 다.B) 스 크 립 트 를 실행 하기 전에/etc/hosts 에 대응 하 는 해석 을 설정 해 야 합 니 다. 예 를 들 어 10.10.10 kazihuo 내용 을 hosts 파일 에 추가 하고 스 크 립 트 를 실행 한 후 서버 10.10.10.10.10 은 자동 으로 Hostname 호스트 이름 을 "jmpserver"로 설정 합 니 다.C)/tmp/sysctl. conf 파일 이 존재 하 는 지 확인 합 니 다. 설 정 된 Kernel 커 널 최적화 매개 변수 파일 을/tmp 디 렉 터 리 에 배치 하고 스 크 립 트 를 실행 하면 최적화 매개 변 수 는 서버 에 자동 으로 설 정 됩 니 다.최적화 파일 이 없 으 면 마지막 함수 에 Kernel 을 주석 하면 됩 니 다.
내용: 스 크 립 트 내용 은 다음 과 같 습 니 다: [root@jumpserver ~]# cat/shell/init.sh
在这里插入代码片
```#!/bin/bash
#====================================================
# Author: Mr.song
# Blog: https://blog.csdn.net/qq_46229380/article/details/108982089
# Create Date: 2020-10-9
# Description: It works for system initalization.
#====================================================

#State:Plese confirm the files of /etc/hosts and /tmp/sysctl.conf before using the script

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

# Defined result function
function Msg(){
      
    if [ $? -eq 0 ];then
        action "$1" /bin/true
    else
        action "$1" /bin/false
    fi
}

# Defined close selinux function
function Selinux(){
      
    [ -f /etc/selinux/config ] && {
      
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
    Msg "Close selinux"
    }
}

# Defined close firewalld function
function Firewalld(){
      
    systemctl stop firewalld.service
    systemctl disable firewalld.service  >/dev/null 2>&1
    Msg "Close firewalld"
}

# Defined bashrc function
function Bashrc(){
      
    sed -i 's/\\h \\W/\\h \\w/g' /etc/bashrc
    Msg "Bashrc"
}

# Defined open files function for Centos6.
function Openfile6(){
      
    if [ `egrep "^\*" /etc/security/limits.conf|wc -l` -eq 0 ];then
        echo '* - nofile 65535' >> /etc/security/limits.conf
        ulimit -SHn 65535
        Msg "Open files"
    fi
}

# Defined open files function for Centos7.
function Openfile7(){
      
    if [ `egrep "^De" /etc/systemd/system.conf|wc -l` -eq 0 ];then
        echo 'DefaultLimitCORE=infinity' >> /etc/systemd/system.conf
        echo 'DefaultLimitNOFILE=100000' >> /etc/systemd/system.conf
        echo 'DefaultLimitNPROC=100000' >> /etc/systemd/system.conf
        ulimit -SHn 100000
        Msg "Open files"
    fi
}

# Defined kernel paramters function
function Kernel(){
      
    if [ -f /tmp/sysctl.conf ];then
        /usr/bin/\cp /etc/sysctl.conf /etc/sysctl.conf.$RANDOM
        /usr/bin/\cp /tmp/sysctl.conf /etc/
        sysctl -p >/dev/null 2>&1
        Msg "kernel paramters"
    else
        echo "/tmp/sysctl.conf is not exist"
    fi
}

# Defined hostname function
function Hostname(){
      
    ip=`/usr/sbin/ip addr|grep brd|awk 'NR==3{print $2}'|awk -F "/" '{print $1}'`
    name=`grep -w "$ip" /etc/hosts|awk '{print $2}'`
    if [ -z $name ];then
        sleep 1
    else
        echo $name > /etc/hostname
        hostnamectl set-hostname $name
        Msg "Hostname"
    fi
}

# Defined device function
function Device(){
      
    /usr/sbin/ip addr|grep ens192  >/dev/null
    RETVAL=$?
    if [ $RETVAL -ne 0 ];then
        /usr/bin/mv /etc/sysconfig/network-scripts/ifcfg-e* /etc/sysconfig/network-scripts/ifcfg-ens192 >/dev/null 2>&1
        sed -i 's/quiet/quiet net.ifnames=0 biosdevname=0/g' /etc/default/grub
        sed -i 's/^DEVICE/#DEVICE/g' /etc/sysconfig/network-scripts/ifcfg-e*
        sed -i '1i DEVICE=ens192' /etc/sysconfig/network-scripts/ifcfg-e*
        /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg >/dev/null 2>&1
        Msg "Device--[WARNING]effecting after reboot~~~"
    else
        echo "the name of eths is exist"
    fi
}

# History collect
function History(){
      
    cat >>/etc/profile.d/history.sh <<EOF
#history
USER=\`whoami\`
USER_IP=\`who -u am i 2>/dev/null|egrep -o "([0-9]{1,3}\\.){3}[0-9]{1,3}"\`
if [ "\$USER_IP" = "" ]; then
USER_IP=\`hostname\`
fi
if [ ! -d /var/log/history ]; then
mkdir /var/log/history
chmod 777 /var/log/history
fi
if [ ! -d /var/log/history/\${
      LOGNAME} ]; then
mkdir /var/log/history/\${
      LOGNAME}
chmod 300 /var/log/history/\${
      LOGNAME}
fi
export HISTSIZE=4096
DT=\`date +"%Y%m%d_%H:%M:%S"\`
export HISTFILE="/var/log/history/\${LOGNAME}/\${USER}@\${USER_IP}_\$DT"
chmod 600 /var/log/history/\${LOGNAME}/*history* 2>/dev/null
EOF
    Msg "History collect"
}

# Defined the hobby.
function Hobby(){
     
    mkdir -p /{
     luomurui,luomurui-bak}/{
     scr,pkg,test,info}
}

# Defined wait function
function Wait(){
     
    echo ""
    echo -n -e "\033[31mTHE SYSTEM IS REBOOTING\033[0m"
    for ((i=0;i<3;i++))
    do
        echo -n "~~ "
        sleep 1
    done
    echo
}

# Defined main function
function main(){
     
Selinux
Firewalld
Bashrc
#Openfile6
Openfile7
Kernel
Hostname
Device
History
Hobby
Wait
reboot
}
main


좋은 웹페이지 즐겨찾기