咸鱼今天给大家分享一个无论是学习还是工作中都很实用的 Linux 系统初始化脚本,其实就是各种命令的集合

完整代码在文章最后哦

定义相关变量

 

配置 yum 镜像源

获取阿里云 yum 镜像源

判断函数是否执行成功

写入一行配置

修改配置

配置系统时区

配置 dns 服务器

修改最大文件描述符限制

关闭系统不需要的服务

内核参数优化相关

安装常用工具

关闭 SELinux

主函数

完整脚本

#环境变量
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH #当前时间
current_time=$(date +%Y%m%d) #阿里的DNS
dns_server=223.5.5.5 if [[ ! -z `uname -r|grep 'el6'` ]]
then
kernel_version=el6
yum_repo=http://mirrors.aliyun.com/repo/Centos-6.repo
elif [[ ! -z `uname -r|grep 'el7'` ]]
then
kernel_version=el7
yum_repo=http://mirrors.aliyun.com/repo/Centos-7.repo
else
echo -e "\e[31mUnidentified Kernel version: $(uname -r). Only support for kernel el6/el7\e[0m"
exit
fi function add_yum_repo(){
local item="Add Aliyun Yum Mirrors"
yum clean all
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.${current_time} && \
curl -o /etc/yum.repos.d/CentOS-Base.repo ${yum_repo} > /dev/null 2>&1
show_result $? "${item}"
yum makecache
} function show_result(){
if [ "$1" -eq 0 ]
then
echo -e "\e[32m$2 is Success . [ OK ] \e[0m"
else
echo -e "\e[31m$2 is Fail . [ FAIL ] \e[0m"
fi
} function add_newconfig_tofile(){
local SearchResult=`grep "$1" "$2"`
if [ -z "${SearchResult}" ]
then
echo "$1" >> $2
fi
} function add_config_tofile(){
local keywords=`echo $1| awk -F "[= ]+" '{print $1}'`
local SearchResult=`grep "^${keywords}" "$2"`
if [ -z "${SearchResult}" ] #空为真,非空为假
then
echo $1 >> $2
else
sed -i "s/^${keywords}.*/$1/" $2
fi
} function config_localtime(){
local item="Config SH As Location"
rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
show_result $? "${item}"
} function config_dns_addr(){
local item="Config DNS Address"
cp /etc/resolv.conf /etc/resolv.conf.${current_time} && \
echo "nameserver ${dns_server}" > /etc/resolv.conf
show_result $? "${item}"
} function maximum_file_dspt(){
local item="Maximum File Descriptor"
cp /etc/security/limits.conf /etc/security/limits.conf.${current_time} && \
echo "* soft nofile 100000
* hard nofile 100000
* soft nproc 65535
* hard nproc 65535
* soft core unlimited
* hard core unlimited" > /etc/security/limits.conf
show_result $? "${item}"
} function shutdown_nonuse_srv(){
local item="Shutdown Unused Services"
if [[ "${kernel_version}" == el6 ]]
then
for i in `chkconfig --list | awk '{print $1}'`
do
chkconfig --level 2345 $i off > /dev/null 2>&1
done
for ii in crond network rsyslog sshd sysstat haldaemon
do
chkconfig --level 2345 $ii on > /dev/null 2>&1
done
show_result $? "${item}"
elif [[ "${kernel_version}" == el7 ]]
then
systemctl disable postfix > /dev/null 2>&1
show_result $? "${item}"
else
echo -e "\e[31mUnidentified Kernel version: $(uname -r). Only support for kernel el6/el7\e[0m"
fi
} function optimize_kel_args(){
local item="Optimize Kernel Arguments"
cp /etc/sysctl.conf /etc/sysctl.conf.${current_time} > /dev/null 2>&1
arch_ratio=$([[ ! -z $(uname -a | grep x86_64) ]] && expr 64 / 32 || expr 32 / 32)
memory_size=$(free -b| awk 'NR==2{print $2}')
nf_conntrack_size=$(expr ${memory_size} / 16384 / ${arch_ratio})
#开启反向路径过滤
add_config_tofile "net.ipv4.conf.default.rp_filter = 1" /etc/sysctl.conf
add_config_tofile "net.ipv4.conf.all.rp_filter = 1" /etc/sysctl.conf
#处理无源路由包
add_config_tofile "net.ipv4.conf.all.accept_source_route = 0" /etc/sysctl.conf
add_config_tofile "net.ipv4.conf.default.accept_source_route = 0" /etc/sysctl.conf
#core文件名中添加pid作为扩展名
add_config_tofile "kernel.core_uses_pid = 1" /etc/sysctl.conf
#开启syn洪水攻击保护
add_config_tofile "net.ipv4.tcp_syncookies = 1" /etc/sysctl.conf
#修改消息队列长度
add_config_tofile "kernel.msgmnb = 65536" /etc/sysctl.conf
add_config_tofile "kernel.msgmax = 65536" /etc/sysctl.conf
#修改最大内存共享段大小bytes
add_config_tofile "kernel.shmmax = 68719476736" /etc/sysctl.conf
add_config_tofile "kernel.shmall = 4294967296" /etc/sysctl.conf
#timewait数量默认18000
add_config_tofile "net.ipv4.tcp_max_tw_buckets = 600" /etc/sysctl.conf
add_config_tofile "net.ipv4.tcp_sack = 1" /etc/sysctl.conf
add_config_tofile "net.ipv4.tcp_window_scaling = 1" /etc/sysctl.conf
add_config_tofile "net.ipv4.tcp_rmem = 4096 87380 16777216" /etc/sysctl.conf
add_config_tofile "net.ipv4.tcp_wmem = 4096 65536 16777216" /etc/sysctl.conf
add_config_tofile "net.core.rmem_default = 8388608" /etc/sysctl.conf
add_config_tofile "net.core.wmem_max = 16777216" /etc/sysctl.conf
#未收到客户端确认信息连接请求的最大值
add_config_tofile "net.ipv4.tcp_max_syn_backlog = 262144" /etc/sysctl.conf
#放弃建立连接之前发送的synack包
add_config_tofile "net.ipv4.tcp_syn_retries = 2" /etc/sysctl.conf
#开启重用,允许time—wait socket 重新用语新的tcp连接
add_config_tofile "net.ipv4.tcp_tw_reuse = 1" /etc/sysctl.conf
add_config_tofile "net.ipv4.tcp_fin_timeout = 1" /etc/sysctl.conf
#防止简单的ddos攻击
add_config_tofile "net.ipv4.tcp_max_orphans = 3276800" /etc/sysctl.conf
#启用timewait快速收回
add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
#keeptime启用时tcp发送keepalive消息的频度,默认2h
add_config_tofile "net.ipv4.tcp_keepalive_time = 600" /etc/sysctl.conf
#允许系统打开的端口范围
add_config_tofile "net.ipv4.ip_local_port_range = 1024 65535" /etc/sysctl.conf
#资源回收
add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
#路由转发
add_config_tofile "net.ipv4.ip_forward = 1" /etc/sysctl.conf
#修改防火墙连接跟踪表大小,默认65535
add_config_tofile "net.netfilter.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
add_config_tofile "net.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
#解禁ping
add_config_tofile "net.ipv4.icmp_echo_ignore_all = 0" /etc/sysctl.conf
modprobe bridge
sysctl -p > /dev/null 2>&1
show_result $? "${item}"
} function install_pkgs(){
local item="Install Common Pkgs"
yum -y groupinstall "Development libraries" > /dev/null 2>&1
yum -y groupinstall "Development tools" > /dev/null 2>&1
yum -y install sysstat tree lrzsz telnet wget net-tools tcpdump lsof vim ntp > /dev/null 2>&1
show_result $? "${item}"
} function shutdown_selinux(){
local item="Shutdown Selinux "
setenforce 0 > /dev/null 2>&1
cp /etc/selinux/config /etc/selinux/config.${current_time} && \
sed -i 's:SELINUX=enforcing:SELINUX=disabled:g' /etc/selinux/config
show_result $? "${item}"
} function main(){
echo -e '\033[34;1m开始初始化操作系统中......\033[0m'
add_yum_repo
install_pkgs
config_localtime
config_dns_addr
maximum_file_dspt
shutdown_nonuse_srv
shutdown_selinux
optimize_kel_args
echo -e '\033[34;1m服务器系统初始化已完成!\033[0m'
} main

干货!超实用的 Linux 初始化脚本的更多相关文章

  1. Linux初始化脚本

    以下脚本用于linux系统的初始化脚本,可以在服务器系统安装完毕之后立即执行.脚本结合各位大牛一些参数,已经在CentOS 5下通过. 使用方法:将其复制,保存为一个shell文件,比如init.sh ...

  2. linux系统最小化安装后的初始化脚本

    作为运维人员,经常会初始化系统,系统在安装过程中基本都会选择最小化安装,这样安装好的系统里会缺少很多环境. 下面分享一个系统安装后的初始化脚本: #!/bin/bash #系统时最小化安装的,这里要安 ...

  3. ubuntu系统安装初始化脚本

    ubuntu系统安装初始化脚本 经常安装卸载ubuntu,每次系统安装完成后要安装好多软件,一个个命令输很麻烦浪费时间,一个sh文件全搞定! 1. flashplayer下载:点击这里 2. Auda ...

  4. 浅析 Linux 初始化 init 系统,第 1 部分: sysvinit 第 2 部分: UpStart 第 3 部分: Systemd

    浅析 Linux 初始化 init 系统,第 1 部分: sysvinit  第 2 部分: UpStart 第 3 部分: Systemd http://www.ibm.com/developerw ...

  5. linux ——shell 脚本

                                                      linux—shell 脚本  精简基础                 2018/10/30 13 ...

  6. Linux链接脚本学习--lds(转)

    Linux链接脚本学习--lds 一.概论 ld: GNU的链接器. 用来把一定量的目标文件跟档案文件链接在一起,并重新定位它们的数据,链接符号引用. 一般编译一个程序时,最后一步就是运行ld进行链接 ...

  7. Linux 初始化之 Systemd机制

    systemd是Linux下的一种init软件,由Lennart Poettering带头开发,其开发目标是提供更优秀的框架以表示系统服务间的依赖关系,并依此实现系统初始化时服务的并行启动,同时达到降 ...

  8. LINUX SHELL脚本攻略笔记[速查]

    Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...

  9. 温故之--Linux 初始化 init 系统

    参选URL: http://www.ibm.com/developerworks/cn/linux/1407_liuming_init1/index.html 本系列一共三篇,看完记住,那水平就不一样 ...

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

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

随机推荐

  1. 高可用系列文章之三 - NGINX 高可用实施方案

    前文链接 高可用系列文章之一 - 概述 - 东风微鸣技术博客 (ewhisper.cn) 高可用系列文章之二 - 传统分层架构技术方案 - 东风微鸣技术博客 (ewhisper.cn) 四 NGINX ...

  2. Linux下“减速”查看日志的方法

    Linux下"减速"查看日志的方法 需求场景 今天查看日志,有个需求,需要按照指定"速率"输出日志信息到终端屏幕上,方便查看. 这个需求日常应该也经常会碰到,比 ...

  3. 【转载】ADOX.Catalog中文帮助详细说明chm文档

    首先给个完全版的地址,如果您机器上装过OFFICE应该可以打开的:ADOX 对象模型, 地址是:"C:\Program Files\Common Files\Microsoft Shared ...

  4. LVGL 中图片使用问题

    此笔记主要是记录在 LVGL 中使用图片的几种方式,以及使用过程中遇到的问题.最近在 ARM linux 中使用 LVGL 时,发现加载图片变得很卡,一开始还好,当连续加载的图片变多后,特别是动画的过 ...

  5. LCD副屏-CPU,内存,时显,日期显示

    文章结构: 项目概述 成品预览 项目框架 硬件资料,代码 项目槽点 -项目概述- 以前的旧模块搁置很久没用,最近看到圈子很多倒腾电脑副屏的,咱虽然没钱,但是闲啊,刚好手头有这些东西,开干. 目的: 显 ...

  6. 拜占庭将军问题与CAP

    1.拜占庭将军问题 拜占庭位于如今的土耳其的伊斯坦布尔,是东罗马帝国的首都.由于当时拜占庭罗马帝国国土辽阔,为了达到防御目的,每个军队都分隔很远,将军与将军之间只能靠信差传消息.在战争的时候,拜占庭军 ...

  7. Grafana 系列文章(八):Grafana Explore 中的 Inspector

    ️URL: https://grafana.com/docs/grafana/latest/explore/explore-inspector/ Description: Explore 中的检查器 ...

  8. 在日报、读后感、小说、公文模版、编程等场景体验了一把chatGPT

    总结/朱季谦 在日报.读后感.小说.公文模版.编程等场景体验了一把chatGPT,说下体会. 昨天经过一顿操作猛如虎的捣鼓,终于在Mac笔记本上将chatGPT的访问环境搭建了起来,忍不住立马开始玩起 ...

  9. Nacos注册中心 (介绍与配置)

    Nacos注册中心 和Euraka一样都是注册中心,但Nacos不需要注册Nacos,因为阿里巴巴已经注册在好了.只需要下载安装后打开即可 Windows安装 开发阶段采用单机安装即可 1.1.下载安 ...

  10. 06yarn简介

    YARN简介 一.YARN是什么 YARN不是facebook的那个yarn,它从Hadoop 2引入,最初目的是改善MapReduce的实现,但是因为具备足够通用性,同样也可以支持其他的分布式计算模 ...