CentOS7最小化安装后初始化脚本

布满荆棘的人生 2022-05-20 02:38 318阅读 0赞

CentOS7最小化安装后初始化脚本

  1. #!/bin/bash
  2. #################################################
  3. # --Info
  4. # Initialization CentOS 7.x script
  5. #################################################
  6. # Auther: shaonbean@qq.com
  7. # Changelog:
  8. # 20180710 wanghui initial create
  9. #################################################
  10. # Check if user is root
  11. #
  12. if [ $(id -u) != "0" ]; then
  13. echo "Error: You must be root to run this script, please use root to initialization OS."
  14. exit 1
  15. fi
  16. echo "+------------------------------------------------------------------------+"
  17. echo "| To initialization the system for security and performance |"
  18. echo "+------------------------------------------------------------------------+"
  19. # add yunwei user
  20. user_add()
  21. {
  22. # add yunwei for jumpserver
  23. id -u yunwei
  24. if [ $? -eq 0 ];then
  25. useradd -s /bin/bash -d /home/yunwei -m yunwei && echo password | passwd --stdin yunwei && echo "yunwei ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/yunwei
  26. else
  27. echo "yunwei user is exist."
  28. fi
  29. }
  30. # update system & install pakeage
  31. system_update(){
  32. echo "*** Starting update system && install tools pakeage... ***"
  33. yum install epel-release -y && yum -y update
  34. yum clean all && yum makecache
  35. yum -y install rsync wget vim openssh-clients iftop htop iotop sysstat lsof telnet traceroute tree man iptraf lrzsz lynx net-tools dstat tree ntpdate dos2unix net-tools git
  36. [ $? -eq 0 ] && echo "System upgrade && install pakeages complete."
  37. }
  38. # Set timezone synchronization
  39. timezone_config()
  40. {
  41. echo "Setting timezone..."
  42. /usr/bin/timedatectl | grep "Asia/Shanghai"
  43. if [ $? -eq 0 ];then
  44. echo "System timezone is Asia/Shanghai."
  45. else
  46. timedatectl set-local-rtc 0 && timedatectl set-timezone Asia/Shanghai
  47. fi
  48. # config chrony
  49. yum -y install chrony && systemctl start chronyd.service && systemctl enable chronyd.service
  50. sed -i '$a 192.168.0.205 time.aniu.so' /etc/hosts
  51. sed -i 's/server 0.centos.pool.ntp.org iburst/server time.aniu.so iburst/g' /etc/chrony.conf
  52. [ $? -eq 0 ] && echo "Setting timezone && Sync network time complete."
  53. }
  54. # disable selinux
  55. selinux_config()
  56. {
  57. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  58. setenforce 0
  59. echo "Dsiable selinux complete."
  60. }
  61. # ulimit comfig
  62. ulimit_config()
  63. {
  64. echo "Starting config ulimit..."
  65. cat >> /etc/security/limits.conf <<EOF
  66. * soft nproc 8192
  67. * hard nproc 8192
  68. * soft nofile 8192
  69. * hard nofile 8192
  70. EOF
  71. [ $? -eq 0 ] && echo "Ulimit config complete!"
  72. }
  73. # sshd config
  74. sshd_config(){
  75. echo "Starting config sshd..."
  76. #sed -i '/^#Port/s/#Port 22/Port 21212/g' /etc/ssh/sshd_config
  77. sed -i "$ a\ListenAddress 0.0.0.0:21212\nListenAddress 0.0.0.0:22 " /etc/ssh/sshd_config
  78. sed -i '/^#UseDNS/s/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
  79. systemctl restart sshd
  80. #sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
  81. #sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
  82. [ $? -eq 0 ] && echo "SSH config complete."
  83. }
  84. # firewalld config
  85. disable_firewalld(){
  86. echo "Starting disable firewalld..."
  87. rpm -qa | grep firewalld >> /dec/null
  88. if [ $? -eq 0 ];then
  89. systemctl stop firewalld && systemctl disable firewalld
  90. [ $? -eq 0 ] && echo "Dsiable firewalld complete."
  91. else
  92. echo "Firewalld not install."
  93. fi
  94. }
  95. # vim config
  96. vim_config() {
  97. echo "Starting vim config..."
  98. /usr/bin/egrep pastetoggle /etc/vimrc >> /dev/null
  99. if [ $? -eq 0 ];then
  100. echo "vim already config"
  101. else
  102. sed -i '$ a\set bg=dark\nset pastetoggle=<F9>' /etc/vimrc
  103. fi
  104. }
  105. # sysctl config
  106. config_sysctl() {
  107. echo "Staring config sysctl..."
  108. /usr/bin/cp -f /etc/sysctl.conf /etc/sysctl.conf.bak
  109. cat > /etc/sysctl.conf << EOF
  110. vm.swappiness = 0
  111. vm.dirty_ratio = 80
  112. vm.dirty_background_ratio = 5
  113. fs.file-max = 2097152
  114. fs.suid_dumpable = 0
  115. net.core.somaxconn = 65535
  116. net.core.netdev_max_backlog = 262144
  117. net.core.optmem_max = 25165824
  118. net.core.rmem_default = 31457280
  119. net.core.rmem_max = 67108864
  120. net.core.wmem_default = 31457280
  121. net.ipv4.tcp_syncookies = 1
  122. net.ipv4.conf.all.rp_filter = 1
  123. net.ipv4.icmp_echo_ignore_all = 1
  124. net.ipv4.icmp_echo_ignore_broadcasts = 1
  125. net.ipv4.conf.all.log_martians = 1
  126. net.ipv4.conf.all.accept_source_route = 0
  127. net.ipv4.conf.all.accept_redirects = 0
  128. EOF
  129. # eg:https://www.vultr.com/docs/securing-and-hardening-the-centos-7-kernel-with-sysctl
  130. # set kernel parameters work
  131. /usr/sbin/sysctl -p
  132. [ $? -eq 0 ] && echo "Sysctl config complete."
  133. }
  134. # ipv6 config
  135. disable_ipv6() {
  136. echo "Starting disable ipv6..."
  137. sed -i '$ a\net.ipv6.conf.all.disable_ipv6 = 1\nnet.ipv6.conf.default.disable_ipv6 = 1' /etc/sysctl.conf
  138. sed -i '$ a\AddressFamily inet' /etc/ssh/sshd_config
  139. systemctl restart sshd
  140. /usr/sbin/sysctl -p
  141. }
  142. # password config
  143. password_config() {
  144. # /etc/login.defs
  145. sed -i 's/PASS_MIN_LEN 5/PASS_MIN_LEN 8/g' /etc/login.defs
  146. authconfig --passminlen=8 --update
  147. authconfig --enablereqlower --update
  148. [ $? -eq 0 ] && echo "Config password rule complete."
  149. }
  150. # disable no use service
  151. disable_serivces() {
  152. systemctl stop postfix && systemctl enable postfix
  153. [ $? -eq 0 ] && echo "Disable postfix service complete."
  154. }
  155. #main function
  156. main(){
  157. user_add
  158. system_update
  159. timezone_config
  160. selinux_config
  161. ulimit_config
  162. sshd_config
  163. disable_firewalld
  164. vim_config
  165. config_sysctl
  166. disable_ipv6
  167. password_config
  168. disable_serivces
  169. }
  170. # execute main functions
  171. main
  172. echo "+------------------------------------------------------------------------+"
  173. echo "| To initialization system all completed !!! |"
  174. echo "+------------------------------------------------------------------------+"

发表评论

表情:
评论列表 (有 0 条评论,318人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vmware下CentOS7安装

    Linux的学习使用什么版本比较合适?个人认为是从命令行开始是最好的,因为以后操作服务器(做系统运维)都是使用命令行进行远程控制,虽然命令行模式最开始接触会有一定难度和不适应,