乾貨!超實用的 Linux 初始化腳本

来源:https://www.cnblogs.com/edisonfish/archive/2023/03/03/17175575.html
-Advertisement-
Play Games

鹹魚今天給大家分享一個無論是學習還是工作中都很實用的 Linux 系統初始化腳本,其實就是各種命令的集合 完整代碼在文章最後哦 定義相關變數 配置 yum 鏡像源 獲取阿裡雲 yum 鏡像源 判斷函數是否執行成功 寫入一行配置 修改配置 配置系統時區 配置 dns 伺服器 修改最大文件描述符限制 關 ...


鹹魚今天給大家分享一個無論是學習還是工作中都很實用的 Linux 系統初始化腳本,其實就是各種命令的集合

 

完整代碼在文章最後哦

 

定義相關變數

 

 

 

配置 yum 鏡像源

 

 

獲取阿裡雲 yum 鏡像源

 

 

判斷函數是否執行成功

 

 

寫入一行配置

 

 

修改配置

 

 

配置系統時區

 

 

配置 dns 伺服器

 

 

修改最大文件描述符限制

 

 

關閉系統不需要的服務

 

 

內核參數優化相關

 

 

安裝常用工具

 

 

關閉 SELinux

 

 

主函數

 

 

完整腳本

#環境變數
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

#當前時間
current_time=$(date +%Y%m%d)

#阿裡的DNS
dns_server=223.5.5.5 

if [[ ! -z `uname -r|grep 'el6'` ]]
  then
  kernel_version=el6
  yum_repo=http://mirrors.aliyun.com/repo/Centos-6.repo
elif [[ ! -z `uname -r|grep 'el7'` ]]
  then
  kernel_version=el7
  yum_repo=http://mirrors.aliyun.com/repo/Centos-7.repo
else
  echo -e "\e[31mUnidentified Kernel version: $(uname -r). Only support for kernel el6/el7\e[0m"
  exit
fi

function add_yum_repo(){
  local item="Add Aliyun Yum Mirrors"
  yum clean all
  cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.${current_time} && \
  curl -o /etc/yum.repos.d/CentOS-Base.repo ${yum_repo} > /dev/null 2>&1
  show_result $? "${item}"
  yum makecache
}

function show_result(){
  if [ "$1" -eq 0 ]
    then
      echo -e "\e[32m$2 is Success .   [ OK ] \e[0m"
    else
      echo -e "\e[31m$2 is Fail .   [ FAIL ] \e[0m"
  fi
}

function add_newconfig_tofile(){
  local SearchResult=`grep "$1" "$2"`
  if [ -z "${SearchResult}" ]
    then
    echo "$1" >> $2
  fi
}

function add_config_tofile(){
  local keywords=`echo $1| awk -F "[= ]+" '{print $1}'`
  local SearchResult=`grep "^${keywords}" "$2"`
  if [ -z "${SearchResult}" ] #空為真,非空為假
    then
    echo $1 >> $2
  else
    sed -i "s/^${keywords}.*/$1/" $2
  fi
}

function config_localtime(){
  local item="Config SH As Location"
  rm -f /etc/localtime
  ln -s  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  show_result $? "${item}"
}

function config_dns_addr(){
  local item="Config DNS Address"
  cp /etc/resolv.conf /etc/resolv.conf.${current_time} && \
  echo "nameserver ${dns_server}" > /etc/resolv.conf
  show_result $? "${item}"
}

function maximum_file_dspt(){
  local item="Maximum File Descriptor"
  cp /etc/security/limits.conf /etc/security/limits.conf.${current_time} && \
  echo "*           soft    nofile          100000
*           hard    nofile          100000
*           soft    nproc           65535
*           hard    nproc           65535
*           soft    core            unlimited
*           hard    core            unlimited" > /etc/security/limits.conf
  show_result $? "${item}"
}


function shutdown_nonuse_srv(){
  local item="Shutdown Unused Services"
  if [[ "${kernel_version}" == el6 ]]
      then
    for i in `chkconfig --list | awk '{print $1}'`
      do
      chkconfig --level 2345 $i off > /dev/null 2>&1
      done
    for ii in crond network rsyslog sshd sysstat haldaemon
      do
      chkconfig --level 2345 $ii on > /dev/null 2>&1
      done
    show_result $? "${item}"
  elif [[ "${kernel_version}" == el7 ]]
    then
    systemctl disable postfix > /dev/null 2>&1
    show_result $? "${item}"
  else
    echo -e "\e[31mUnidentified Kernel version: $(uname -r). Only support for kernel el6/el7\e[0m"
  fi
}

function optimize_kel_args(){
  local item="Optimize Kernel Arguments"
  cp /etc/sysctl.conf /etc/sysctl.conf.${current_time} > /dev/null 2>&1
  arch_ratio=$([[ ! -z $(uname -a | grep x86_64) ]] && expr 64 / 32 || expr 32 / 32)
  memory_size=$(free -b| awk 'NR==2{print $2}')
  nf_conntrack_size=$(expr ${memory_size} / 16384 / ${arch_ratio})
  #開啟反向路徑過濾
  add_config_tofile "net.ipv4.conf.default.rp_filter = 1" /etc/sysctl.conf
  add_config_tofile "net.ipv4.conf.all.rp_filter = 1" /etc/sysctl.conf
  #處理無源路由包
  add_config_tofile "net.ipv4.conf.all.accept_source_route = 0" /etc/sysctl.conf
  add_config_tofile "net.ipv4.conf.default.accept_source_route = 0" /etc/sysctl.conf
  #core文件名中添加pid作為擴展名
  add_config_tofile "kernel.core_uses_pid = 1" /etc/sysctl.conf
  #開啟syn洪水攻擊保護
  add_config_tofile "net.ipv4.tcp_syncookies = 1" /etc/sysctl.conf
  #修改消息隊列長度
  add_config_tofile "kernel.msgmnb = 65536" /etc/sysctl.conf
  add_config_tofile "kernel.msgmax = 65536" /etc/sysctl.conf
  #修改最大記憶體共用段大小bytes
  add_config_tofile "kernel.shmmax = 68719476736" /etc/sysctl.conf
  add_config_tofile "kernel.shmall = 4294967296" /etc/sysctl.conf
  #timewait數量預設18000
  add_config_tofile "net.ipv4.tcp_max_tw_buckets = 600" /etc/sysctl.conf
  add_config_tofile "net.ipv4.tcp_sack = 1" /etc/sysctl.conf
  add_config_tofile "net.ipv4.tcp_window_scaling = 1" /etc/sysctl.conf
  add_config_tofile "net.ipv4.tcp_rmem = 4096 87380 16777216" /etc/sysctl.conf
  add_config_tofile "net.ipv4.tcp_wmem = 4096 65536 16777216" /etc/sysctl.conf
  add_config_tofile "net.core.rmem_default = 8388608" /etc/sysctl.conf
  add_config_tofile "net.core.wmem_max = 16777216" /etc/sysctl.conf
  #未收到客戶端確認信息連接請求的最大值
  add_config_tofile "net.ipv4.tcp_max_syn_backlog = 262144" /etc/sysctl.conf
  #放棄建立連接之前發送的synack包
  add_config_tofile "net.ipv4.tcp_syn_retries = 2" /etc/sysctl.conf
  #開啟重用,允許time—wait socket 重新用語新的tcp連接
  add_config_tofile "net.ipv4.tcp_tw_reuse = 1" /etc/sysctl.conf
  add_config_tofile "net.ipv4.tcp_fin_timeout = 1" /etc/sysctl.conf
  #防止簡單的ddos攻擊
  add_config_tofile "net.ipv4.tcp_max_orphans = 3276800" /etc/sysctl.conf
  #啟用timewait快速收回
  add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
  #keeptime啟用時tcp發送keepalive消息的頻度,預設2h
  add_config_tofile "net.ipv4.tcp_keepalive_time = 600" /etc/sysctl.conf
  #允許系統打開的埠範圍
  add_config_tofile "net.ipv4.ip_local_port_range = 1024 65535" /etc/sysctl.conf
    #資源回收
    add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
    #路由轉發
    add_config_tofile "net.ipv4.ip_forward = 1" /etc/sysctl.conf 
  #修改防火牆連接跟蹤表大小,預設65535
  add_config_tofile "net.netfilter.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
  add_config_tofile "net.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
  #解禁ping
  add_config_tofile "net.ipv4.icmp_echo_ignore_all = 0" /etc/sysctl.conf
      modprobe bridge
  sysctl -p > /dev/null 2>&1
  show_result $? "${item}"
}

function install_pkgs(){
  local item="Install Common Pkgs"
  yum -y groupinstall "Development libraries" > /dev/null 2>&1
  yum -y groupinstall "Development tools" > /dev/null 2>&1
  yum -y install sysstat  tree  lrzsz  telnet wget net-tools tcpdump lsof vim ntp > /dev/null 2>&1
  show_result $? "${item}"
}

function shutdown_selinux(){
  local item="Shutdown Selinux "
  setenforce 0 > /dev/null 2>&1
  cp /etc/selinux/config /etc/selinux/config.${current_time} && \
  sed -i 's:SELINUX=enforcing:SELINUX=disabled:g' /etc/selinux/config
  show_result $? "${item}"
}

function main(){
  echo -e '\033[34;1m開始初始化操作系統中......\033[0m'
  add_yum_repo
  install_pkgs
  config_localtime
  config_dns_addr
  maximum_file_dspt
  shutdown_nonuse_srv
  shutdown_selinux
  optimize_kel_args
  echo -e '\033[34;1m伺服器系統初始化已完成!\033[0m'
}

main

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 背景 最近在重構自己曾經的代碼, 具體需求是在Unity等待如一個模型動畫, 一段ui動畫 如下: Await的目標 await的目標是一個可等待對象, 而擁有GetAwaiter方法並且該方法擁有合適返回值的目標即可稱為可等待對象(暫時你還不需要知道返回值需要符合什麼規則,待會兒Studio會告訴 ...
  • WPF相對於Winform而言,在WPF中是用不同的容器安排佈局。每個容器都有各自的佈局邏輯,有的以堆棧方式佈置有的以單元格排列元素。這也是WPF中比較有意思的,更容易入門。通過瞭解WPF佈局之後能有個大概的WPF樂趣之處。 1 - 理解WPF中佈局 區別於Winform而言,Winform中使用刻 ...
  • 一個應用要運行起來,往往需要讀取很多的預設好的配置信息,根據約定好的信息或方式執行一定的行為。 配置的本質就是軟體運行的參數,在一個軟體實現中需要的參數非常多,如果我們以 Hard Code(硬編碼)的方式寫在應用代碼中,這樣配置就會很亂,而且後續也不容易修改。亂而多,而且不容易修改,這就需要一個統 ...
  • winform使用PDFDocument 、PDFPageView查看pdf其他pdf文件沒有問題,但有一個pdf文件圖片無法顯示,但當在該pdf文件上編譯後編譯的內容可以看見(如下圖)。 後一點點排查發現在.csproj文件中 <PlatformTarget>x86</PlatformTarget ...
  • 最近在忙於 Fireasy 的重構,`3.x` 拋棄了 `.Net Framework` 時代的一些思想和模式,緊密擁抱 `.Net Core`,但它的思想仍然是**開放性**和**靈活性**。今天我主要來說說依賴註入與服務發現。 ...
  • 企業面試題 京東 問題1:使用Linux命令查詢file1中空行所在的行號。 [root@server ~]# cat file1 問題1:使用Linux命令查詢file1中空行所在的行號。 [root@server ~]# awk '/^$/{print NR}' file1 2 問題2:有文件c ...
  • 【安裝準備】 1、準備一個U盤,可儲存空間不低於20G,U盤內資料移出去,待會兒要格式化做U盤啟動盤 2、windows操作系統上下載“Rufus”,官網:http://rufus.ie/zh/,製作U盤啟動盤所用的軟體 3、通過https://openanolis.cn/download下載Ano ...
  • 前言 在上一篇文章中,儘管使用了變數和模式,但還是有不夠好的地方,在Makefile中要指明每一個源文件,我們接下來利用函數對其進行優化,並介紹其他常用的一些函數。 依舊是以fun.c ,main.c 和Makefile三個文件為例,文件內容就不再貼出來了,前兩篇文章中都有。 1.wildcard函 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...