#!/bin/bash
# 时间: 2018-11-21
# 作者: HuYuan
# 描述: CentOS 7 初始化脚本 # 加载配置文件
if [ -n "${1}" ];then
/bin/sh ${1}
fi # 可接受配置(shell 变量格式)
# INIT_HOSTNAME 主机名, 默认为 localhost
# INIT_NTPSERVER ntp 服务器, 默认为 ntp1.aliyun.com
# INIT_DNS1 dns 服务器
# INIT_DNS2
# INIT_DNS3 # INIT_ZABBIX_AGENT 是否安装 zabbix agent, 默认为 yes
# INIT_ZAB_VERSION zabbix agent 的版本, 默认为 3.4.11
# INIT_ZAB_SERVER zabbix server 的地址, 默认为 192.168.0.112
# INIT_ZAB_DOWNLOAD zabbix agent 的下载地址, 默认为阿里的 yum 仓库 # INIT_SSH_PORT ssh 监听端口, 默认 22
# INIT_SSH_DNS 是否启用 ssh dns 反解, 默认 no
# INIT_SSH_ROOT_LOGIN 是否允许 root 用户登录, 默认 yes HOSTNAME=${INIT_HOSTNAME:-'localhost'}
NTPSERVER=${INIT_NTPSERVER:-'ntp1.aliyun.com'}
DNS1=${INIT_DNS1:-'114.114.114.114'}
DNS2=${INIT_DNS2:-'223.5.5.5'}
DNS3=${INIT_DNS3:-'8.8.8.8'} ZABBIX_AGENT=${INIT_ZABBIX_AGENT:-'yes'}
ZABBIX_SERVER=${INIT_ZABBIX_SERVER:-'192.168.0.112'}
ZABBIX_VERSION=${INIT_ZABBIX_VERSION:-'3.4.11'}
ZABBIX_DOWNLOAD=${INIT_ZABBIX_DOWNLOAD:-'https://mirrors.aliyun.com/zabbix/zabbix/'} SSH_PORT=${INIT_SSH_PORT:-'22'}
SSH_DNS=${INIT_SSH_DNS:-'no'}
SSH_ROOT_LOGIN=${INIT_SSH_ROOT_LOGIN:-'yes'} ping -c 1 -w 3 114.114.114.114 &> /dev/null
if [ ${?} -ne 0 ];then
echo "系统初始化必须能够连接 Internet 网络"
exit 10
else
echo "nameserver 114.114.114.114" > /etc/resolv.conf echo -e "\033[31m正在更新软件包\033[0m"
yum -y update &> /dev/null yum -y install wget &> /dev/null
if [ ${?} -ne 0 ];then
echo -e "\033[31m安装wget失败, 退出${0}\033[0m"
exit 2
fi
fi set_service() {
setenforce 0
sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/sysconfig/selinux
sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/selinux/config disable_services=(firewalld postfix acpid ip6tables mcelogd mdmonitor rpcbind rpcgssd rpcidmapd auditd haldaemon lldpad atd kdump netfs nfslock openct)
for service in ${disable_services[@]};do
systemctl disable ${service} &> /dev/null
done
systemctl enable sshd crond &> /dev/null
} install_package() {
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
echo -e "\033[31m正在安装常用软件包\033[0m"
yum -y install vim lsof zip strace openssl openssl-devel gcc gcc-c++ cmake bind-utils libxml2-devel net-tools sysstat &> /dev/null
yum -y install ntpdate curl telnet lbzip2 bzip2 bzip2-devel pcre pcre-devel zlib-devel python-devel lrzsz man glibc glibc-devel &> /dev/null
} install_zabbix_agent() {
[ ${ZABBIX_AGENT} != 'yes' ] && return 0 echo -e "\033[31m正在安装zabbix-agent\033[0m"
cd /tmp/
ZAB_DOWNLOAD=${ZABBIX_DOWNLOAD}
ZAB_VERSION=${ZABBIX_VERSION}
VERSION=$(echo "${ZAB_VERSION}"|cut -c-3)
wget ${ZAB_DOWNLOAD}${VERSION}/rhel/7/x86_64/zabbix-agent-${ZAB_VERSION}-1.el7.x86_64.rpm &> /dev/null
yum -y localinstall zabbix-agent-${ZAB_VERSION}-1.el7.x86_64.rpm &> /dev/null
rm -f zabbix-agent-${ZAB_VERSION}-1.el7.x86_64.rpm sed -i "s#^Server=.*#Server=${ZABBIX_SERVER}#" /etc/zabbix/zabbix_agentd.conf
sed -i "s#^ServerActive=.*#ServerActive=${ZABBIX_SERVER}#" /etc/zabbix/zabbix_agentd.conf systemctl enable zabbix-agent &> /dev/null
systemctl start zabbix-agent &> /dev/null || echo -e "\033[31m安装zabbix-agent失败\033[0m" } init_system() {
hostnamectl set-hostname ${HOSTNAME}
timedatectl set-timezone Asia/Shanghai
echo "* * * * * /usr/sbin/ntpdate ${NTPSERVER}" > /var/spool/cron/root
echo "nameserver ${DNS1}" > /etc/resolv.conf
echo "nameserver ${DNS2}" >> /etc/resolv.conf
echo "nameserver ${DNS3}" >> /etc/resolv.conf
chattr +i /etc/resolv.conf
echo 'LANG="en_US.UTF-8"' > /etc/locale.conf
echo 'export LANG="en_US.UTF-8"' >> /etc/profile.d/custom.sh
} set_kernel_parameter() {
cat > /etc/sysctl.conf << EOF
fs.file-max = 999999
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
vm.swappiness = 10
EOF
} set_system_limit() {
cat >> /etc/security/limits.conf <<EOF
* soft nproc 65530
* hard nproc 65530
* soft nofile 65530
* hard nofile 65530
EOF
} set_history() {
cat >> /etc/profile.d/custom.sh <<EOF
USER_IP=$(who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g')
[ -z ${USER_IP} ] && USER_IP=${hostname}
export HISTTIMEFORMAT="%F %T ${USER_IP} "
shopt -s histappend
export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"
EOF
} # 如果升级失败造成的影响较大, 建议管理员手动升级
# update_openssh() {
# [ ${UPDATE_OPENSSH} == 'no' ] && return
# cd /tmp/
# yum -y install cmake gcc zlib-devel openssl-devel pam-devel wget
# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.back # wget https://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-7.5p1.tar.gz
# tar xf openssh-7.5p1.tar.gz && cd openssh-7.5p1 # ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-md5-passwords --with-pam \
# --with-zlib --with-openssl-includes=/usr --with-privsep-path=/var/lib/sshd # [ ${?} -ne 0 ] && echo "升级 openssh 失败" >> system_init.log && return
# make || echo "升级 openssh 失败" >> system_init.log && return
# make install || echo "升级 openssh 失败" >> system_init.log # cp /etc/ssh/sshd_config /etc/ssh/sshd_config.back2
# echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
# echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
# sed -i 's#GSSAPIAuthentication.*##' /etc/ssh/sshd_config
# sed -i 's#GSSAPICleanupCredentials.*##' /etc/ssh/sshd_config
# } set_openssh() {
echo "Port ${SSH_PORT}" >> /etc/ssh/sshd_config
[ ${SSH_DNS} != 'yes' ] && echo "UseDNS no" >> /etc/ssh/sshd_config
[ ${SSH_ROOT_LOGIN} == 'no' ] && sed -i 's/^#PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
} set_nic() {
grub_cinfig='GRUB_CMDLINE_LINUX="crashkernel=auto ipv6.disable=1 net.ifnames=0 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"'
sed -i "s#GRUB_CMDLINE_LINUX.*#${grub_cinfig}#" /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null || echo -e "\033[31m修改grub失败\033[0m"
} set_service
install_package
install_zabbix_agent
init_system
set_kernel_parameter
set_system_limit
set_history
set_openssh
set_nic echo -e "\033[32m${0} 执行完毕, 请确认无误之后执行 reboot 重启系统\033[0m"

  

centos 7 初始化脚本的更多相关文章

  1. centos虚拟机初始化脚本

    功能 修改主机名为node. 网卡开机启动并设置ip为静态18.8 更新yum源为国内yum源,并安装vim lrzsz wget man ntpdate软件. 关闭iptables selinux, ...

  2. centos系统初始化脚本

    #!/bin/bash #检测是否为root用户 ];then echo "Must be root can do this." exit fi #检测网络 echo " ...

  3. centos7 初始化脚本

    #!/bin/bash # 时间: 2018-11-21 # 作者: HuYuan # 描述: CentOS 7 初始化脚本 # 加载配置文件 if [ -n "${1}" ];t ...

  4. bash-1 初始化CentOS系统的初始化脚本

    初始化CentOS系统的初始化脚本 #!/bin/bash # #******************************************************************* ...

  5. centos初始化脚本

    centos初始化脚本 添加IP.主机名.挂载/dev/sdb1磁盘 #!/bin/bash # ip=$ hostname=$ if [ -z $ip ] || [ -z $hostname ]; ...

  6. Centos7初始化脚本

    今天分享一个自己写的一个初始化的小脚本. 编写初始化系统要考虑到系统的版本问题,现在用的比较多的就是centos6和centos7,所以首先要判断一下系统的版本. cat /etc/redhat-re ...

  7. centos系统初始化流程及实现系统裁剪

    Linux系统的初始化流程: POST:ROM+RAM BIOS: Boot Sequence MBR: 446:bootloader 64: 分区表 2: 5A kernel文件:基本磁盘分区 /s ...

  8. 搭建yum本地源_阿里云CentOS服务器初始化设置

    CentOS服务器初始化设置其实不分阿里云或其它服务器了,操作配置过程与步骤也差不多一.挂载硬盘 1.磁盘分区 fdisk -l #查看设备,一般可以看到设备名为/dev/xvdb fdisk /de ...

  9. liunx用户环境初始化脚本

          liunx用户环境初始化脚本 编写生成脚本基本格式,包括作者,联系方式,版本,时间,描述等 [root@magedu ~]# vim .vimrc set ignorecase set c ...

随机推荐

  1. java网络爬虫实现信息的抓取

    转载请注明出处:http://blog.csdn.NET/lmj623565791/article/details/23272657 今天公司有个需求,需要做一些指定网站查询后的数据的抓取,于是花了点 ...

  2. 2018.10.12 NOIP模拟 数据结构(线段树)

    传送门 sb线段树题居然还卡常. 修改操作直接更新区间最小值和区间标记下传即可. 询问加起来最多5e65e65e6个数. 因此直接询问5e65e65e6次最小值就行了. 代码

  3. 2018.09.11 poj2976Dropping tests(01分数规划)

    传送门 01分数规划板子题啊. 就是简单变形移项就行了. 显然 ∑i=1na[i]∑i=1nb[i]≤k" role="presentation" style=" ...

  4. 2018.07.31洛谷P1552 [APIO2012]派遣(可并堆)

    传送门 貌似是个可并堆的模板题,笔者懒得写左偏堆了,直接随机堆水过.实际上这题就是维护一个可合并的大根堆一直从叶子合并到根,如果堆中所有数的和超过了上限就一直弹直到所有数的和不超过上限为止,最后对于当 ...

  5. 解决sea_born和matplotlib画图中文显示的问题

    #以下解决mtpl中文显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] #以下解决seaborn中文编码报错问 ...

  6. java.lang.ExceptionInInitializerError Caused by: org.hibernate.InvalidMappingException: Unable to read XML

    此错误是说无法读取你的xml文档,于是我们就该去更改xml文档,因为我是自动生成的,所以我找了一份之前手写的,发现是dtd错了,把之前的dtd拷贝过来之后程序就测试通过了

  7. webUploader上传视频,包括上传进度、上传状态、暂停和取消等

    踩坑视频上传: 点击开始上传: 头部引入webuploader.css <!DOCTYPE html> <html lang="en"> <head& ...

  8. 解决火狐浏览器发送jquery的ajax请求无效的问题

    今天遇到这样一个问题: 页面在chrome下发送ajax的请求是没有问题的,但是在firfox下无效. 代码大致如下: //前面省略 <form> ..... <button cla ...

  9. C - 无间道之并查集 HihoCoder - 1066

    输入 每个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,表示黑叔叔总共进行的操作次数. 每组测试数据的第2~N+1行,每行分别描述黑叔叔的一次操作,其中第i+1行为一个整 ...

  10. SyntaxError: Non-ASCII character '\xe5' in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

    python的默认编码文件是用的ASCII码,而你的python文件中使用了中文等非英语字符. 在Python源文件的最开始一行,加入一句: # coding=UTF-8