翼度科技»论坛 云主机 LINUX 查看内容

干货!超实用的 Linux 初始化脚本

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
咸鱼今天给大家分享一个无论是学习还是工作中都很实用的 Linux 系统初始化脚本,其实就是各种命令的集合
 
完整代码在文章最后哦
 
定义相关变量
 
 
 
配置 yum 镜像源

 
 
获取阿里云 yum 镜像源

 
 
判断函数是否执行成功

 
 
写入一行配置

 
 
修改配置

 
 
配置系统时区

 
 
配置 dns 服务器

 
 
修改最大文件描述符限制

 
 
关闭系统不需要的服务

 
 
内核参数优化相关

 
 
安装常用工具

 
 
关闭 SELinux

 
 
主函数

 
 
完整脚本
  1. #环境变量
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  3. export PATH
  4. #当前时间
  5. current_time=$(date +%Y%m%d)
  6. #阿里的DNS
  7. dns_server=223.5.5.5
  8. if [[ ! -z `uname -r|grep 'el6'` ]]
  9.   then
  10.   kernel_version=el6
  11.   yum_repo=http://mirrors.aliyun.com/repo/Centos-6.repo
  12. elif [[ ! -z `uname -r|grep 'el7'` ]]
  13.   then
  14.   kernel_version=el7
  15.   yum_repo=http://mirrors.aliyun.com/repo/Centos-7.repo
  16. else
  17.   echo -e "\e[31mUnidentified Kernel version: $(uname -r). Only support for kernel el6/el7\e[0m"
  18.   exit
  19. fi
  20. function add_yum_repo(){
  21.   local item="Add Aliyun Yum Mirrors"
  22.   yum clean all
  23.   cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.${current_time} && \
  24.   curl -o /etc/yum.repos.d/CentOS-Base.repo ${yum_repo} > /dev/null 2>&1
  25.   show_result $? "${item}"
  26.   yum makecache
  27. }
  28. function show_result(){
  29.   if [ "$1" -eq 0 ]
  30.     then
  31.       echo -e "\e[32m$2 is Success .   [ OK ] \e[0m"
  32.     else
  33.       echo -e "\e[31m$2 is Fail .   [ FAIL ] \e[0m"
  34.   fi
  35. }
  36. function add_newconfig_tofile(){
  37.   local SearchResult=`grep "$1" "$2"`
  38.   if [ -z "${SearchResult}" ]
  39.     then
  40.     echo "$1" >> $2
  41.   fi
  42. }
  43. function add_config_tofile(){
  44.   local keywords=`echo $1| awk -F "[= ]+" '{print $1}'`
  45.   local SearchResult=`grep "^${keywords}" "$2"`
  46.   if [ -z "${SearchResult}" ] #空为真,非空为假
  47.     then
  48.     echo $1 >> $2
  49.   else
  50.     sed -i "s/^${keywords}.*/$1/" $2
  51.   fi
  52. }
  53. function config_localtime(){
  54.   local item="Config SH As Location"
  55.   rm -f /etc/localtime
  56.   ln -s  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  57.   show_result $? "${item}"
  58. }
  59. function config_dns_addr(){
  60.   local item="Config DNS Address"
  61.   cp /etc/resolv.conf /etc/resolv.conf.${current_time} && \
  62.   echo "nameserver ${dns_server}" > /etc/resolv.conf
  63.   show_result $? "${item}"
  64. }
  65. function maximum_file_dspt(){
  66.   local item="Maximum File Descriptor"
  67.   cp /etc/security/limits.conf /etc/security/limits.conf.${current_time} && \
  68.   echo "*           soft    nofile          100000
  69. *           hard    nofile          100000
  70. *           soft    nproc           65535
  71. *           hard    nproc           65535
  72. *           soft    core            unlimited
  73. *           hard    core            unlimited" > /etc/security/limits.conf
  74.   show_result $? "${item}"
  75. }
  76. function shutdown_nonuse_srv(){
  77.   local item="Shutdown Unused Services"
  78.   if [[ "${kernel_version}" == el6 ]]
  79.       then
  80.     for i in `chkconfig --list | awk '{print $1}'`
  81.       do
  82.       chkconfig --level 2345 $i off > /dev/null 2>&1
  83.       done
  84.     for ii in crond network rsyslog sshd sysstat haldaemon
  85.       do
  86.       chkconfig --level 2345 $ii on > /dev/null 2>&1
  87.       done
  88.     show_result $? "${item}"
  89.   elif [[ "${kernel_version}" == el7 ]]
  90.     then
  91.     systemctl disable postfix > /dev/null 2>&1
  92.     show_result $? "${item}"
  93.   else
  94.     echo -e "\e[31mUnidentified Kernel version: $(uname -r). Only support for kernel el6/el7\e[0m"
  95.   fi
  96. }
  97. function optimize_kel_args(){
  98.   local item="Optimize Kernel Arguments"
  99.   cp /etc/sysctl.conf /etc/sysctl.conf.${current_time} > /dev/null 2>&1
  100.   arch_ratio=$([[ ! -z $(uname -a | grep x86_64) ]] && expr 64 / 32 || expr 32 / 32)
  101.   memory_size=$(free -b| awk 'NR==2{print $2}')
  102.   nf_conntrack_size=$(expr ${memory_size} / 16384 / ${arch_ratio})
  103.   #开启反向路径过滤
  104.   add_config_tofile "net.ipv4.conf.default.rp_filter = 1" /etc/sysctl.conf
  105.   add_config_tofile "net.ipv4.conf.all.rp_filter = 1" /etc/sysctl.conf
  106.   #处理无源路由包
  107.   add_config_tofile "net.ipv4.conf.all.accept_source_route = 0" /etc/sysctl.conf
  108.   add_config_tofile "net.ipv4.conf.default.accept_source_route = 0" /etc/sysctl.conf
  109.   #core文件名中添加pid作为扩展名
  110.   add_config_tofile "kernel.core_uses_pid = 1" /etc/sysctl.conf
  111.   #开启syn洪水攻击保护
  112.   add_config_tofile "net.ipv4.tcp_syncookies = 1" /etc/sysctl.conf
  113.   #修改消息队列长度
  114.   add_config_tofile "kernel.msgmnb = 65536" /etc/sysctl.conf
  115.   add_config_tofile "kernel.msgmax = 65536" /etc/sysctl.conf
  116.   #修改最大内存共享段大小bytes
  117.   add_config_tofile "kernel.shmmax = 68719476736" /etc/sysctl.conf
  118.   add_config_tofile "kernel.shmall = 4294967296" /etc/sysctl.conf
  119.   #timewait数量默认18000
  120.   add_config_tofile "net.ipv4.tcp_max_tw_buckets = 600" /etc/sysctl.conf
  121.   add_config_tofile "net.ipv4.tcp_sack = 1" /etc/sysctl.conf
  122.   add_config_tofile "net.ipv4.tcp_window_scaling = 1" /etc/sysctl.conf
  123.   add_config_tofile "net.ipv4.tcp_rmem = 4096 87380 16777216" /etc/sysctl.conf
  124.   add_config_tofile "net.ipv4.tcp_wmem = 4096 65536 16777216" /etc/sysctl.conf
  125.   add_config_tofile "net.core.rmem_default = 8388608" /etc/sysctl.conf
  126.   add_config_tofile "net.core.wmem_max = 16777216" /etc/sysctl.conf
  127.   #未收到客户端确认信息连接请求的最大值
  128.   add_config_tofile "net.ipv4.tcp_max_syn_backlog = 262144" /etc/sysctl.conf
  129.   #放弃建立连接之前发送的synack包
  130.   add_config_tofile "net.ipv4.tcp_syn_retries = 2" /etc/sysctl.conf
  131.   #开启重用,允许time—wait socket 重新用语新的tcp连接
  132.   add_config_tofile "net.ipv4.tcp_tw_reuse = 1" /etc/sysctl.conf
  133.   add_config_tofile "net.ipv4.tcp_fin_timeout = 1" /etc/sysctl.conf
  134.   #防止简单的ddos攻击
  135.   add_config_tofile "net.ipv4.tcp_max_orphans = 3276800" /etc/sysctl.conf
  136.   #启用timewait快速收回
  137.   add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
  138.   #keeptime启用时tcp发送keepalive消息的频度,默认2h
  139.   add_config_tofile "net.ipv4.tcp_keepalive_time = 600" /etc/sysctl.conf
  140.   #允许系统打开的端口范围
  141.   add_config_tofile "net.ipv4.ip_local_port_range = 1024 65535" /etc/sysctl.conf
  142.     #资源回收
  143.     add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
  144.     #路由转发
  145.     add_config_tofile "net.ipv4.ip_forward = 1" /etc/sysctl.conf
  146.   #修改防火墙连接跟踪表大小,默认65535
  147.   add_config_tofile "net.netfilter.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
  148.   add_config_tofile "net.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
  149.   #解禁ping
  150.   add_config_tofile "net.ipv4.icmp_echo_ignore_all = 0" /etc/sysctl.conf
  151.       modprobe bridge
  152.   sysctl -p > /dev/null 2>&1
  153.   show_result $? "${item}"
  154. }
  155. function install_pkgs(){
  156.   local item="Install Common Pkgs"
  157.   yum -y groupinstall "Development libraries" > /dev/null 2>&1
  158.   yum -y groupinstall "Development tools" > /dev/null 2>&1
  159.   yum -y install sysstat  tree  lrzsz  telnet wget net-tools tcpdump lsof vim ntp > /dev/null 2>&1
  160.   show_result $? "${item}"
  161. }
  162. function shutdown_selinux(){
  163.   local item="Shutdown Selinux "
  164.   setenforce 0 > /dev/null 2>&1
  165.   cp /etc/selinux/config /etc/selinux/config.${current_time} && \
  166.   sed -i 's:SELINUX=enforcing:SELINUX=disabled:g' /etc/selinux/config
  167.   show_result $? "${item}"
  168. }
  169. function main(){
  170.   echo -e '\033[34;1m开始初始化操作系统中......\033[0m'
  171.   add_yum_repo
  172.   install_pkgs
  173.   config_localtime
  174.   config_dns_addr
  175.   maximum_file_dspt
  176.   shutdown_nonuse_srv
  177.   shutdown_selinux
  178.   optimize_kel_args
  179.   echo -e '\033[34;1m服务器系统初始化已完成!\033[0m'
  180. }
  181. main
复制代码
 

来源:https://www.cnblogs.com/edisonfish/p/17175575.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具