You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/os/get-os-init9.sh

99 lines
2.9 KiB

#!/usr/bin/bash
set -e
color(){
unset c
declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
sleep 0.5
}
static_addr(){
ifname=$(ip -f inet a | awk '/^2/{print $2}')
ifname=$(echo ${ifname/:/})
ipaddr=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $1}')
prefix=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $2}')
gateway=$(ip r |awk '/default/{print $3}')
cat > /etc/sysconfig/network-scripts/ifcfg-$ifname <<EOF
[connection]
id=${ifname}
uuid=`uuidgen`
type=ethernet
autoconnect-priority=-999
interface-name=${ifname}
timestamp=1676740817
[ethernet]
[ipv4]
method=manual
address1=${ipaddr}/${prefix},${gateway}
dns=223.5.5.5
[ipv6]
method=auto
addr-gen-mode=eui64
[proxy]
EOF
nmcli c reload
nmcli c up ${ifname}
ping -w1 -c1 www.baidu.com &>/dev/null && \
return 10 || \
return 20
}
init(){
# 系统信息采集
color Info "当前用户 $USER"
color Info "当前时间 `date "+%F %X"`"
echo 3 >/proc/sys/vm/drop_caches
color Info "当前内存 $(free |awk 'NR==2{print $4/1024}')M"
color Info "当前核心 $(grep -E '^processor' /proc/cpuinfo|wc -l)"
color Info "核心品牌 $(grep -E '^model name' /proc/cpuinfo |head -n 1 |awk -F: '{print $2}')"
color Info "启动时长 $(uptime |awk -F',' '{print $1}')"
color Success "开始配置静态ip"
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65535
* soft nproc 1024
* hard nproc 1024
EOF
color Success "已经优化系统文件打开数、进程打开数"
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
color Success "已经开启系统路由转发"
static_addr
if [ $? -eq 10 ];then
color Success "静态ip配置并检测完成"
else
color Error "静态ip配置异常"
fi
color Info "正在关闭防火墙、selinux"
color Warning "注意,这将降低服务器安全性"
systemctl disable firewalld --now
if [ `getenforce` = 'Enforcing' ];then
setenforce 0
fi
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && \
color Success "配置完成" || color Error "配置异常,请检查"
dnf install -y epel-release
dnf makecache
dnf install -y wget unzip vim yum-utils httpd-tools psacct net-tools nc jq htop bash-completion lrzsz ntpsec && \
color Success "安装完成" || color Warning "部分包可能失败"
color Info "开始进行时间校准"
ntpdate ntp.aliyun.com && \
# 修改时间到硬件
clock -w
#保存时间到硬件
hwclock -s
color Success "时间校准完成"
echo {"time": "`date +'%F %T'`"} > /tmp/.init
}
if [ -f /tmp/.init ];then
color Success "检测到此前已进行初始化,如需重新执行,请删除: rm -f /tmp/.init"
exit 0
fi
init
color Success "初始化完成"