Linux Initialization

  

Universal:

  1. Disable Bell In the Terminal
    sed -i '/bell-style none/c \set bell-style none' /etc/inputrc

     

Ubuntu:  

  1. Setup Fountain
    sed -i 's@http://.*.ubuntu.com@http://mirrors.aliyun.com@g' /etc/apt/sources.list
    OR
    sed -i -e 's/archive.ubuntu.com/mirrors.aliyun.com/g' -e 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 
  2. Install Basic Software
    apt install ca-certificates lsb-release software-properties-common tree apt-file net-tools info
  3.  Collocation Environ (Workstation)

    printf "%-8s %-25s %-4s %s\n" @@@@@ ~/.profile '==>' Commence
    printf "%-8s %-25s %-4s %s\n" @@@@@ ~/.profile '==>' Closure
    printf "%-8s %-25s %-4s %s\n" @@@@@ ~/.bashrc '==>' Commence
    printf "%-8s %-25s %-4s %s\n" @@@@@ ~/.bashrc '==>' Closure
    printf "%-8s %-25s %-4s %s\n" @@@@@ /etc/profile '==>' Commence
    printf "%-8s %-25s %-4s %s\n" @@@@@ /etc/profile '==>' Closure
    printf "%-8s %-25s %-4s %s\n" @@@@@ /etc/bash.bashrc '==>' Commence
    printf "%-8s %-25s %-4s %s\n" @@@@@ /etc/bash.bashrc '==>' Closure
    printf "%-8s %-25s %-4s %s\n" @@@@@ /etc/profile.d/init.sh '==>' ^_^ 

  4. locale 

    ll /usr/share/i18n
    dpkg -L locales
    dpkg-reconfigure locales
    update-locale ==> /etc/default/locale
    update-locale --reset
    update-locale LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 
  5. Remove Redundant Kernel

    uname -a
    dpkg --get-selections | grep linux
    dpkg --get-selections | grep -e 'linux.*36' -e 'linux.*43' | awk '{print $1}' | xargs dpkg --purge
  6. Clear Caches i.e. Firefox ( Workstation ) 

    rm -rf  /home/flail/.mozilla/firefox/*.default/Cache
  7. Clear APT Caches

    apt-get autoclean
    apt-get clean  # /var/cache/apt/archives/ exclude archives/lock archives/partial
    rm -f /var/cache/apt/*.bin  # pkgcache.bin srcpkgcache.bin
    rm -rf /var/lib/apt/lists/*
    dpkg -l | grep '^rc' | awk '{print $2}' | xargs dpkg --purge  # 清除残余配置文件
  8. Disable Auto-upgrade
    dpkg-reconfigure unattended-upgrades

    /etc/apt/apt.conf.d/20auto-upgrades

      
  9. [Time]
    NTP=time.windows.com ntp1.aliyun.com
    FallbackNTP=ntp2.aliyun.com ntp3.aliyun.com
    RootDistanceMaxSec=5
    PollIntervalMinSec=32
    PollIntervalMaxSec=2048
    timedatectl set-ntp true
    systemctl --now enable systemd-timesyncd
    timedatectl show | status

     

 RHEL:  

  1.  /etc/profile.d/init.sh      初始化脚本 
    echo 'source /etc/init.d/init' >> ~/.bashrc

     

    #---------------------------------------------------------------------
    # History
    #---------------------------------------------------------------------
    export HISTCONTROL=ignoredups
    export HISTFILE=$HOME/.bash_history
    export HISTFILESIZE=2000
    export HISTSIZE=1000
    shopt -s histappend
    
    if unset "${HISTTIMEFORMAT}" 2>/dev/null; then
      export HISTTIMEFORMAT="$(echo -e "\e[7m$(whoami)\e[0m") %F %T>> "
      typeset -r HISTTIMEFORMAT
    fi
    
    
    #---------------------------------------------------------------------
    # Fedora Default Editor
    #---------------------------------------------------------------------
    if grep -iq 'Fedora' /etc/os-release; then
      export EDITOR=/usr/bin/vim
    fi
    
    
    #---------------------------------------------------------------------
    # Trap
    #---------------------------------------------------------------------
    trap 'echo -e "\U26A1 \e[30;46mCommand >>$BASH_COMMAND<< failed with error code $?\e[0m \U1F300"' ERR
    trap 'echo $BASH_COMMAND > /dev/null' DEBUG
    
    
    #---------------------------------------------------------------------
    # PS1
    #---------------------------------------------------------------------
    case $(awk '/^ID/' /etc/os-release | awk -F= '{print tolower($2)}' | tr -d \") in
      centos | fedora)
        source /usr/share/git-core/contrib/completion/git-prompt.sh
    esac
    
    function lampooner() {
      if git rev-parse --git-dir >&/dev/null; then
        message=''
        # git_dir
        if [[ $(git rev-parse --is-inside-git-dir 2>/dev/null) == true ]]; then
          PS1="\[\e[1;31m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\][\[\e[1;33m\]\t\[\e[0m\]]:\[\e[36m\](GIT_DIR)\[\e[0m\]\[\e[1;34m\]\w\[\e[0m\]\\$ "
          return 0
        fi
        # cherry-pick
        if git status 2>/dev/null | grep -q 'cherry-pick'; then
          message='|Cherry-pick'
        fi
        # rebase
        if git status 2>/dev/null | grep -q 'rebase'; then
          message+='|Rebase'
        fi
        # merge
        case $(git status --short | head -1 | cut --characters 1-2) in
        DD | AU | UD | UA | DU | AA | UU)
          message+='|Merging'
          ;;
        *)
          :
          ;;
        esac
        if git status | grep -qi conclude; then
          message+='|Resolved'
        fi
        # branch
        branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
    
        if [ "$branch" != "" ]; then
          if [ "$branch" == HEAD ]; then
            branch=$(git rev-parse --short HEAD 2>/dev/null)
            if [ $? != 0 ]; then
              branch=Lank
            fi
          fi
        fi
        PS1="\[\e[1;31m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\][\[\e[1;33m\]\t\[\e[0m\]]:\[\e[36m\](${branch}${message})\[\e[0m\]\[\e[1;34m\]\w\[\e[0m\]\\$ "
      else
        PS1="\[\e[1;31m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\][\[\e[1;33m\]\t\[\e[0m\]]:\[\e[1;34m\]\w\[\e[0m\]\\$ "
      fi
    }
    
    function languor() {
      if git rev-parse --git-dir >&/dev/null; then
        local msg
        msg=$(__git_ps1)
        msg=${msg:1}
        PS1="${VENV}${CONDA_PROMPT_MODIFIER}\[\e[1;31m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\][\[\e[1;33m\]\t\[\e[0m\]]:\[\e[36m\]${msg}\[\e[0m\]\[\e[1;34m\]\w\[\e[0m\]\\$ "
      else
        PS1="${VENV}${CONDA_PROMPT_MODIFIER}\[\e[1;31m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\][\[\e[1;33m\]\t\[\e[0m\]]:\[\e[1;34m\]\w\[\e[0m\]\\$ "
      fi
    }
    
    declare -x PROMPT_COMMAND=languor
    
    
    #---------------------------------------------------------------------
    # User
    #---------------------------------------------------------------------
    # if [[ $(id -u) -eq 0 ]]; then
    #   echo -e "\U1F308\U2744\U1F319 \e[34;47mWelcome Root\e[0m \U1F31F\U1F320\U231B"
    # else
    #   echo "\U1F31B\U1F31F Welcome Ordinary"
    # fi
    
    
    #---------------------------------------------------------------------
    # Completion
    #---------------------------------------------------------------------
    if command -v kubectl >&/dev/null; then
      source <(kubectl completion bash)
    fi
    
    
    #---------------------------------------------------------------------
    # Distro
    #---------------------------------------------------------------------
    DISTRO=$(tr "[:upper:]" "[:lower:]" </etc/os-release | grep -Poi '(debian|ubuntu|red hat|centos|fedora)' | sort | uniq)
    if [ -z "$DISTRO" ]; then
      DISTRO=unknow
    fi

     

     

  2. 配置centos-7源
    # delete obsolete antique
    rm -rf /etc/yum.repos.d/*
    # epel
    curl -so /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    # base updates extras contrib centosplus
    curl -so /etc/yum.repos.d/base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/base.repo
    # docker-ce
    yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    # kubernetes
    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    yum clean all -v
    yum makecache

     

  3. 禁用 firewalld NetworkManager  selinux
    systemctl mask --now firewalld  
    systemctl mask --now dnsmasq
    systemctl mask --now NetworkManager  
    
    setenforce 0
    sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
    #关闭swap分区
    swapoff -a && sysctl -w vm.swappiness=0
    sed -r -i '/^[^#]*swap/ s@^@#@' /etc/fstab
    echo 'vm.swappiness = 0' >> /etc/kubernetes.conf

     

  4. 安装基本软件
    yum -y install wget net-tools tree sysstat vim telnet yum-utils rsync chrony info

     

  5. 初始化分区
    UEFI

     

      

      

      

  6. 更新内核, 移除旧内核
    rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm

      

      

    yum-config-manager --disable elrepo
    yum-config-manager --enable elrepo-kernel

     

     

     

      

      

      

     需先卸载原内核kernel-tools-libs kernel-tools



    检查内核

    yum check-update kernel*

     修改默认内核

     

      

     


    第一种方式

     

     

     只保留一个内核


    移除旧内核相关文件


    一个内核的相关文件

     

      

    rpm -qa kernel* | grep -v `uname -r`  # 查看当前没有使用的内核包
    grub2-editenv list  # 查看默认内核
    grub2-set-default 0
    grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
    
    yum -y update --exclude=kernel\*  # 升级软件包(排除内核)
    
    grubby --update-kernel="$(grubby --default-kernel)" --args="user_namespace.enable=1"  # 为当前内核添加参数
    grubby --info="(grubby --default-kernel)"  # 查看默认内核

     

  7. 时间同步
    采用ntpdate + crond

    yum -y install ntpdate
    echo "*/5 * * * * `which ntpdate` time.windows.com" >> /var/spool/cron/root
    systemctl restart crond
    ln -svfn /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

     

VMware 虚拟环境:      9台试验机

  1. 密钥

    ssh-keygen -t ed25519 -P '' -C ram -f ~/.ssh/id_ed25519
    cat ~/.ssh/id_ed25519.pub > ~/.ssh/authorized_keys
    for i in `seq 1 1 9`;do echo ram$i,192.168.8.1$i $(< /etc/ssh/ssh_host_ecdsa_key.pub) >> ~/.ssh/known_hosts;done

     

  2. /etc/hosts
    echo -e '\n\n# ratiocination' >> /etc/hosts
    for i in {1..9};do echo 192.168.8.1$i ram$i >> /etc/hosts;done

     

  3. clone前, 清理 garbage
    yum clean all
    rm -rf /tmp/*
    > ~/.bash_history
    history -c

     

  4. 每台clone虚拟机 修改主机名,   IP


  5. /etc/issue
    \S (Terminal: \l)
    Kernel: \r  ==  Platform: \m  ==  Hostname: \n
    Date: \d \t
    

      

posted @ 2021-06-18 13:22  ascertain  阅读(48)  评论(0编辑  收藏  举报