linux shell脚本经典案例1---服务器系统配置初始化脚本

服务器系统配置初始化

背景:购买10台服务器并已安装Linux操作
需求:对10台服务器进行系统配置的初始化
问题:要做哪些初始化,要配置哪些东西?

  • 1.时间同步(与互联网时间同步)
  • 2.禁用selinux(selinux是一个安全机制,不会用的话,会影响做其他操作)
  • 3.关闭防火墙,清空防火墙默认策略
  • 4.历史命令显示操作时间(默认是不显示的,为了查看,历史审计,可以加上这个时间)
  • 5.禁止root远程登录(root远程登录是不安全的,)
  • 6.禁止定时任务发送邮件(默认定时任务产生的事件都会发送给默认的用户名的。长期积累会产生很多的垃圾文件。占用磁盘空间)
  • 7.设置最大打开文件数(默认是很少的,很多应用无法满足)
  • 8.减少Swap使用(默认当物理内存不够时,使用Swap作为物理内存的交换。而Swap是磁盘上的空间,读写性能远不如使用物理内存。尽量不使用Swap来提高引用程序的性能)
  • 9.一些系统内核参数的优化
  • 10.安装一些性能分析工具及其他

上述可以写一个shell脚本,可以一键部署的

1.时间同步(与互联网时间同步)
设置时间的时候要考虑定期的修改时间
centos是使用最广泛的,也是开源的

cat /etc/redhat-release
看系统的版本号

在这里插入图片描述

创建一个存放shell脚本的目录
mkdir shell_scripts
将文件1.sh移动到shell_scripts目录里面
mv 1.sh shell_scripts
安装ntp
yum install -y ntp
同步互联网时间
ntpdate time.windows.com(time.windows.com是一个windows时间服务器)
查看时间
date
查看所有的定时任务
crontab -l
写入定时任务(写脚本的时候不需要这样的交互输入)
crontab -e
强制保存退出
wq!

免交互输入

(echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
  • 每天1点同步,将它的错误和输出都追加为空
  • ;是紧接着再执行一个命令crontab -l
  • ():是将两个作为一组作为一个shell命令
  • |:是接收左边的标准输入
  • crontab:将左边的定时任务追加进crontab里面
if ! crontab -l |grep ntpdate &>/dev/null ; then
    (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
fi

判断一下,有没有这个定时任务,有的话就不追加,没有的话就追加

2.禁用selinux(selinux是一个安全机制,不会用的话,会影响做其他操作)
在/etc/selinux/config
下面将对应的参数改掉

查看一下
vi /etc/selinux/config
退出 : :wq!

在这里插入图片描述
默认是允许的,现在将它设置为disable

sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
  • sed 流编译器
  • -i 对文件进行修改
  • /SELINUX/:匹配到这一行
  • {s/enforcing/disabled/}:替换这一行中的enforcing为disabled

3.关闭防火墙,清空防火墙默认策略

if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
    systemctl stop firewalld
    systemctl disable firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
    service iptables stop
    chkconfig iptables off
fi
  • 先看一下是什么版本,如果是7点几的话,就把firewalld关掉
  • 如果是6点几的话,就把iptables关掉
  • &>/dev/null:是将标准的错误和标准的输出重定向为空
  • &>/dev/null 等同于 >/dev/null 2>&1

4.历史命令显示操作时间(默认是不显示的,为了查看,历史审计,可以加上这个时间)

if ! grep HISTTIMEFORMAT /etc/bashrc; then
    echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc
fi
>> /etc/bashrc:把追加到/etc/bashrc这个里面,下次启动用户终端时,会自动加载
centos装vim编译器
    yum -y install vim*
查看前面执行的命令
    history

在这里插入图片描述

前面是序号,现在想要加上执行的时间
export HISTTIMEFORMAT="%F %T `whoami` "

在这里插入图片描述
对审计,后续查看谁操作的,都很方便

5.SSH超时时间
终端不操作情况下的超时时间。提供安全性

if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
    echo "export TMOUT=600" >> /etc/profile
fi

终端不操作情况下的超时时间是10分钟

6.禁止root远程登录

sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

默认是允许登录的
vi /etc/ssh/sshd_config
在这里插入图片描述
将#PermitRootLogin yes改成PermitRootLogin no

7.禁止定时任务发送邮件

sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab

很多情况下在/var/mail/下会看到好多小文件
主要原因就是crontab -l里面的定时任务没有将标准输出和错误输出指向为空
会在/var/mail/这个下面生成许多小文件
设置上面的目的就是不让发这个邮件,让MAILTO="",默认是当前用户
‘s/^MAILTO=root/MAILTO=""/’:将源内容MAILTO=root,替换为MAILTO=""

vi /etc/crontab

在这里插入图片描述

8.设置最大打开文件数

if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
    cat >> /etc/security/limits.conf << EOF
    * soft nofile 65535
    * hard nofile 65535
EOF
fi
vi /etc/security/limits.conf

在这里插入图片描述

* soft nofile 65535
* hard nofile 65535
写入到这个文件的最后

  • domain:域,就是限制那方面的。*就是所有
  • type:有软线和硬线
  • item:项目,nofile打开文件名服务
  • values:数量


* soft nofile 65535
* hard nofile 65535
追加到/etc/security/limits.conf里面

9.减少SWAP使用

echo "0" > /proc/sys/vm/swappiness

在这里插入图片描述
默认是30,设置为0之后就尽量不用这个

10.安装系统性能分析工具及其他

yum install gcc make autoconf vim sysstat net-tools iostat iftop iotp lrzsz -y
chomd +x 1.sh
给这个文件加上可执行权限
安装dos2unix
yum install dos2unix -y
将windows下的脚本要在linux上执行,最好用dos2unix转化一下。不然很容易就出现语法或者格式错误

下面是完整的脚本

#/bin/bash
# 设置时区并同步时间
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
if ! crontab -l |grep ntpdate &>/dev/null ; then
    (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
fi

# 禁用selinux
sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config

# 关闭防火墙
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
    systemctl stop firewalld
    systemctl disable firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
    service iptables stop
    chkconfig iptables off
fi

# 历史命令显示操作时间
if ! grep HISTTIMEFORMAT /etc/bashrc; then
    echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc
fi

# SSH超时时间
if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
    echo "export TMOUT=600" >> /etc/profile
fi

# 禁止root远程登录
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

# 禁止定时任务向发送邮件
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab

# 设置最大打开文件数
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
    cat >> /etc/security/limits.conf << EOF
    * soft nofile 65535
    * hard nofile 65535
    EOF
fi

# 系统内核优化
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_fin_timeout = 20
EOF

# 减少SWAP使用
echo "0" > /proc/sys/vm/swappiness

# 安装系统性能分析工具及其他
yum install gcc make autoconf vim sysstat net-tools iostat iftop iotp lrzsz -y
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章