Linux系統初始化*引數初始化(指令碼)

Michael_DD發表於2014-10-08
Linux系統初始化*引數初始化(指令碼)

1.  系統環境初始化

點選(此處)摺疊或開啟

  1. #!/bin/bash
  2. #configure yum source
  3. cat>/etc/yum.repos.d/rhel64.repo<<EOF
  4. [Server]
  5. name=Red Hat Enterprise Linux Server
  6. baseurl=ftp://192.168.8.125/yumfile/Server/
  7. enabled=1
  8. gpgcheck=0
  9. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  10. EOF

  11. yum clean all
  12. yum list|grep \"kde-i18n-Chinese.noarch\"
  13. echo \"yum source has been configured\"

  14. #install chinese rpm
  15. yum install -y kde-i18n-Chinese.noarch

  16. #disable selinux
  17. sed -i \'s/SELINUX=enforcing/SELINUX=disabled/g\' /etc/sysconfig/selinux
  18. grep SELINUX= /etc/sysconfig/selinux
  19. echo \"disable selinux success\"

  20. #set initdefault
  21. sed -i \'s/id:5:initdefault:/id:3:initdefault:/g\' /etc/inittab
  22. grep initdefault /etc/inittab
  23. echo \"initdefault set to 3\"


  24. #set ntp & get into crontab
  25. /usr/sbin/ntpdate 192.168.1.15
  26. hwclock -w
  27. echo \"0 1 * * * root /usr/sbin/ntpdate 172.26.1.15 && /sbin/hwclock -w\" >> cron.txt
  28. crontab cron.txt
  29. rm -f cron.txt
  30. date
  31. echo \"set ntp success\"

  32. #configure DNS & disable NetworkManager
  33. service NetworkManager stop
  34. chkconfig NetworkManager off
  35. echo \"nameserver 172.26.1.20\" >> /etc/resolv.conf
  36. sed -i \'s/#UseDNS yes/UseDNS no/g\' /etc/ssh/sshd_config
  37. sed -i \'s/#GSSAPIAuthentication no/GSSAPIAuthentication no/g\' /etc/ssh/sshd_config
  38. sed -i \'s/GSSAPIAuthentication yes/#GSSAPIAuthentication yes/g\' /etc/ssh/sshd_config
  39. service sshd restart
  40. grep GSSAPIAuthentication /etc/ssh/sshd_config; grep DNS /etc/ssh/sshd_config
  41. echo \"DNS configure successful,service network restarting...\"
  42. service network restart

  43. #add user mwuser
  44. cat>mwuser.sh<<EOF
  45. mwuser_pwd=mwuser
  46. logop_pwd=Sinolife2012
  47. groupadd -g 3000 mwgrp
  48. groupadd -g 3001 loggrp
  49. useradd -u 3000 -g mwgrp -G loggrp mwuser
  50. useradd -u 3001 -g loggrp logop
  51. (sleep 1 ; echo ${mwuser_pwd} ;sleep 1; echo ${mwuser_pwd})|passwd mwuser
  52. (sleep 1 ; echo ${logop_pwd} ;sleep 1; echo ${logop_pwd})|passwd logop
  53. if [ ! -d \"/mwbase\" ] ;then
  54.   mkdir /mwbase
  55. fi

  56. if [ ! -d \"/mwbase/applogs/rtlog\" ];then
  57.   mkdir -p /mwbase/applogs/rtlog
  58. fi

  59. if [ ! -d \"/mwbase/applogs/archlog\" ];then
  60.   mkdir -p /mwbase/applogs/archlog
  61. fi

  62. chown -R mwuser:mwgrp /mwbase
  63. chmod -R 755 /mwbase
  64. chown -R mwuser:loggrp /mwbase/applogs
  65. chmod -R 2750 /mwbase/applogs
  66. EOF
  67. sh mwuser.sh
  68. echo \"user mwuser established\"

  69. #chkconfig list
  70. #chkconfig off
  71. chkconfig NetworkManager --levels 2345 off
  72. chkconfig abrt-ccpp --levels 2345 off
  73. chkconfig abrtd --levels 2345 off
  74. chkconfig acpid --levels 2345 off
  75. chkconfig certmonger --levels 2345 off
  76. chkconfig cgconfig --levels 2345 off
  77. chkconfig cgred --levels 2345 off
  78. chkconfig cpuspeed --levels 2345 off
  79. chkconfig cups --levels 2345 off
  80. chkconfig dnsmasq --levels 2345 off
  81. chkconfig firstboot --levels 2345 off
  82. chkconfig ip6tables --levels 2345 off
  83. chkconfig iptables --levels 2345 off
  84. chkconfig kdump --levels 2345 off
  85. chkconfig mdmonitor --levels 2345 off
  86. chkconfig netconsole --levels 2345 off
  87. chkconfig netfs --levels 2345 off
  88. chkconfig nfs --levels 2345 off
  89. chkconfig nfslock --levels 2345 off
  90. chkconfig ntpd --levels 2345 off
  91. chkconfig ntpdate --levels 2345 off
  92. chkconfig numad --levels 2345 off
  93. chkconfig oddjobd --levels 2345 off
  94. chkconfig portreserve --levels 2345 off
  95. chkconfig postfix --levels 2345 off
  96. chkconfig quota_nld --levels 2345 off
  97. chkconfig rdisc --levels 2345 off
  98. chkconfig restorecond --levels 2345 off
  99. chkconfig rhnsd --levels 2345 off
  100. chkconfig rhsmcertd --levels 2345 off
  101. chkconfig rngd --levels 2345 off
  102. chkconfig rpcgssd --levels 2345 off
  103. chkconfig rpcidmapd --levels 2345 off
  104. chkconfig rpcsvcgssd --levels 2345 off
  105. chkconfig saslauthd --levels 2345 off
  106. chkconfig smartd --levels 2345 off
  107. chkconfig sssd --levels 2345 off
  108. chkconfig wdaemon --levels 2345 off
  109. chkconfig winbind --levels 2345 off
  110. chkconfig wpa_supplicant --levels 2345 off
  111. chkconfig ypbind --levels 2345 off

  112. #chkconfig on
  113. chkconfig --levels 2345 atd on
  114. chkconfig --levels 2345 auditd on
  115. chkconfig --levels 2345 crond on
  116. chkconfig --levels 2345 psacct on
  117. chkconfig --levels 2345 rsyslog on
  118. chkconfig --levels 2345 sshd on
  119. chkconfig --levels 2345 network on

2.  系統引數初始化

點選(此處)摺疊或開啟

  1. #!/bin/sh
  2. #set -x
  3. #修改sysctl-conf檔案
  4. function edit_sysctl_conf()
  5. {
  6.   param_name=$1
  7.   param_value=$2
  8.   conf_file=\"/etc/sysctl.conf\"
  9.   value_count=$(echo ${param_value}|awk \'{print NF}\')
  10.   if [ \"${value_count}\" -eq \"1\" ] ; then
  11.     old_line=$(grep \"^${param_name}[[:space:]]*=[[:space:]]*[0-9]\\+\" ${conf_file})
  12.     if [ -n \"${old_line}\" ] ; then
  13.        file_value=$(echo ${old_line} |sed \'s/ //g\'| cut -d= -f2)
  14.        if [ ${param_value} -gt $file_value ] ; then
  15.           new_line=\"${param_name} = ${param_value}\"
  16.           sed -ie \"s/^${param_name}\\(.*\\)$/${new_line}/g\" ${conf_file}
  17.           sed -i \"/^${param_name}/i #${old_line}\" ${conf_file}
  18.        fi
  19.      else
  20.        echo \"\" >> ${conf_file}
  21.        echo \"${param_name} = ${param_value}\" >> ${conf_file}
  22.      fi
  23.      cur_value=$( /sbin/sysctl -n ${param_name} |sed \'s/ //g\')
  24.      if [ ${param_value} -gt ${cur_value} ] ; then
  25.          if ! /sbin/sysctl -w ${param_name}=\"${param_value}\" ; then
  26.             echo \"/sbin/sysctl failed to set ${param_name} = ${param_value}\"
  27.          fi
  28.      fi
  29.   fi
  30.   if [ \"${value_count}\" -gt \"1\" ] ; then
  31.       declare -a cur_values
  32.       declare -a file_values
  33.       declare -a new_values
  34.      local cur_value
  35.      local file_value
  36.      old_line=$(sed -ne \"/^${param_name}/p\" ${conf_file})
  37.      line_value=$(echo ${old_line}| cut -d= -f2)
  38.      for (( i=0;i<${value_count};i++ )) ;do
  39.         cur_values[$i]=$(/sbin/sysctl -n \"${param_name}\"| awk \"{print \\$(($i+1))}\")
  40.         new_values[$i]=$( echo $param_value|awk \"{print \\$(($i+1))}\" )
  41.      done
  42.      if [ ! -z \"${line_value}\" ] ; then
  43.        for (( i=0;i<${value_count};i++ )) ;do
  44.         file_values[$i]=$( echo $line_value| awk \"{print \\$(($i+1))}\")
  45.        done
  46.      fi
  47.      for (( i=0;i<${value_count};i++ )) ;do
  48.         if [ ${new_values[$i]} -gt ${cur_values[$i]} ] ; then
  49.           cur_values[$i]=${new_values[$i]}
  50.           flag_cur=\"true\"
  51.         fi
  52.         if test -z \"$line_value\" || test ${new_values[$i]} -gt ${file_values[$i]} ; then
  53.           file_values[$i]=${new_values[$i]}
  54.           flag_file=\"true\"
  55.         fi
  56.      done
  57.      if [ \"$flag_cur\" == \"true\" ] ; then
  58.         for (( i=0;i<${value_count};i++ )) ;do
  59.            cur_value=\"${cur_value}${cur_values[$i]} \"
  60.         done
  61.         if ! /sbin/sysctl -w ${param_name}=\"${cur_value}\" ; then
  62.            echo \"/sbin/sysctl failed to set ${param_name} \"
  63.         fi
  64.      fi
  65.      if [ \"$flag_file\" == \"true\" ] ; then
  66.         for (( i=0;i<${value_count};i++ )) ;do
  67.            file_value=\"${file_value}${file_values[$i]} \"
  68.         done
  69.         new_line=\"${param_name} = ${file_value}\"
  70.         if [ -z \"${old_line}\" ] ; then
  71.           echo \"\" >> ${conf_file}
  72.           echo $new_line >> ${conf_file}
  73.         else
  74.           sed -ie \"s/^${param_name}\\(.*\\)$/${new_line}/g\" ${conf_file}
  75.           sed -i \"/^${param_name}/i #${old_line}\" ${conf_file}
  76.         fi
  77.      fi
  78.   fi
  79. }

  80. #修改limits_conf檔案
  81. function edit_limits_conf()
  82. {
  83.   limit_domain=$1
  84.   limit_type=$2
  85.   limit_item=$3
  86.   limit_value=$4
  87.   conf_file=\"/etc/security/limits.conf\"
  88.   old_line=$( grep \"^${limit_domain}[[:space:]]\\+${limit_type}[[:space:]]\\+${limit_item}[[:space:]]\\+\" ${conf_file})
  89.   if [ -n \"${old_line}\" ] ; then
  90.          file_value=$( echo ${old_line}| awk \'{print $4}\')
  91.          if [ \"${file_value}\" != \"unlimited\" ]; then
  92.             if [ $limit_value -gt ${file_value} ] ; then
  93.                new_line=\"${limit_domain} ${limit_type} ${limit_item} ${limit_value}\"
  94.                sed -ie \"s/^${limit_domain}[[:space:]]\\+${limit_type}[[:space:]]\\+${limit_item}[[:space:]]\\(.*\\)$/${new_line}/g\" ${conf_file}
  95.                sed -i \"/^${limit_domain}[[:space:]]\\+${limit_type}[[:space:]]\\+${limit_item}[[:space:]]/i #${old_line}\" ${conf_file}
  96.              fi
  97.          fi
  98.    else
  99.         new_line=\"${limit_domain} ${limit_type} ${limit_item} ${limit_value}\"
  100.         sed -ie \"/^# End of file/i ${new_line}\" ${conf_file}
  101.   fi
  102.   
  103. }

  104. #修改muser_coredump檔案
  105. function enable_muser_coredump()
  106. {
  107.  profile=\"/etc/profile.d/mwuser-profile.sh\"
  108.  if [ ! -e \"$profile\" ]; then
  109.    echo \"if [ \\\"\\$LOGNAME\\\" = \\\"mwuser\\\" ] ; then \" >> ${profile}
  110.    echo \" ulimit -S -c unlimited > /dev/null 2>&1 \" >> ${profile}
  111.    echo \"fi\" >> ${profile}
  112.  fi
  113. }

  114. #edit_sysctl_conf \"kernel.shmmax\" \"68719476738\"
  115. #edit_sysctl_conf \"kernel.shmmni\" \"4096\"
  116. #edit_sysctl_conf \"kernel.shmall\" \"2097152\"
  117. #edit_sysctl_conf \"kernel.sem\" \"250 32000 100 128\"
  118. edit_sysctl_conf \"net.core.rmem_default\" \"16777216\"
  119. edit_sysctl_conf \"net.core.wmem_default\" \"16777216\"
  120. edit_sysctl_conf \"net.core.rmem_max\" \"16777216\"
  121. edit_sysctl_conf \"net.core.wmem_max\" \"16777216\"
  122. edit_sysctl_conf \"net.ipv4.tcp_rmem\" \"4096 87380 16777216\"
  123. edit_sysctl_conf \"net.ipv4.tcp_wmem\" \"4096 65536 16777216\"
  124. edit_sysctl_conf \"net.ipv4.ip_local_port_range\" \"50000 65500\"
  125. edit_sysctl_conf \"net.ipv4.tcp_fin_timeout\" \"30\"
  126. edit_sysctl_conf \"fs.file-max\" \"6815744\"
  127. #edit_sysctl_conf \"fs.aio-max-size\" \"\"
  128. #edit_sysctl_conf \"fs.aio-max-nr\" \"1048576\"


  129. edit_limits_conf \"mwuser\" \"soft\" \"nproc\" \"2047\"
  130. edit_limits_conf \"mwuser\" \"hard\" \"nproc\" \"16384\"
  131. edit_limits_conf \"mwuser\" \"hard\" \"nofile\" \"16384\"
  132. edit_limits_conf \"mwuser\" \"soft\" \"nofile\" \"8192\"
  133. edit_limits_conf \"mwuser\" \"hard\" \"core\" \"unlimited\"

  134. enable_muser_coredump


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29500582/viewspace-1291948/,如需轉載,請註明出處,否則將追究法律責任。

相關文章