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 "未添加光盘源!退出脚本" exit 1 ; }
  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 "
  131. net.ipv4.tcp_syncookies = 1 #SYN等待队列溢出,启用cookie,可防少量ddos
  132. net.ipv4.tcp_tw_resue = 1 #重用TIME_WAIT套接字用于新的TCP链接
  133. net.ipv4.tcp_tw_recycle = 1 #启用TIME_WAIT套接字快速回收
  134. net.ipv4.tcp_keepalive_time = 1200 #tcp keepalive消息的频度,默认2小时
  135. net.ipv4.tcp_fin_timeout = 5 #指定孤儿连接在内核中生存的时间为5秒
  136. net.ipv4.ip_local_port_range = 10000 65000 #配置可用端口
  137. net.ipv4.tcp_max_syn_backlog = 262144 #表示SYN队列的长度
  138. net.ipv4.tcp_max_tw_buckets = 5000 #超过数量的TIME_WAIT将立刻被清除并打印警告信息
  139. net.core.netdev_max_backlog = 262144 #当网络接口接收数据包的速率比内核处理速率快时,允许送到缓冲队列的数据包最大数目。
  140. net.core.somaxconn = 40000 #用于存放已经建立好的TCP连接,等待服务端应用listener accept进行处理
  141. " >>$kernel_args
  142. sysctl -p $kernel_args &>/dev/null
  143. if [ $? != 0 ]
  144. then
  145. echo '读取Tcp内核参数错误!'
  146. fi
  147. }
  148. #优化swap
  149. function Set_kernel_swap(){
  150. kernel_args=/etc/sysctl.d/swap.conf
  151. echo "vm.swappiness = 0" >>$kernel_args
  152. sysctl -p $kernel_args &>/dev/null
  153. if [ $? != 0 ]
  154. then
  155. echo '读取Tcp内核参数错误!'
  156. fi
  157. }
  158. #禁用ssh的DNS功能
  159. function Disabled_sshd_dns(){
  160. #[ `grep "^#UseDNS \(no\|yes\)" /etc/ssh/sshd_config | wc -l` -eq 0 ] && { echo '已禁用该配置,Do nothing!' ; return 1; }
  161. sed -ri 's@#UseDNS (no|yes)@UseDNS no@g' /etc/ssh/sshd_config
  162. sed -ri 's@GSSAPIAuthentication yes@GSSAPIAuthentication no@g' /etc/ssh/sshd_config
  163. if [ "$Get_host_version" == '6' ]
  164. then
  165. service sshd restart
  166. elif [ "$Get_host_version" == '7' ]
  167. then
  168. systemctl restart sshd
  169. else
  170. Error_system_version
  171. fi
  172. }
  173. #配置网卡名称为eth*
  174. function Modify_network_card_name(){
  175. if [ "$Get_host_version" == '6' ] #修改Centos6 的网卡
  176. then
  177. Count_cart=`cat /etc/udev/rules.d/70-persistent-net.rules | grep 'SUBSYSTEM=="net", ACTION=="add"' | wc -l`
  178. [ "$Count_cart" -eq 0 ] && { echo "没有网卡信息,请检查网卡驱动!" ; return 1; }
  179. count=1
  180. 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\}"`
  181. for i in `$ALL_mac`
  182. do
  183. sed -ri 's@('$i'.*NAME=").*[[:digit:]]+"$@\1eth'$count'$"@' /etc/udev/rules.d/70-persistent-net.rules
  184. let count+=1
  185. done
  186. echo '修改网卡名成功,请查看配置!'
  187. echo "`cat /etc/udev/rules.d/70-persistent-net.rules | grep 'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="'`"
  188. elif [ "$Get_host_version" == '7' ] #修改Centos7 的网卡
  189. then
  190. boot_grub=/boot/grub2/grub.cfg
  191. grub_default_cfg=/etc/default/grub
  192. Name_count=`cat $boot_grub 2>/dev/null | grep "quiet[[:blank:]]\+net.ifnames" | wc -l`
  193. cp $grub_default_cfg ${grub_default_cfg}.`date +'%F_%H.%M'`
  194. [ $? -ne 0 ] && { echo "没有 $grub_default_cfg 这个文件" ; return 1; }
  195. if [ "$Name_count" -eq 0 ]
  196. then
  197. sed -ri 's/(GRUB_CMDLINE_LINUX=.*quiet)/\1 net.ifnames=0/g' $grub_default_cfg
  198. grub2-mkconfig -o $boot_grub
  199. if [ $? -eq 0 ]
  200. then
  201. echo '生成新的配置文件,生效需重启!'
  202. else
  203. echo "grub文件生成错误! $boot_grub 可能会产生错误!请检查"
  204. fi
  205. else
  206. echo '已经修改过grub参数,无需再次修改!Do nothing'
  207. fi
  208. else
  209. Error_system_version
  210. fi
  211. }
  212. #配置yum仓库为aliyun
  213. function Modify_yumrepo(){
  214. repo_path=/etc/yum.repos.d/
  215. base_repo_count=`ls $repo_path | grep Alibase.repo | wc -l`
  216. epel_repo_count=`ls $repo_path | grep epel.repo | wc -l`
  217. mkdir -p ${repo_path}bak 2>/dev/null
  218. cd $repo_path
  219. Test_network
  220. [ $? -ne 0 ] && { echo '网络不通,退出函数!' ; return 1; }
  221. mv CentOS-* bak 2>/dev/null
  222. #根据系统版本添加源
  223. if [ "$Get_host_version" -eq 6 ]
  224. then
  225. if [ "$base_repo_count" -eq 0 ];then
  226. wget https://mirrors.aliyun.com/repo/Centos-6.repo -O ${repo_path}Alibase.repo
  227. else
  228. echo "已经添加过阿里源!"
  229. fi
  230. sleep 1
  231. if [ "$epel_repo_count" -eq 0 ];then
  232. wget https://mirrors.aliyun.com/repo/epel-6.repo -O ${repo_path}epel.repo
  233. else
  234. echo "已经添加过epel源!"
  235. fi
  236. yum clean all
  237. elif [ "$Get_host_version" -eq 7 ]
  238. then
  239. if [ "$base_repo_count" -eq 0 ];then
  240. wget https://mirrors.aliyun.com/repo/Centos-7.repo -O ${repo_path}Alibase.repo
  241. else
  242. echo "已经添加过阿里源!"
  243. fi
  244. sleep 1
  245. if [ "$epel_repo_count" -eq 0 ];then
  246. wget https://mirrors.aliyun.com/repo/epel-7.repo -O ${repo_path}epel.repo
  247. else
  248. echo "已经添加过epel源!"
  249. fi
  250. yum clean all
  251. else
  252. Error_system_version
  253. fi
  254. }
  255. #安装一些软件包
  256. function Install_some_packege(){
  257. 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"
  258. yum install -y $packges
  259. }
  260. #配置Bond
  261. function Config_Bond(){
  262. [ `ls /etc/sysconfig/network-scripts/ifcfg-Bond* 2>/dev/null | wc -l ` -ne 0 ] && { echo '已经配置了了Bond' ; return 1; }
  263. Net_card_name=`netstat -I | sed '1,2d' | sed '/lo/d' | awk '{print $1}'`
  264. Net_card_Num=`netstat -I | sed '1,2d' | sed '/lo/d' | awk '{print $1}' | wc -l`
  265. Named_eth_count=`echo $Net_card_name | grep -io eth | wc -l`
  266. [ "$Named_eth_count" -ne "$Net_card_Num" ] && { echo "网卡名并未变更为eth,或者已经添加过了聚合类型!配置失败!" ; return 1; }
  267. net_path=/etc/sysconfig/network-scripts/
  268. if [ "$Get_host_version" == '6' ]
  269. then
  270. service NetworkManager stop
  271. chkconfig NetworkManager off
  272. for i in $Net_card_name
  273. do
  274. cat >>${net_path}ifcfg-$i <<EOF
  275. DEVICE=$i
  276. BOOTPROTO=none
  277. MASTER=bond0
  278. SLAVE=yes
  279. USERCTL=no
  280. EOF
  281. done
  282. cat >>${net_path}ifcfg-Bond0 <<EOF
  283. DEVICE=bond0
  284. BOOTPROTO=none
  285. BONDING_OPTS="miimon=100 mode=0"
  286. DNS1=8.8.8.8
  287. IPADDR=172.18.30.2
  288. PREFIX=16
  289. GATEWAY=172.18.0.1
  290. ONBOOT=yes
  291. EOF
  292. service network restart
  293. elif [ "$Get_host_version" == '7' ]
  294. then
  295. 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
  296. [ $? -eq 0 ] && nmcli con up Bond0
  297. for i in $Net_card_name
  298. do
  299. nmcli con add type bond-slave con-name $i-bond ifname $i master Bond0
  300. [ $? -eq 0 ] && nmcli con up $i-bond || echo "激活失败!"
  301. done
  302. else
  303. Error_system_version
  304. fi
  305. }
  306. #这里开始调用执行
  307. Bakup_etc #备份etc
  308. Off_firewall_and_selinux #关闭selinux
  309. Install_wget #安装wget
  310. Modify_charaset #修改全局字符集
  311. Set_timezone_and_time #设置时区和时间
  312. Set_handler_Num # 设置打开文件数
  313. Set_tcp_kernel_arguments #优化内核tcp连接
  314. Set_kernel_swap #优化swap
  315. Modify_yumrepo #修改yum仓库
  316. Install_some_packege #安装一些软件包
  317. Disabled_sshd_dns #禁用ssh的dns功能
  318. #Shadow_system_version #隐藏系统版本
  319. Modify_network_card_name #统一网卡名称为eth
  320. Config_Bond #配置Bond,默认ip为172.18.30.1,需要手动配置
文档更新时间: 2019-03-27 11:18   作者:张尚