#### 20191223 当前模版,容器镜像方式,脚本优化不需要太复杂,废弃了
>### 备注:使用前请阅读并测试
>内核优化部分感谢[海渊_haiyuan的博客](http://blog.csdn.net/ll845876425)
>早期脚本原型感谢suzezhi,抱歉当时未收藏博客
>20170918更新了Centos7的优化脚本,相关内容也更新到了Cobbler脚本调用中
### Centos 7 一键优化
#### 为了后期调用,分为公共函数,自定义函数和主文件
##### centos7_optimiz.sh
```shell
#!/bin/sh
#Author: NoteD
#Date: 2017-05-3
#Remark: Centos 7 system optimization script
# Shell variables #
script_logdir="/var/log/"
shell_name=$(basename $0)
shell_logfile="$script_logdir/${shell_name}.log"
# System variables
create_basdir="/data/ "
local_repo_name="cobbler-config.repo"
install_basesoft="gcc gcc-c++ ntp lrzsz tree telnet dos2unix sysstat sysstat iptraf ncurses-devel openssl-devel zlib-devel OpenIPMI-tools nmap screen nfs-utils"
system_user="ding"
user_id="2017"
system_user_password="ding@)!&"
ssh_port="52020"
ntp_server="202.120.2.101"
ctrlaltdel="/usr/lib/systemd/system/ctrl-alt-del.target"
# Set Check shell
system_user_check=`cat /etc/passwd |grep ${system_user} |wc -l`
system_openfile_check=`ulimit -a |grep "open files"|awk '{print $4}'`
system_kernel_check=`grep "NoteD" /etc/sysctl.conf|wc -l`
ntpdate_check=`grep "ntpdate" /var/spool/cron/root|wc -l`
check_shell=`grep "PS1" /etc/profile|wc -l`
# Load function
. ./custom_fun.sh
. ./public_fun.sh
# Exec
function_check_uid
function_create_basedir
function_set_repo
function_install_basesoft
function_create_user
function_set_openfile
function_disable_ctlaltdel
function_disable_selinux
function_set_ssh
function_disable_ipv6
function_set_kernel
function_set_ntp
function_set_shell
function_set_vim
```
##### custom_fun.sh
```shell
#Customize Function: Create basedir
function_create_basedir(){
mkdir -p ${create_basdir}
function_writelog_judgment "[create basedir]"
}
#Customize Function: set yum repo
function_set_repo(){
#clean OS default repo
mkdir /etc/yum.repos.d/old && mv /etc/yum.repos.d/C* /etc/yum.repos.d/old/
if [ ! -f "/etc/yum.repos.d/${local_repo_name}" ];then
#add repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
function_writelog_judgment "[add aliyun mirrors base]"
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
function_writelog_judgment "[add aliyun mirrors epel]"
#rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
fi
# update system soft
yum clean all
yum -y update
function_writelog_judgment "[update system]"
}
#Customize Function: install base soft
function_install_basesoft(){
yum -y install ${install_basesoft}
function_writelog_judgment "[install basesoft]"
}
#Customize Function: create user
function_create_user(){
if [ ${system_user_check} -eq 0 ];then
#add default user
useradd ${system_user} -u ${user_id}
function_writelog_judgment "[add user ${system_user}]"
echo "${system_user_password}" | passwd --stdin ${system_user} && history -c
function_writelog_judgment "[set ${system_user} password]"
#set sudo authority
echo "" >> /etc/sudoers
echo "#set sudo authority" >> /etc/sudoers
echo "${system_user} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
fi
function_writelog_judgment "[set user sudo]"
}
#Customize Function: set open file
function_set_openfile(){
if [ ${system_openfile_check} -lt 65535 ];then
#set the file limit
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535
* hard nofile 65535
EOF
fi
function_writelog_judgment "[set system openfile]"
}
#Customize Function: disable server
function_disable_ctlaltdel(){
if [ -f ${ctrlaltdel} ];then
#disable control-alt-delete
mv /usr/lib/systemd/system/ctrl-alt-del.target /usr/lib/systemd/system/ctrl-alt-del.target.bak
function_writelog_judgment "[disable ctrl+alt+del]"
fi
}
function_disable_selinux(){
#disable selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
function_writelog_judgment "[disable selinux]"
}
#Customize Function: set ssh
function_set_ssh(){
#set ssh
\cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +%F`
sed -i "s/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/" /etc/ssh/sshd_config
sed -i "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config
sed -i "s%#PermitRootLogin yes%PermitRootLogin no%g" /etc/ssh/sshd_config
sed -i "s%#PermitEmptyPasswords no%PermitEmptyPasswords no%g" /etc/ssh/sshd_config
#sed -i "s%#Port 22%Port ${ssh_port} %g" /etc/ssh/sshd_config
function_writelog_judgment "[set ssh]"
}
#Customize Function: disable ipv6
function_disable_ipv6(){
if [ `ip addr|grep inet6|wc -l ` -gt 0 ]; then
sed -i 's%^IPV6INIT="yes"%IPV6INIT="no"%g' /etc/sysconfig/network-scripts/ifcfg-eth0
#sed -i 's%^IPV6INIT="yes"%IPV6INIT="no"%g' /etc/sysconfig/network-scripts/ifcfg-eth1
#sed -i 's%^IPV6INIT="yes"%IPV6INIT="no"%g' /etc/sysconfig/network-scripts/ifcfg-eth2
#sed -i 's%^IPV6INIT="yes"%IPV6INIT="no"%g' /etc/sysconfig/network-scripts/ifcfg-eth3
echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network
fi
function_writelog_judgment "[disable ipv6]"
}
#Customize Function: set kernel
function_set_kernel(){
if [ ${system_kernel_check} -eq 0 ];then
#tune kernel parametres
cat >> /etc/sysctl.conf << EOF
#20170914 NoteD update
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_fin_timeout = 30
vm.swappiness=10
vm.max_map_count = 262144
net.ipv6.conf.all.disable_ipv6=1
EOF
/sbin/sysctl -p
function_writelog_judgment "[set sysctl]"
fi
}
#Customize Function: set ntp
function_set_ntp(){
if [ ${ntpdate_check} -eq 0 ]; then
echo "* 4 * * * /usr/sbin/ntpdate ${ntp_server}> /dev/null 2>&1" >> /var/spool/cron/root
fi
function_writelog_judgment "[add ntp]"
}
#Customize Function: set shell
function_set_shell(){
if [ ${check_shell} -eq 0 ]; then
#modify PS1
echo 'export PS1="[ \033[01;33m\u\033[0;36m@\033[01;34m\h \033[01;31m\w\033[0m ]\033[0m \n#"' >> /etc/profile
function_writelog_judgment "[set shell]"
fi
}
#Customize Function: set vim
function_set_vim(){
if [ ! -f /root/.vimrc ]; then
#modify vimrc
cat >> /root/.vimrc << EOF
syntax enable
syntax on
set ruler
set number
set cursorline
set cursorcolumn
set hlsearch
set incsearch
set ignorecase
set nocompatible
set wildmenu
set paste
set expandtab
set tabstop=2
set shiftwidth=4
set softtabstop=4
set gcr=a:block-blinkon0
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions-=R
highlight CursorLine cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE
highlight CursorColumn cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE
EOF
cp /root/.vimrc /home/${system_user}/
fi
function_writelog_judgment "[set vim]"
}
```
##### public_fun.sh(部分函数未使用)
```shell
#Public Function: judgment lockfile
function_create_lockfile(){
if [ -f $shell_lockfile ];then
function_writelog_simple "[Warning] The script is running! please wait."
exit
else
echo $$ > $shell_lockfile
fi
}
#Public Function: directly_delete shell lockfile
function_delete_lockfile(){
rm -f ${shell_lockfile} && exit 1
}
#Public Function: judgment after delete shell lockfile
function_judgment_delete_lockfile(){
if [ $? -ne 0 ];then
function_delete_lockfile
exit
fi
}
#Public Function: judgment and write Log
function_writelog_judgment(){
if [ $? -eq 0 ];then
log_info=$1
echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") [Note]: ${shell_name} : ${log_info} executed sucessfully" >> ${shell_logfile}
else
echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") [Error]: ${shell_name} : ${log_info} executed failed" |tee -a ${shell_logfile}
exit
fi
}
#Public Function: simple write Log
function_writelog_simple(){
log_info=$1
echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") : ${shell_name} : ${log_info} " >> ${shell_logfile}
}
#Public Function: judgment UID
function_check_uid(){
if [ $UID -eq 0 ];then
return 0
else
echo "Please use root run script."
exit 1
fi
}
function_check_network(){
if [ `curl -s -w %{http_code} http://www.baidu.com -o /dev/null` -ne 200 ];then
function_writelog_simple "network "
exit
else
echo $$ > $shell_lockfile
fi
}
```
### Centos 6 一键优化(由于时间关系,6.x的优化没有做完全转化)
centos6_optimiz.sh
```shell
#crond clean mail file
echo "find /var/spool/clientmqueue/ -type f -mtime +30 |xargs rm -f" > /app/scripts/del_clientmqueue.sh
chmod +x /app/scripts/del_clientmqueue.sh
echo "00 00 * * 6 /bin/sh /server/scripts/del_clientmqueue.sh >/dev/null 2>&1" >> /var/spool/cron/root
#update system character
cp /etc/sysconfig/i18n /etc/sysconfig/i18n.Brandyn
echo 'LANG="en_US.UTF-8"' >/etc/sysconfig/i18n
source /etc/sysconfig/i18n
#set the control-alt-delete to guard against the miSUSE
mv /etc/init/control-alt-delete.conf /etc/init/control-alt-delete.conf.bak
#disabel service
LANG=en
for chkoff in `chkconfig --list|grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $chkoff off;done
for chkoff in crond network rsyslog sshd;do chkconfig --level 3 $chkoff on;done
```
- 献给乐于奉献的你
- 一、工作感悟
- 1.1 工作感悟
- 1.2 数据库工作总结
- 二、运维专题(非技术)
- 2.1 公有云运维
- 2.1.1 阿里云采坑记.md
- 三、运维专题(技术类)
- 3.1 Linux(操作系统)
- 3.1.1 常见工作总结
- 3.1.2 常见服务使用和部署
- 3.1.3 操作系统优化
- 3.1.4 常用命令(Centos8)
- 3.2 Docker & K8s(容器技术)
- 3.2.1 Docker
- 1. Docker
- 1-1 容器基础
- 1-2 部署和加速
- 1-3 常用命令
- 1-4 Dockerfile编写
- 1-5 容器网络
- 1-6 数据持久化
- 2. docker-compose
- 2-1 基础
- 3.2.2 kubernetes
- 1. 导读-请先看我
- 2. kubeadm部署集群
- 1-1 k8s-1.14-基于calico
- 1-2 k8s-1.17-基于flanne
- 3. 二进制部署集群
- 4. 日常工作及故障处理
- 4-1 常用命令
- 4-2 故障处理
- 3.2.3 依赖服务部署
- 1. Harbor(镜像仓库)
- 1-1 harbor-2.1.0(单节点)
- 3.3 CICD(持续集成/部署)
- 3.3.1 GitLab
- 1. 服务部署
- 1-1 Gitlab-CE-13.3.4(单节点)
- 2. Git基础
- 3.3.2 Ansible
- 1. 服务部署
- 1-2 ansible-2.5(pip部署)
- 3. ansible-playbook
- 3-1 基于Roles的Playbook
- 3-3 循环语法
- 3.3.3 Jnekins
- 1. Jenkins部署
- 1-1 Jenkins-2.65部署
- 1-2 Jenkins-2.249部署
- 2. Jenkins项目初始化
- 3. Jenkins集成
- 3-1 Jenkins-2.65集成Sonar
- 3.4 LB/HA(负载均衡,反向代理)
- 3.4.1 LVS+Keepalive
- 1. LVS为MySQL读提供负载均衡
- 3.4.2 Pacemaker(HA)
- 1. 常用命令(转)
- 3.5 Runtime(代码运行环境)
- 3.5.1 Tomcat(Web中间件)
- 1. Tomcat部署手册
- 1-1 Tomcat-7.0.76部署
- 2. Tomcat常用脚本
- 3.6 NoSQL(非关系型数据库)
- 3.6.1 redis(非关系数据库)
- 1. Redis 基础
- 2. Redis 4.0变化
- 3. Codis实现Redis的集群
- 4. Redis故障处理
- 5. redis安全第一步
- 6. Redis集群搭建
- 7. CacheCloud部署
- 3.6.1 Redis挑战
- 3.6.2 MongoDB(文档数据库)
- 1. Mongodb基础
- 1-1 Mongodb4.0新特性
- 1-2 支持多大数据量
- 2. Mongodb安装
- 2-1 Mac OS安装Mongodb
- 2-2 Yum安装Mongodb
- 2-3 二进制安装Mongodb
- 2-4 docker容器安装Mongodb
- 2-5 Mongodb 配置文件详解
- 2-6 Mongodb 生产安全清单
- 2-7 用户身份认证和授权
- 3. Mongodb副本集
- 3-1 副本集搭建
- 3-2 用户身份认证与授权
- 4. 日常维护工作
- 4-1 Mongodb磁盘回收
- 4-2 Mongodb备份恢复到任意时间点
- 4-3 Mongodb慢查询分析
- 4-4 Mongodb版本升级
- 4-5 Mongodb副本集成员状态
- 4-6 Mongodb备份恢复工具使用
- 4-7 Mongodb服务启动和停止
- 4-8 修改副本集成员oplog大小
- 4-9 Mongodb 副本集Oplog
- 3.7 MQ(消息队列)
- 3.7.1 Zookeeper(分布式协调系统)
- 1. ZooKeeper基础
- 2. ZooKeeper集群搭建
- 2-1 ZK-3.4.10部署
- 3.2 RabbitMQ(消息队列)
- 1. 服务部署
- 1-1 RabbitMQ-3.8部署
- 2. 常用命令
- 3.8 Monitor(数据收集,监控)
- 3.8.1 Zabbix(运维监控)
- 1. 服务部署
- 1-1 服务端部署
- 1-2 客户端部署
- 2. 监控服务
- 2-1 监控Apache
- 2-2 监控IIS
- 2-3 监控Ningx
- 2-4 监控Tomcat(6/7/8)
- 2-5 监控WebSphere 7
- 2-6 监控MySQL
- 2-7 监控Oracle
- 2-8 监控SQL Servre
- 2-9 监控Weblogic
- 2-10 监控Windows
- 2-11 自定义监控项
- 3. 告警推送
- 3-1 邮件告警
- 3-2 短信告警
- 3-3 告警推到Syslog
- 4. 日常工作
- 4-1 数据库优化(TokuDB)
- 4-2 数据库优化(分区表)
- 4-3 前端定制(Grafana)
- 5. 与Grafana结合
- 3.8.2 ELKBstack(日志收集展示)
- 1. 服务部署
- 1-1 ELK 5.5部署及配置
- 1-1-1 ELKBstack介绍
- 1-1-2 Elasticsearch部署
- 1-1-3 Logstash部署
- 1-1-4 Kibana部署
- 1-1-5 X-pack部署
- 1-1-6 Filebeat部署
- 2. ELK高级配置
- 1. Elasticsearch实战
- 2. Logstash实战
- 3. Filebeat实战
- 5. 引入队列
- 3.9 Virtualization(虚拟化)
- 3.10 Basic(基础服务)
- 3.10.1 Piwik-Matomo(用户行为分析)
- 1. Piwik前期分析
- 2. Piwik介绍和部署
- 2-1 Piwik-3.x版本(早期)
- 3. Piwik 功能配置
- 4. Piwik 模拟数据和压测
- 5. Piwik运转原理
- 6. Piwik数据库模式(一)
- 6-1 第一部分
- 6-2 第二部分
- 3.10.2 Cobbler(系统自动部署)
- 1. Cobbler 可以干什么?
- 2. Cobbler 基础原理
- 3. Cobbler 安装
- 3-1 Cobbler-2.8部署
- 4. Cobbler 基础配置
- 5. Cobbler 配置文件
- 6. 一键优化脚本
- 3.10.3 Rsync(数据同步服务)
- 1. Rsync基础
- 2. 案例:页面部署(服务端拉取)
- 3.10.4 NFS(共享存储)
- 1. NFS部署手册
- 2. 客户端NFS备份脚本
- 3.10.5 Grafana(可视化)
- 1. 安装(8.2.x)
- 3.11 Tools(软件工具)
- 3.11.1 基准测试
- 1. 基准测试方法论
- 2. 压测工具 - Siege
- 3. 压测工具 - http_load
- 3.12 DB(关系型数据库)
- 3.12.1 MySQL(关系数据库)
- 1. MySQL部署
- 1-1 MySQL-5.7部署
- 1-2 Percona-5.7 + TokuDB 部署
- 2. MySQL复制
- 2-1 MySQL异步复制
- 3. MySQL备份恢复
- 3-1 xtrabackup 备份恢复
- 4. MySQL 高可用
- 4-1 MHA(HA)
- 4-1-1 MHA 架构介绍和原理
- 4-1-2 MHA日常管理
- 4-1-3 MHA 自动Failover
- 4-1-4 MHA常用参数
- 4-1-5 MHA 报错
- 4-1-6 MHA相关配置文件和脚本
- 4-2 MyCAT
- 4-2-1 MyCAT 介绍和部署
- 4-1-3 MyCAT读写分离案例解析
- 5. MySQL 常用脚本
- 5-1 MySQL常用统计语句
- 5-2 MySQL性能分析脚本
- 6. MySQL 日常及故障处理
- 6-1 MySQL死锁排查
- 6-2 复制故障
- 6-3 MySQL 升级注意事项
- 6-3 MySQL授权
- 3.12.2 Oracle(关系数据库)
- 1. Oracle部署
- 1-1 Oracle11g单实例部署
- 1-2 Oracle12c单实例部署
- 2. Oracle常用脚本
- 3. Oracle 知识点
- 六、Ansible开源项目
- 6.1 项目初始化手册
- 6.1.1 Ansible错误处理
- 6.1.2 一种预先判断是否操作的方法
- 6.2 System初始化
- 6.3 Nginx/Tnginx部署
- 6.4 Python部署
- 6.5 PHP部署
- 6.6 MySQL部署
- 6.7 Docker部署
- 6.8 Haproxy部署
- 6.9 Redis部署
- 1. 变量和tags信息
- 3. Redis主从部署
- 4. Redis集群部署
- 5. 清理数据
- 6.10 Software软件部署
- 6.11 Zabbix部署
- 6.12 Elastic部署
- 6.13 Tomcat
- 6.14 Kafka部署
- 6.15 Zookeeper部署
- 6.16 Etcd集群部署
- 6.17 M3DB部署
- 6.18 Pormetheus部署
- 七、学习资源推荐
- 八、从瞎搞到放弃
- 8.1 CodeQL(语义代码分析引擎)
- 8.1.1 背景及计划
- 8.1.2 CodeQL概述
- 8.1.3 简单部署和使用
- 8.1.4 后续
- 8.2 dbdeployer(轻松部署MySQL)
- 归档笔记
- 三、常用服务部署(迁移中)
- 3.4 Nginx & PHP(Web服务)
- 3.4.1 Nginx(Web)
- 1. Nginx基础和部署
- 2. Nginx 我的一些思考
- 3. Nginx(Web)配置
- 4. Nginx(Proxy)配置
- 5. Nginx日常管理
- 3.4.3 PHP
- 1. PHP 7.1 部署
- 2. PHP5.6 部署
- 4. PHP原理
- 5. PHP 常用模块
- 二、运维项目实战(迁移中)
- 2.1 标准化 & 工具化项目
- 2.1.1 系统部署和优化
- 2.1.5 全网日志收集展示平台项目
- 1. 项目需求
- 2. 整体方案规划
- 3. 日志收集配置
- 4. 消息缓冲队列
- 5. 日志处理转发
- 6. 日志数据展示(待补充)
- 7. ELK安全配置(上)
- 8. ELK安全配置(下)
- 9. 项目总结
- 2.2 高性能Web项目
- 2.2.1 网站需求(完善中)