Shell脚本-服务器系统配置初始化

Shell脚本-服务器系统配置初始化

背景:

新购买多台服务器并且安装Linux操作系统,对服务器进行基础配置

需求:

  • 设置时区,并同步时间

  • 关闭selinux

  • 关闭防火墙,清除防火墙策略

  • 历史命令显示操作时间

  • 禁止root远程登陆

  • 禁止定时任务发送邮件

  • 设置最大打开文件数

  • 减少Swap使用

  • 系统内核参数优化

  • 安装系统分析工具,其他工具

如果有命令不清楚,可以在浏览器使用此网站查找,或者下载插件来查询命令用法

Linux命令搜索引擎 命令,Linux Linux命令搜索引擎 命令详解:最专业的Linux命令大全,内容包含Linux命令手册、详解、学习,值得收藏的Linux命令速查手册。 - Linux 命令搜索引擎

新建文件夹,在此文件夹下编写shell脚本

[root@vm1 ~]# mkdir shell_scripts
[root@vm1 ~]# cd shell_scripts/

设置时区并同步时间

#设置时区 首先查看/etc/localtime 被链接到哪里

[root@vm1 ~]# ls -l /etc/localtime
lrwxrwxrwx. 1 root root 38 May 18 03:36 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
#这里将美国纽约时间改成中国上海时间 首先断开软链
[root@vm1 ~]# rm /etc/localtime
rm: remove symbolic link ‘/etc/localtime’? y
[root@vm1 ~]#  ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@vm1 ~]# ls -l /etc/localtime
lrwxrwxrwx. 1 root root 33 May 19 12:24 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai



#同步网络时间


[root@vm1 ~]# ntpdate time.windows.com
18 May 22:00:59 ntpdate[11360]: adjust time server 20.189.79.72 offset 0.002811 sec



#定时任务

crontab

[root@vm1 ~]#  (echo "* 2 * * * ntpdate time.windows.com >/dev/null 2>&1" ; crontab -l) | crontab
no crontab for root
[root@vm1 ~]# crontab -l
* 2 * * * ntpdate time.windows.com >/dev/null 2>&1

关闭selinux

#关闭selinux 我的centos7 默认selinux为enforcing

[root@vm1 ~]# getenforce
Enforcing
[root@vm1 ~]# sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
[root@vm1 ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

关闭防火墙

#查看centos版本
[root@vm1 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

#根据不同版本关闭防火墙

if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
         systemctl stop firewalld
         systemctl disabled firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then    
         service iptables stop
         chkconfig iptables off
else
         systemctl stop firewalld.service
         systemctl disable firewalld.service
fi

历史命令显示操作时间

#查看历史命令
[root@vm1 ~]# history
    1  date
    2  ntpdate
    3  ntpdate time.windows.com
    4  yum install -y ntp
    5  ntpdate time.windows.com
    6  date
    7  crontab -l  


#显示时间 日期 命令操作的用户
[root@vm1 ~]# export HISTTIMEFORMAT="%F %T `whoami` "
[root@vm1 ~]# history
    1  2023-05-18 21:49:38 root date
    2  2023-05-18 21:49:38 root ntpdate
    3  2023-05-18 21:49:38 root ntpdate time.windows.com

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

禁止定时任务发送邮件

#默认情况下 定时任务所输出的信息如果没有被处理掉,就会发送给/var/mail/下 默认发给root用户


[root@vm1 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
#禁止定时任务发送邮件
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
[root@vm1 ~]# sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
[root@vm1 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

设置最大文件打开数

默认1024,当服务并发高时,很容易到达1024

#设置最大打开文件数
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/security/limits.conf << EOF:这行代码使用 cat 命令将后续的内容追加到 /etc/security/limits.conf 文件中。>> 表示追加而不是覆盖。<< EOF 表示将输入直到遇到 EOF 为止的内容追加到文件中。

* soft nofile 65535:这是要追加到文件中的第一行内容,表示设置软限制的最大文件描述符数为 65535。

* hard nofile 65535:这是要追加到文件中的第二行内容,表示设置硬限制的最大文件描述符数为 65535

系统内核优化

#系统内核优化
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
net.ipv4.tcp_syncookies = 1:这是要追加到文件中的第一行内容,表示启用 TCP syn cookies,用于防止 SYN 攻击。

net.ipv4.tcp_max_tw_buckets = 20480:这是要追加到文件中的第二行内容,表示设置系统同时保持的最大时间等待套接字数量。

net.ipv4.tcp_max_syn_backlog = 20480:这是要追加到文件中的第三行内容,表示设置系统中处理的最大 SYN 连接请求队列的长度。

net.core.netdev_max_backlog = 262144:这是要追加到文件中的第四行内容,表示设置网络设备接收数据包时的最大排队长度。

net.ipv4.tcp_fin_timeout = 20:这是要追加到文件中的第五行内容,表示设置 TCP 连接的 FIN-WAIT-2 状态的超时时间

减少swap使用

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

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

yum install gcc make autoconf vim sysstat net-tools iostat iftop iotp lrzsz -y

执行脚本前,为了防止自己编写的命令可能可是与unix格式不同,下载dos2unix格式转换工具

yum install -y dos2unix

脚本整合

#!/bin/bash
#设置时区并同步时间

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &>/dev/null

if ! crontab -l | grep ntpdate &>/dev/null ; then
   (echo "* 2 * * * ntpdate time.windows.com >/dev/null 2>&1" ; crontab -l) | crontab
fi
   
#关闭selinux
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
#关闭防火墙
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
         systemctl stop firewalld
		 systemctl disabled firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then	
         service iptables stop
         chkconfig iptables off
else
         systemctl stop firewalld.service
		 systemctl disable firewalld.service
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

使用dos2unix更改格式

[root@vm1 shell_scripts]# dos2unix test
dos2unix: converting file test to Unix format ...
[root@vm1 shell_scripts]# cat test

经过dos2unix转转换后的脚本

#!/bin/bash
#设置时区并同步时间

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &>/dev/null

if ! crontab -l | grep ntpdate &>/dev/null ; then
   (echo "* 2 * * * ntpdate time.windows.com >/dev/null 2>&1" ; crontab -l) | crontab
fi

#关闭selinux
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
#关闭防火墙
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
         systemctl stop firewalld
                 systemctl disabled firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
         service iptables stop
         chkconfig iptables off
else
         systemctl stop firewalld.service
                 systemctl disable firewalld.service
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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值