翼度科技»论坛 云主机 LINUX 查看内容

最小化安装Linux系统初始化脚本

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
目录

最小化安装Linux系统初始化脚本

注:此脚本适用于centos 7/8、Ubuntu1804,具体需要根据实际情况进行测试调整。
此脚本包含的功能:

  • 允许 root 用户使用 ssh 登录
  • 关闭 selinux
  • 关闭防火墙
  • 设置 ps1
  • 设置默认编辑器为 vim
  • 自定义 vim
  • 自定义历史命令
  • 修改内核参数
  • 设置资源限制
  • 修改软件源
  • 安装常用包
  • 设置时间同步
  • 修改网卡为传统命令格式
  • 设置IP地址等
[code][root@centos8 ~]# cat init_v1.sh#!/bin/bash##**************************************************#Author:                Xan_Yum#QQ:                    7993167#Email:                 waluna@qq.com#Version:               1.0#Date:                  2021-11-03#FileName:              init_v1.sh#Description:           system init#URL:                   https://blog.waluna.top#Copyroght (C):         2021 ALL rights reserved#**************************************************OS=`awk -F'"' '/PRETTY_NAME/{print $2}' /etc/os-release|tr ' ' '-'`#1set_ssh () {    if [[ $OS == Ubuntu-18.04* ]];then        sed -i.bak '/#PermitRootLogin/a PermitRootLogin yes' /etc/ssh/sshd_config        systemctl restart sshd    fi    echo -e "\e[32;1mPermit root login set complete\e[0m"}#2disable_selinux () {    if [[ $OS == CentOS* ]];then        sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config    fi    echo -e "\e[32;1mSElinux already disabled,Restart to take effect\e[0m"}#3disbale_firewall () {    systemctl disable --now firewalld &> /dev/null    echo -e "\e[32;1mFirewall already disabled\e[0m"}#4set_ps1 () {    if [[ $OS == CentOS* ]];then        echo "PS1='\[\e[1;36m\][\u@\h \W]\\$ \[\e[0m\]'" >> /etc/profile.d/env.sh        . /etc/profile.d/env.sh    elif [[ $OS == Ubuntu* ]];then        echo 'PS1="\[\e[1;32m\][${debian_chroot:+($debian_chroot)}\u@\h \w]\\$ \[\e[0m\]"' >> .bashrc        . .bashrc    fi    echo -e "\e[32;1mPS1 already modify,Please login again\e[0m"}#5set_default_text_editor_vim () {    echo "export EDITOR=vim" >> /etc/profile.d/env.sh    . /etc/profile.d/env.sh    echo -e "\e[32;1mdefault_text_editor already modify vim,Please login again\e[0m"}#6set_vim () {cat > ~/.vimrc  /etc/profile.d/env.sh    echo -e "\e[32;1mHistory modify\e[0m"}#8modify_kernel_parameters () {    mv /etc/sysctl.conf{,.bak}cat > /etc/sysctl.conf  /etc/security/limits.conf  /var/spool/cron/root        systemctl restart crond    elif [[ $OS == CentOS-Linux-8* ]];then        sed -i.bak '/^pool /c pool time1.aliyun.com iburst' /etc/chrony.conf        systemctl restart chronyd && systemctl enable chronyd        echo '*/5 * * * * chronyc -a makestep &> /dev/null && hwclock -w' >> /var/spool/cron/root        systemctl restart crond    elif [[ $OS == Ubuntu-18.04* ]];then        echo '*/5 * * * * ntpdate time1.aliyun.com &> /dev/null && hwclock -w' >> /var/spool/cron/root        systemctl restart cron    fi    echo -e "\e[32;1mTime sync complete\e[0m"}#13set_eth () {    if [[ $OS == CentOS* ]];then        sed -i.bak '/GRUB_CMDLINE_LINUX/s#"$# net.ifnames=0"#' /etc/default/grub        grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null    elif [[ $OS == Ubuntu-18.04* ]];then        sed -i.bak '/GRUB_CMDLINE_LINUX/s#"$#net.ifnames=0"#' /etc/default/grub        grub-mkconfig -o /boot/grub/grub.cfg &> /dev/null    fi        echo -e "\e[32;1mNetname already modify,Restart to take effect\e[0m"}set_eth0 () {    if [[ $OS == Ubuntu-18.04* ]];then        mv /etc/netplan/01-netcfg.yaml{,.bak}cat > /etc/netplan/01-netcfg.yaml

举报 回复 使用道具