[Centos系统] Centos 7 操作系统的优化项

22 0
Honkers 昨天 16:19 来自手机 | 显示全部楼层 |阅读模式

第1章 优化的第一阶段

以下列出的是一些优化项,并不是说得按照这个顺序来一项一项的优化,你得根据你的场景、你的需求以及你对当前操作系统的梳理。并且有些优化项不是千遍一律的。


01 更改yum源

  1. ### 更改base源为阿里云的源
  2. curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  3. ### 更改epel源
  4. yum install -y epel-release
复制代码


02 安装常用工具

  1. yum install -y \
  2. tree telnet lrzsz wget ntpdate vim nc namp dos2unix tcpdump pstree expect sshpass elinks unzip psmisc \
  3. lsof net-tools htop iproute bridge-utils \
  4. bind-utils nscd \
  5. gcc gcc-c++ make cmake libaio zlib-devel pcre-devel \
  6. psmisclsof sysstat yum-utils
复制代码


03 清空系统版本显示

  1. >/etc/issue
  2. >/etc/issue.net
复制代码


04 关闭selinux

  1. sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
  2. setenforce 0
复制代码


05 关闭firewalld防火墙

  1. systemctl stop firewalld.service # 临时停止firewalld服务
  2. systemctl disable firewalld.service # 不让其开机自启动
  3. systemctl mask firewalld.service # 不让其启动和设置开机自启动
复制代码


06 让用户密码永不过期

  1. cat >>/etc/login.defs<<EOF
  2. PASS_MAX_DAYS 99999
  3. PASS_MIN_DAYS 0
  4. PASS_MIN_LEN 5
  5. PASS_WARN_AGE 7
  6. EOF
复制代码


07 命令行及命令行文件对历史操作的记录

  1. echo "export HISTSIZE=10" >>/etc/bashrc # 用history只能看到最近操作的10条命令记录
  2. echo "export HISTFILESIZE=10" >>/etc/bashrc # 历史文件中只保留最近命令行操作的10条命令记录
  3. source /etc/bashrc
复制代码


08 不记录命令行以空格开头的操作记录

  1. echo "HISTCONTROL=ignorespace" >>/etc/bashrc
  2. source /etc/bashrc
复制代码


09 给危险命令rm做别名

  1. echo "alias rm='echo Do not use the rm command'" >>/etc/bashrc
  2. source /etc/bashrc
复制代码


10 设置支持中文字符集

  1. echo "LANG="zh_CN.UTF-8"" >/etc/locale.conf
复制代码


11 更改/etc/rc.d/rc.local文件权限744

  1. chmod 744 /etc/rc.d/rc.local
复制代码


12 校准和更新操作系统的时间

初次更新和校准系统时间

  1. ## 创建/etc/sysconfig/clock文件
  2. cat >>/etc/sysconfig/clock <<EOF
  3. ZONE="Asia/Shanghai"
  4. UTC=false
  5. ARC=false
  6. EOF
  7. ## 强制让其与/etc/localtime文件进行软链接
  8. ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  9. ## 让其与阿里云的时间服务器进行同步一次
  10. ntpdate ntp1.aliyun.com
  11. ## 设置硬件时间和系统时间一致并校准
  12. /sbin/hwclock --systohc
  13. hwclock --show
复制代码

系统定时更新系统时间

定时更新Linux操作系统的系统时间


13 调整swap交换页面

  1. ## 优化的命令
  2. chattr -i /etc/sysctl.conf
  3. echo "vm.swappiness=10" >>/etc/sysctl.conf
  4. sysctl -p
  5. # 我这里是让其当物理内存使用到90%时,才使用swap交换分区
  6. # 其实当服务器的物理内存用到80%的时候就要进行报警了;
  7. ## 对应的文件(还没有执行上面的命令哈)和说明
  8. [root@node31 ~]# cat /proc/sys/vm/swappiness
  9. 30
  10. # 不同的操作系统这个值是不一样的哈,oracle linux是60,我这里是CentOS linux;
  11. # 30的意思是:当服务器的物理内存被用到100%-30%=70%时,就让其使用swap交换页面(分区)了
  12. # 如果设置为0,则表示不使用swap交换页面(分区)
复制代码

14 防止Cannot allocate memory(无法分配内存)

值为不超过总内存的1%即可,我这里设置的是512M,min_free_kbytes表示强制 Linux 系统最低保留的空闲内存(Kbytes),如果系统可用内存低于设定的 min_free_kbytes 值,则默认系统启动 oom-killer 或强制重启。具体行为由内核参数 vm.panic_on_oom 值决定:
若 vm.panic_on_oom=0(默认),则系统会提示 OOM,并启动 oom-killer 杀掉占用最高内存的进程。
若 vm.panic_on_oom =1,则系统会自动重启。

  1. chattr -i /etc/sysctl.conf
  2. echo "vm.min_free_kbytes=524288" >>/etc/sysctl.conf
  3. sysctl -p
复制代码


14 调整limit限制

  1. ## 这是修改全局下
  2. cat >>/etc/security/limits.conf<<EOF
  3. #### memlock(max locked memory)
  4. #### cpu(cpu time)
  5. * soft memlock unlimited
  6. * hard memlock unlimited
  7. * soft cpu unlimited
  8. * hard cpu unlimited
  9. ### open files(nproc\nofile)
  10. * soft nproc 102431
  11. * hard nproc 102431
  12. * soft nofile 102431
  13. * hard nofile 102431
  14. ####
  15. * soft stack 65536
  16. * hard stack 65536
  17. ####
  18. * soft core unlimited
  19. * hard core unlimited
  20. EOF
  21. ## 调整可以 运行的最大并发进程数
  22. echo " * - nproc unlimited" >/etc/security/limits.d/20-nproc.conf
  23. echo "session required pam_limits.so" >>/etc/pam.d/login
  24. ## 调整sshd服务,当我们用ssh客户端工具连接后,才会生效
  25. echo "UsePAM yes" >>/etc/ssh/sshd_config
  26. echo "UseLogin yes" >>/etc/ssh/sshd_config
  27. systemctl restart sshd
复制代码


15.创建普通用户,让其可以su到超级用户

  1. chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
  2. useradd chenliang -G wheel
  3. echo "chenliang123456"|passwd --stdin chenliang
  4. chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow
复制代码


17.ssh服务优化

  1. ## 更改firewalld防火墙的ssh服务的端口为921
  2. sed -i 's#22#921#g' /usr/lib/firewalld/services/ssh.xml
  3. ## ssh服务的优化如下
  4. cat >>/etc/ssh/sshd_config<<EOF
  5. Port 921
  6. PermitRootLogin no
  7. PermitEmptyPasswords no
  8. UseDNS no
  9. GSSAPIAuthentication no
  10. EOF
  11. ## 重启sshd服务
  12. systemctl restart sshd.service
复制代码


二、脚本

脚本名称:Centos7_opt_scrip.sh

  1. #!/bin/bash
  2. #
  3. # ***** For newly installed systems
  4. # ***** Have access to the Internet
  5. # ***** Root user execution
  6. ########## Define variables
  7. RETVAL=0
  8. Baidu_url="www.baidu.com"
  9. Yum_soure="http://mirrors.aliyun.com/repo/Centos-7.repo"
  10. ## According to operating system version
  11. Common_tools="tree telnet lrzsz wget ntp ntpdate vim net-tools \
  12. lsof nc namp dos2unix tcpdump gcc gcc-c++ make \
  13. cmake libaio zlib-devel pcre-devel psmisclsof \
  14. sysstat yum-utils"
  15. Change_ssh_port="921"
  16. Firewalld_ssh_file="/usr/lib/firewalld/services/ssh.xml"
  17. ########## Determine the user to execute
  18. if [ "$UID" -ne $RETVAL ];then
  19. echo "Must be root to run scripts"
  20. exit 1
  21. fi
  22. ########## Load local functions
  23. [ -f /etc/init.d/functions ] && source /etc/init.d/functions
  24. ########## Check Internet access
  25. ping -c 2 $Baidu_url >/dev/null 2>&1
  26. RETVAL=$?
  27. if [ $RETVAL -eq 0 ];then
  28. action "Check internet access" /bin/true
  29. else
  30. action "Check internet access" /bin/false
  31. exit 1
  32. fi
  33. ########## Change domestic yum sources
  34. curl -o /etc/yum.repos.d/CentOS-Base.repo $Yum_soure >/dev/null 2>&1
  35. RETVAL=$?
  36. if [ $RETVAL -eq 0 ];then
  37. action "Change yum sources" /bin/true
  38. else
  39. action "Change yum sources" /bin/false
  40. fi
  41. ########## Install common toolkits
  42. yum install -y $Common_tools >/dev/null 2>&1
  43. RETVAL=$?
  44. if [ $RETVAL -eq 0 ];then
  45. action "Install common toolkits" /bin/true
  46. else
  47. action "Install common toolkits" /bin/false
  48. fi
  49. ########## Empty version display
  50. if [ -f /etc/issue -a /etc/issue.net ];then
  51. >/etc/issue && >/etc/issue.net
  52. RETVAL=$?
  53. if [ $RETVAL -eq 0 ];then
  54. action "Emty version display" /bin/true
  55. fi
  56. else
  57. echo "/etc/issue or /etc/issue.net is not exists"
  58. fi
  59. ########## Disable selinux
  60. if [ -f /etc/selinux/config ];then
  61. sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config &&
  62. setenforce 0
  63. RETVAL=$?
  64. if [ $RETVAL -eq 0 ];then
  65. action "Disable selinux" /bin/true
  66. else
  67. action "Disable selinux" /bin/false
  68. fi
  69. fi
  70. ########## User password does not expire
  71. if [ -f /etc/login.defs ];then
  72. echo -e "\nPASS_MAX_DAYS 99999\nPASS_MIN_DAYS 0\nPASS_MIN_LEN 5\nPASS_WARN_AGE 7\n" >>/etc/login.defs
  73. RETVAL=$?
  74. if [ $RETVAL -eq 0 ];then
  75. action "Set user password not expire" /bin/true
  76. else
  77. action "Set user password not expire" /bin/false
  78. fi
  79. fi
  80. ########## Command line history sav change
  81. echo -e "\nexport HISTSIZE=10\nexport HISTFILESIZE=10\nexport HISTCONTROL=ignorespace" >>/etc/bashrc
  82. RETVAL=$?
  83. if [ $RETVAL -eq 0 ];then
  84. action "Command line history sav change" /bin/true
  85. else
  86. action "Command line history sav change" /bin/false
  87. fi
  88. ########## rm command alias set
  89. echo "alias rm='echo Do not use the rm command'" >>/etc/bashrc
  90. RETVAL=$?
  91. if [ $RETVAL -eq 0 ];then
  92. action "Command rm alias set" /bin/true
  93. else
  94. action "Command rm alias set" /bin/false
  95. fi
  96. ######## Time proofread and first update
  97. echo -e "ZONE="Asia/Shanghai"\nUTC=false\nARC=false" >/etc/sysconfig/clock &&
  98. ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
  99. ntpdate ntp1.aliyun.com >/dev/null 2>&1 &&
  100. /sbin/hwclock --systohc
  101. RETVAL=$?
  102. if [ $RETVAL -eq 0 ];then
  103. action "Time proofread and first update" /bin/true
  104. else
  105. action "Time proofread and first update" /bin/false
  106. fi
  107. ######### /etc/rc.d/rc.local file permission change
  108. chmod 744 /etc/rc.d/rc.local
  109. RETVAL=$?
  110. if [ $RETVAL -eq 0 ];then
  111. action "/etc/rc.d/rc.local file permission change" /bin/true
  112. else
  113. action "/etc/rc.d/rc.local file permission change" /bin/false
  114. fi
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行