脚本写的比较简单粗暴,只适用于全新的环境,里面的内容大家自行修改。
#!/bin/bash
<<ldp
DateTime: 2023/12/11
Author: ldp
Version: v1.0.0
Mail: @qq.com
此脚本为centos9初始化脚本
功能为
1,关闭防火墙
2,关闭Selinux
3,设置脚本编写模板
4,设置历史命令记录和用户操作
5,设置主机名称
6,下载常用的应用vim sysstat lrzsz wget net-tools
如有不足请批评指正
ldp
echo -e "\033[34;40m***************** $(date +%F-%T) ***************** \033[0m"
echo "************* step1 开始进行Selinux的初始化动作 ************* "
sestatus |grep 'Current mode' |grep -o enforcing && setenforce 0
grep '^SELINUX=enforcing' /etc/sysconfig/selinux && sed -i 's@SELINUX=enforcing@SELINUX=disabled@' /etc/sysconfig/selinux
grep '^SELINUX=enforcing' /etc/selinux/config && sed -i 's@SELINUX=enforcing@SELINUX=disabled@' /etc/selinux/config
echo "************* step1 开始进行Selinux的初始化已完成 ************* "
echo "************* step2 开始进行Firewalld的初始化动作 ************* "
systemctl status firewalld | grep -o 'active (running)' && systemctl stop firewalld
systemctl list-unit-files |grep 'STATE\|firewalld' | awk '{print $2}' |grep -o enabled && systemctl disable firewalld.service
echo "************* step2 开始进行Firewalld的初始化动作已完成 ************* "
echo "************* step2 开始设置历史命令时间用户 ************* "
cat >>~/.bash_profile<<ldp
# format history
# save in ~/.bashrc
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami`@${USER_IP}: "
export HISTFILESIZE=1000000
export PROMPT_COMMAND="history -a; history -r; $PROMPT_COMMAND"
shopt -s histappend
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
ldp
. ~/.bashrc
echo "************* step2 设置历史命令时间用户完成 ************* "
echo "************* step2 下载常用软件 ************* "
yum -y install vim
yum -y install sysstat
yum -y install lrzsz
yum -y install wget
yum -y install net-tools
yum -y install unzip
yum -y install tar
echo "************* step2 下载常用软件完成 ************* "
echo "************* step2 设置vim写作模板 ************* "
cat >~/.vimrc<<ldp
autocmd BufNewFile *.py,*.sh exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
func SetTitle()
"如果文件类型为.sh文件
if &filetype == 'sh'
call setline(1,"\#########################################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: LDP")
call append(line(".")+2, "\# mail: 2043565830@qq.com")
call append(line(".")+3, "\# Created Time: ".strftime("%c"))
call append(line(".")+4, "\#######################################################")
call append(line(".")+5, "\#!/bin/bash")
call append(line(".")+6, "")
elseif &filetype == 'python'
call setline(1, "\#######################################")
call append(line("."), "\#> File Name: ".expand("%"))
call append(line(".")+1, "\#> Author: LDP")
call append(line(".")+2, "\#> Mail: 2043565830@qq.com")
call append(line(".")+3, "\#> Created Time: ".strftime("%c"))
call append(line(".")+4, " \######################################################")
call append(line(".")+5, "#!/usr/bin/python")
call append(line(".")+6, "# -*- coding:utf-8 -*-")
call append(line(".")+7,"")
else
call setline(1, "/*************************************************************************")
call append(line("."), " > File Name: ".expand("%"))
call append(line(".")+1, " > Author: LDO")
call append(line(".")+2, " > Mail: 2043565830@qq.com")
call append(line(".")+3, " > Created Time: ".strftime("%c"))
call append(line(".")+4, "/**********************************************************************")
call append(line(".")+5, "")
endif
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
endfunc
ldp
echo "************* step2 设置vim写作模板完成 ************* "
###注意里面的内容和名称修改为自己的信息###
echo "************* step2 修改主机名称 ************* "
hostnamectl set-hostname --static LDP
echo "************* step2 修改主机名称完成 ************* "
###主机名称填到--static后面
echo "************* step2 清除iptables规则 ************* "
iptables -F
echo "************* step2 清除iptables规则完成 ************* "
echo -e "\033[34;40m***************** $(date +%F-%T) ***************** \033[0m"
echo "************* 当前主机 echo $HOSTNAME 的初始化操作已经完成 ************* "
reboot