shell脚本:一键优化centos系统

简介:

自己参考了系统优化的点,写了一个一键优化脚本,适用于centos6.x版本,各个项已经单独进行测试通过。适用于centos6.x系统最小化安装执行,可以根据自己的需求进行添加或修改完善。


主要优化内容有:

关闭系统不必要的服务;关闭selinux,关闭iptables;关闭ctrl+alt+del重启;设置ssh端口,关闭DNS解析;设置系统最大文件描述符;设置系统关键文件权限;配置安装ntp;安装vim;配置安装阿里云yum源和epel源;


脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
[root@localhost ~] # cat youhua.sh
#!/bin/bash
#written by mofansheng@2015-11-03
#system optimization script
#The fllow apply to CentOS 6.x
/etc/init .d /functions
 
function  check_ok(){
   if  [ $? - eq  0 ]
    then
      echo  ""
      continue
   else
      echo  "pls check error"
      exit
   fi
}
 
cat <<EOF
-----------------------------------------------------------------------
|                     system optimization                             |
-----------------------------------------------------------------------
EOF
 
#close unimportant system services
echo  "===Close unimportant system services,it will take serval mintinues==="
for  in  `chkconfig --list| grep  3:on| awk  '{print $1}' | grep  -Ev  "crond|sshd|sysstat|rsyslog|network" `
do
   chkconfig $s off
done
check_ok
action  "Close unimportant system services"  /bin/true
 
 
#close selinux
echo  "===close SELINUX==="
if  [ `getenforce` !=  "Disabled"  ]
then
  sed  -i  's/SELINUX=enforcing/SELINUX=disabled/g'  /etc/selinux/config
  echo  "selinux is disabled,you must reboot!"
else
  action  "SELINUX is closed"  /bin/true
fi
check_ok
action  "Close SELINUX"  /bin/true
 
 
#close ctrl+alt+del
mv  /etc/init/control-alt-delete .conf  /etc/init/control-alt-delete .conf.bak
 
 
#close iptables
echo  "===close iptables==="
iptables-save > /etc/sysconfig/iptables_ $( date  +%s)
iptables -F
service iptables save
check_ok
action  "iptables is closed"  /bin/true
 
 
#set ulimit
echo  "ulimit -SHn 65535"  >> /etc/rc . local
 
#set SSH
sed  -i  's/#UseDNS yes/UseDNS no/g'  /etc/ssh/sshd_config
sed  -i  's/#Port 22/Port 65500/g'  /etc/ssh/sshd_config
service sshd restart
 
 
#set system files permission
chmod  600  /etc/passwd
chmod  600  /etc/group
chmod  600  /etc/shadow
chmod  600  /etc/gshadow
 
 
#set ntp
yum  install  ntpdate -y
ntpdate ntp.fudan.edu.cn
echo  "* 3 * * * /usr/sbin/ntpdate ntp.fudan.edu.cn >/dev/null 2>&1"  >> /etc/crontab
service crond restart
check_ok
action  "ntpdate is installed and add in crontab"  /bin/true
 
 
#set vim
echo  "===install vim,it will take serval mintinues==="
yum  install  vim-enhanced -y &> /dev/null
alias  vi =vim
echo  "alias vi=vim"  >> /root/ .bashrc
check_ok
action  "vim is installed"  /bin/true
 
 
#set yum repos
echo  "===update yum repos,it will take serval mintinues==="
yum  install  wget -y
mv  /etc/yum .repos.d /CentOS-Base .repo  /etc/yum .repos.d /CentOS-Base .repo.bak
wget -O  /etc/yum .repos.d /CentOS-Base .repo http: //mirrors .aliyun.com /repo/Centos-6 .repo &> /dev/null
wget -O  /etc/yum .repos.d /epel .repo http: //mirrors .aliyun.com /repo/epel-6 .repo &> /dev/null
yum clean all &> /dev/null
yum makecache &> /dev/null
check_ok
action   "yum repos update is ok"  /bin/true




本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1710247,如需转载请自行联系原作者
目录
相关文章
|
3天前
|
关系型数据库 MySQL Linux
一次虚拟机centos7系统崩溃恢复记录
一次虚拟机centos7系统崩溃恢复记录
|
3天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
10 1
|
3天前
|
Java Linux API
minio文档系统在centos7虚拟机上的搭建
minio文档系统在centos7虚拟机上的搭建
|
3天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
11 1
|
4天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
4天前
|
安全 Linux Anolis
centos停止更新?这篇博客教会你CentOS 7转化系统为阿里龙蜥Anolis OS 7
centos停止更新?这篇博客教会你CentOS 7转化系统为阿里龙蜥Anolis OS 7
|
4天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
24 5
|
6天前
|
安全 关系型数据库 MySQL
CentOS 7系统加固详细方案SSH FTP MYSQL加固
CentOS 7系统加固详细方案SSH FTP MYSQL加固
LXJ
|
Shell Linux
centos安装fail2ban脚本(shell,仅供参考)
centos安装fail2ban脚本(shell,仅供参考)
LXJ
253 0

热门文章

最新文章