1. #!/bin/bash
  2. #
  3. #********************************************************************
  4. #encoding -*-utf8-*-
  5. #Author: zhangshang
  6. #Date: 2017-12-19
  7. #URL: http://blog.vservices.top/myblog
  8. #Description: The test script
  9. #Copyright (C): 2017 All rights reserved
  10. #QQ Numbers: 765030447
  11. #********************************************************************
  12. #查看系统版本
  13. Get_host_version=`cat /etc/centos-release | grep -i centos | grep -o "\<[[:digit:]]\+" |head -1`
  14. #查看内核版本
  15. kernel_version=`uname -r`
  16. #设置开机启动文件的权限
  17. chmod +x /etc/rc.d/rc.local
  18. #安装wget必备工具
  19. function Install_wget(){
  20. mount /dev/sr0 /mnt
  21. [ $? -ne 0 ] && { echo "未添加光盘源!退出脚本" ; kill -9 $$ ; }
  22. rpm -ivh /mnt/Packages/wget*
  23. cd /
  24. umount /mnt
  25. }
  26. #修改字符集位zh_CN.UTF-8
  27. function Modify_charaset(){
  28. echo 'export LANG=zh_CN.UTF-8' >>/etc/profile
  29. export LANG=zh_CN.UTF-8
  30. }
  31. #输出错误的系统版本
  32. function Error_system_version(){
  33. echo "未知的系统版本 $Get_host_version"
  34. }
  35. #备份操作的相关目录
  36. function Bakup_etc(){
  37. Now_of_time=`date +'%F_%H.%M'`
  38. back_path=/bak/initsys/
  39. mkdir -p $back_path
  40. tar -czf $back_path/etc.${Now_of_time}.tar.gz /etc
  41. }
  42. #关闭防火墙和selinux
  43. function Off_firewall_and_selinux(){
  44. #off firewall
  45. if [ "$Get_host_version" == 7 ]
  46. then
  47. systemctl stop firewalld &>/dev/null
  48. systemctl disable firewalld &>/dev/null
  49. elif [ "$Get_host_version" == 6 ]
  50. then
  51. service iptables stop &>/dev/null
  52. chkconfig iptables off &>/dev/null
  53. else
  54. Error_system_version
  55. return 1
  56. fi
  57. #off selinux
  58. sed -ri 's/^(SELINUX=).*$/\1disabled/g' /etc/selinux/config
  59. setenforce 0
  60. }
  61. #配置时区和时间
  62. function Set_timezone_and_time(){
  63. /usr/bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  64. #/usr/sbin/ntpdate 10.11.23.22 #设置ntp服务器同步,如果需要取消注释
  65. #hwclock -w #同步系统时间到硬件时间
  66. if [ "$Get_host_version" == '6' ]
  67. then
  68. cat > /etc/sysconfig/clock << EOF
  69. ZONE="Asia/Shanghai"
  70. UTC=false
  71. ARC=false
  72. EOF
  73. elif [ "$Get_host_version" == '7' ]
  74. then
  75. timedatectl set-local-rtc yes
  76. else
  77. Error_system_version
  78. fi
  79. }
  80. #隐藏系统版本
  81. function Shadow_system_version(){
  82. echo '' > /etc/issue
  83. echo '' > /etc/motd
  84. echo '' > /etc/redhat-release
  85. echo '' > /etc/centos-release
  86. }
  87. #测试外网是否连通
  88. function Test_network(){
  89. ping -c1 www.baidu.com &>/dev/null
  90. if [ $? -eq 0 ]
  91. then
  92. return 0
  93. else
  94. return 1
  95. fi
  96. }
  97. #设置系统最大句柄数
  98. function Set_handler_Num(){
  99. limit_count=`cat /etc/security/limits.conf | grep "^\*[[:blank:]]\+\(soft\|hard\)[[:blank:]]\+\(nofile\|nproc\)[[:blank:]]\+" | wc -l`
  100. if [ "$limit_count" -eq 0 ]
  101. then
  102. cat >> /etc/security/limits.conf << EOF
  103. * soft nofile 102400
  104. * hard nofile 102400
  105. * soft nproc 40960
  106. * hard nproc 40960
  107. EOF
  108. ulimit -n 102400 #设置文件打开数,并马上生效,
  109. else
  110. echo "已经添加过limit限制!"
  111. fi
  112. }
  113. #优化tcp连接
  114. function Set_tcp_kernel_arguments(){
  115. kernel_args=/etc/sysctl.d/tcp_optimization.conf
  116. flag_1=`cat $kernel_args 2>/dev/null | grep tcp_flag | awk '{print $2}'`
  117. flag_2=`cat $kernel_args 2>/dev/null | grep tcp_flag | wc -l`
  118. if [ "$flag_2" -gt 1 ]
  119. then
  120. echo "系统错误,TCP重复的优化参数,请查看 $kernel_args 是否正确!"
  121. return 1
  122. fi
  123. if [ "$flag_1" == 1 ]
  124. then
  125. echo "TCP内核参数已经优化过了。"
  126. return 1
  127. fi
  128. echo "#tcp_flag 1" >>$kernel_args
  129. touch $kernel_args
  130. echo "net.ipv4.tcp_syncookies = 1" >> $kernel_args #开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击
  131. echo "net.ipv4.tcp_tw_recycle = 1" >> $kernel_args #表示开启TCP连接中TIME-WAIT sockets的快速回收
  132. echo "net.ipv4.tcp_tw_reuse = 1" >> $kernel_args #表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接
  133. echo "net.ipv4.tcp_fin_timeout = 5" >> $kernel_args ##指定孤儿连接在内核中生存的时间为5秒
  134. echo "net.ipv4.tcp_keepalive_time = 1200" >> $kernel_args #表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省>是2小时,改为20分钟
  135. echo "net.ipv4.ip_local_port_range = 10000 65000" >> $kernel_args #表示用于向外连接的端口范围
  136. echo "net.ipv4.tcp_max_syn_backlog = 8192" >> $kernel_args #表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数
  137. echo "net.ipv4.tcp_max_tw_buckets = 5000" >> $kernel_args #表示系统同时保持TIME_WAIT的最大数量,如果超过这个数字,TIME_WAIT将立刻被清除并打印警告信息。
  138. sysctl -p $kernel_args &>/dev/null
  139. if [ $? != 0 ]
  140. then
  141. echo '读取Tcp内核参数错误!'
  142. fi
  143. }
  144. #禁用ssh的DNS功能
  145. function Disabled_sshd_dns(){
  146. #[ `grep "^#UseDNS \(no\|yes\)" /etc/ssh/sshd_config | wc -l` -eq 0 ] && { echo '已禁用该配置,Do nothing!' ; return 1; }
  147. sed -ri 's@#UseDNS (no|yes)@UseDNS no@g' /etc/ssh/sshd_config
  148. sed -ri 's@GSSAPIAuthentication yes@GSSAPIAuthentication no@g' /etc/ssh/sshd_config
  149. if [ "$Get_host_version" == '6' ]
  150. then
  151. service sshd restart
  152. elif [ "$Get_host_version" == '7' ]
  153. then
  154. systemctl restart sshd
  155. else
  156. Error_system_version
  157. fi
  158. }
  159. #配置网卡名称为eth*
  160. function Modify_network_card_name(){
  161. if [ "$Get_host_version" == '6' ] #修改Centos6 的网卡
  162. then
  163. Count_cart=`cat /etc/udev/rules.d/70-persistent-net.rules | grep 'SUBSYSTEM=="net", ACTION=="add"' | wc -l`
  164. [ "$Count_cart" -eq 0 ] && { echo "没有网卡信息,请检查网卡驱动!" ; return 1; }
  165. count=1
  166. All_mac=`cat 70-persistent-net.rules | grep 'SUBSYSTEM=="net", ACTION=="add"' |grep -o "\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}"`
  167. for i in `$ALL_mac`
  168. do
  169. sed -ri 's@('$i'.*NAME=").*[[:digit:]]+"$@\1eth'$count'$"@' /etc/udev/rules.d/70-persistent-net.rules
  170. let count+=1
  171. done
  172. echo '修改网卡名成功,请查看配置!'
  173. echo "`cat /etc/udev/rules.d/70-persistent-net.rules | grep 'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="'`"
  174. elif [ "$Get_host_version" == '7' ] #修改Centos7 的网卡
  175. then
  176. boot_grub=/boot/grub2/grub.cfg
  177. grub_default_cfg=/etc/default/grub
  178. Name_count=`cat $boot_grub 2>/dev/null | grep "quiet[[:blank:]]\+net.ifnames" | wc -l`
  179. cp $grub_default_cfg ${grub_default_cfg}.`date +'%F_%H.%M'`
  180. [ $? -ne 0 ] && { echo "没有 $grub_default_cfg 这个文件" ; return 1; }
  181. if [ "$Name_count" -eq 0 ]
  182. then
  183. sed -ri 's/(GRUB_CMDLINE_LINUX=.*quiet)/\1 net.ifnames=0/g' $grub_default_cfg
  184. grub2-mkconfig -o $boot_grub
  185. if [ $? -eq 0 ]
  186. then
  187. echo '生成新的配置文件,生效需重启!'
  188. else
  189. echo "grub文件生成错误! $boot_grub 可能会产生错误!请检查"
  190. fi
  191. else
  192. echo '已经修改过grub参数,无需再次修改!Do nothing!'
  193. fi
  194. else
  195. Error_system_version
  196. fi
  197. }
  198. #配置yum仓库为aliyun
  199. function Modify_yumrepo(){
  200. repo_path=/etc/yum.repos.d/
  201. base_repo_count=`ls $repo_path | grep Alibase.repo | wc -l`
  202. epel_repo_count=`ls $repo_path | grep epel.repo | wc -l`
  203. mkdir -p ${repo_path}bak 2>/dev/null
  204. cd $repo_path
  205. Test_network
  206. [ $? -ne 0 ] && { echo '网络不通,退出函数!' ; return 1; }
  207. mv CentOS-* bak 2>/dev/null
  208. #根据系统版本添加源
  209. if [ "$Get_host_version" -eq 6 ]
  210. then
  211. if [ "$base_repo_count" -eq 0 ];then
  212. wget https://mirrors.aliyun.com/repo/Centos-6.repo -O ${repo_path}Alibase.repo
  213. else
  214. echo "已经添加过阿里源!"
  215. fi
  216. sleep 1
  217. if [ "$epel_repo_count" -ne 0 ];then
  218. wget https://mirrors.aliyun.com/repo/epel-6.repo -O ${repo_path}epel.repo
  219. else
  220. echo "已经添加过epel源!"
  221. fi
  222. yum clean all
  223. elif [ "$Get_host_version" -eq 7 ]
  224. then
  225. if [ "$base_repo_count" -eq 0 ];then
  226. wget https://mirrors.aliyun.com/repo/Centos-7.repo -O ${repo_path}Alibase.repo
  227. else
  228. echo "已经添加过阿里源!"
  229. fi
  230. sleep 1
  231. if [ "$epel_repo_count" -ne 0 ];then
  232. wget https://mirrors.aliyun.com/repo/epel-7.repo -O ${repo_path}epel.repo
  233. else
  234. echo "已经添加过epel源!"
  235. fi
  236. yum clean all
  237. else
  238. Error_system_version
  239. fi
  240. }
  241. #安装一些软件包
  242. function Install_some_packege(){
  243. packges="gcc glibc zlib openssl openssl-devel lrzsz lftp ftp telnet nmap-ncat net-snmp net-snmp-devel vim sysstat bash-completion wget lsof psmisc ntp"
  244. yum install -y $packges
  245. }
  246. #配置Bond
  247. function Config_Bond(){
  248. [ `ls /etc/sysconfig/network-scripts/ifcfg-Bond* 2>/dev/null | wc -l ` -ne 0 ] && { echo '已经配置了了Bond' ; return 1; }
  249. Net_card_name=`netstat -I | sed '1,2d' | sed '/lo/d' | awk '{print $1}'`
  250. Net_card_Num=`netstat -I | sed '1,2d' | sed '/lo/d' | awk '{print $1}' | wc -l`
  251. Named_eth_count=`echo $Net_card_name | grep -io eth | wc -l`
  252. [ "$Named_eth_count" -ne "$Net_card_Num" ] && { echo "网卡名并未变更为eth,或者已经添加过了聚合类型!配置失败!" ; return 1; }
  253. net_path=/etc/sysconfig/network-scripts/
  254. if [ "$Get_host_version" == '6' ]
  255. then
  256. service NetworkManager stop
  257. chkconfig NetworkManager off
  258. for i in $Net_card_name
  259. do
  260. cat >>${net_path}ifcfg-$i <<EOF
  261. DEVICE=$i
  262. BOOTPROTO=none
  263. MASTER=bond0
  264. SLAVE=yes
  265. USERCTL=no
  266. EOF
  267. done
  268. cat >>${net_path}ifcfg-Bond0 <<EOF
  269. DEVICE=bond0
  270. BOOTPROTO=none
  271. BONDING_OPTS="miimon=100 mode=0"
  272. DNS1=8.8.8.8
  273. IPADDR=172.18.30.2
  274. PREFIX=16
  275. GATEWAY=172.18.0.1
  276. ONBOOT=yes
  277. EOF
  278. service network restart
  279. elif [ "$Get_host_version" == '7' ]
  280. then
  281. nmcli con add type bond con-name Bond0 ifname Bond0 mode 0 ipv4.method manual ipv4.addresses 172.18.30.1 ipv4.gateway 172.18.0.1 ipv4.dns 8.8.8.8 &>/dev/null
  282. [ $? -eq 0 ] && nmcli con up Bond0
  283. for i in $Net_card_name
  284. do
  285. nmcli con add type bond-slave con-name $i-bond ifname $i master Bond0
  286. [ $? -eq 0 ] && nmcli con up $i-bond || echo "激活失败!"
  287. done
  288. else
  289. Error_system_version
  290. fi
  291. }
  292. #这里开始调用执行
  293. Bakup_etc #备份etc
  294. Off_firewall_and_selinux #关闭selinux
  295. Install_wget #安装wget
  296. Modify_charaset #修改全局字符集
  297. Set_timezone_and_time #设置时区和时间
  298. Set_handler_Num # 设置打开文件数
  299. Set_tcp_kernel_arguments #优化内核tcp连接
  300. Modify_yumrepo #修改yum仓库
  301. Install_some_packege #安装一些软件包
  302. Disabled_sshd_dns #禁用ssh的dns功能
  303. #Shadow_system_version #隐藏系统版本
  304. Modify_network_card_name #统一网卡名称为eth
  305. Config_Bond #配置Bond,默认ip为172.18.30.1,需要手动配置

centos6、7系统初始化脚本的更多相关文章

  1. centos6.6系统初始化脚本

    #!/bin/bash # Program: # system_init_shell # History: # 2012/06/01 25061008@qq.com # Release: # 1.1 ...

  2. centos7 系统初始化脚本

    现在自己的本地虚拟机系统,直接安装的是centos7.2 mini版,安装完成发现好多东西都没有安装,所以写了一个简单的系统初始化脚本,让自己可以省一些力气,哈哈 人懒主要是. 下面贴出写的脚本,脚本 ...

  3. Centos7系统初始化脚本

    前言: 因公司业务增加,陆续新增服务器,时不时的来几台,手动地一台台对服务器初始化操作感觉太麻烦. 于是乎,根据初始化需求整合了一个初始化脚本,实现批量脚本初始化操作. 说明: 本脚本根据自身需求编写 ...

  4. centos系统初始化脚本

    #!/bin/bash #检测是否为root用户 ];then echo "Must be root can do this." exit fi #检测网络 echo " ...

  5. 简单的 centos7&rhel7 系统初始化脚本

    #!/bin/bash #描述: 基于RHEL7&centos7的初始化配置 #读取用户输入的ip read -p "输入你当前Linux的IP地址:" LAST #截取网 ...

  6. LINUX 系统初始化脚本

    #!/bin/bash ######the system first start configuretion #####for install  ####copy right by donglei## ...

  7. Linux系统初始化脚本

    #查看centos的版本号 CentOS_version=`cut -d /etc/centos-release | cut -d` #改变PS3格式 PS3="Please enter t ...

  8. 给公司个别安装好的系统环境处理-相当half系统初始化脚本shell

    #!/bin/bash# Used for other system-environment update! echo -e '\n\033[35m~~请使用root权限运行此脚本~~\033[0m\ ...

  9. 分享我自己的一个最小化安装CentOS6的初始化脚本

    在自己的虚拟机上使用的基于CentOS6的系统初始化脚本 #!/bin/bash # #Filename:system_init.sh #Description:系统安装完成后,对系统进行一些配置,以 ...

随机推荐

  1. Android netty客户端入门

    新建项目,加入netty库 implementation 'io.netty:netty-all:4.1.36.Final'

  2. jquery设置input不可编辑,背景变灰,鼠标变禁止

    先看效果 $("#id").attr("onfocus", "this.blur()"); $("#id").css(& ...

  3. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar中的类解压后放在运行jar中

    前文: [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar打在jar包中 使用7z打开压缩包,查看所有依赖的jar都被解压以包名及class的方式存储在了运行jar中,此时jar的 ...

  4. XKC's basketball team【线段树查询】

    XKC , the captain of the basketball team , is directing a train of nn team members. He makes all mem ...

  5. UWP笔记-边框背景虚化效果

    这是一个简单的UI外观 1.添加Negut包: Microsoft.Toolkit.Uwp.UI.Controls 2.xaml:命名空间中引用 xmlns:controls="using: ...

  6. 一个基于Unix套接字的注册登录系统

    2016/5/5 今天,我参考<Unix网络编程-卷1>第5章的TCP回射客户/服务器程序写了一个简单的注册登录系统,其功能如下:(1)注册.客户端向服务器发送个人信息请求注册,服务器查询 ...

  7. 什么是Java多线程?

    第五阶段 多线程 前言: 一个场景:周末,带着并不存在的女票去看电影,无论是现场买票也好,又或是手机买票也好,上一秒还有位置,迟钝了一下以后,就显示该座位已经无法选中,一不留神就没有座位了,影院的票是 ...

  8. 什么是DataV数据可视化

    DataV数据可视化是使用可视化大屏的方式来分析并展示庞杂数据的产品.DataV旨让更多的人看到数据可视化的魅力,帮助非专业的工程师通过图形化的界面轻松搭建专业水准的可视化应用,满足您会议展览.业务监 ...

  9. Ural 1298 Knight 题解

    目录 Ural 1298 Knight 题解 题意 题解 程序 Ural 1298 Knight 题解 题意 给定一个\(n\times n(1\le n\le8)\)的国际象棋棋盘和一个骑士(基本上 ...

  10. 牛客 26C 手铐 (缩环, 树形dp)

    先缩环建树, 对于树上个环$x,y$, 假设$x,y$路径上有$cnt$个环(不包括$x,y$), 贡献就为$2^{cnt}$. 这题卡常挺严重的, 刚开始用并查集合并竟然T了. #include & ...