Linux装机后总会做一些基础配置,这里分享一些我个人使用Centos时习惯性的配置,先分步骤解释,后脚本统一执行

1.启动网络服务(我这里是动态获取IP)

sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-ens33

systemctl restart network

2.关闭防火墙服务

systemctl stop firewalld
systemctl disable firewalld

3.修改主机名

hostnamectl set-hostname wakamizu-test01

4.增加映射

echo "192.168.1.1.105 wakamizu-test01">>/etc/hosts

5.关闭sellinix

setenforce 0

sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config

6.修改yum源(个人习惯使用清华源)

mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo bak
mv /etc/yum.repos.d/bak/CensOS-Base.repo /etc/yum.repos.d/QH.repo
sed -i 's/^mirrorlist/#mirrorlist/g' QH.repo
sed -i 's/^#baseurl/baseurl/g' QH.repo
sed -i 's/mirror.centos.org/mirrors.tuna.tsinghua.edu.cn/g' QH.repo
yum clean all
yum makecache


7.安装常用服务(vim、net-tools、chrony、wget)

yum install vim net-tools wget chrony -y

启动时间同步服务

systemctl start chronyd
chronyc sources

以上就是我新机常用的配置,下面把它转化为脚本一键执行(后续功能会继续完善,各位也可提一下需求)

#!/bin/bash

#配置网卡
networkcfg(){
  read -p "请输入网卡配置方式:1.静态获取IP 2.动态获取IP" netmethon
  case $netmethon in
    1)
      sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static'/ /etc/sysconfig/network-scripts/ifcfg-ens33
      sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-ens33
      read -p "请输入您的IP:" ip
      read -p "请输入您的子网掩码:" netmask
      read -p "请输入您的网关:" gateway
      read -p "请输入您的dns:" dns
      cat >> /etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
IPADDR=$ip
NETMASK=$netmask
GATEWAY=$gateway
EOF
      echo "nameserver $dns" >> /etc/resolv.conf
      systemctl restart network
    ;;
    2)
      sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-ens33
      systemctl restart network
    ;;
    *)
      echo "请输入正确的选项!"
    ;;
  esac
}
#配置主机名及映射
hostnamecfg(){
  read -p "请输入您的主机名:" hostname
  hostnamectl set-hostname $hostname
  nowip=`ip addr | grep inet | grep -v inet6 | grep -v 127 | cut -d ' ' -f6|sed 's/\/.*//'`
  echo "$nowip $hostname" >> /etc/hosts
}
#配置防火墙及selinux
safecfg(){
  systemctl stop firewalld 
  systemctl disable firewalld >> /dev/null 2>&1 
  setenforce 0
  sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config
}
#配置yum源
yumcfg(){
  mkdir /etc/yum.repos.d/bak
  mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
  mv /etc/yum.repos.d/bak/CentOS-Base.repo /etc/yum.repos.d/QH.repo
  sed -i 's/^mirrorlist/#mirrorlist/g' /etc/yum.repos.d/QH.repo
  sed -i 's/^#baseurl/baseurl/g' /etc/yum.repos.d/QH.repo
  sed -i 's/mirror.centos.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/yum.repos.d/QH.repo
  yum clean all
  yum makecache
}
#配置常用服务
servicecfg(){
yum install $* -y >> /dev/null 2>&1
systemctl start chronyd
systemctl enable chronyd >> /dev/null 2>&1
chronyc sources >> /dev/null 2>&1
echo "已成功安装$*服务"
}

#检查执行状态
state(){
  statenum=`echo $?`
  if [ $statenum == 0 ]
  then
    echo $1 
  else      
    echo "执行脚本出错!退出脚本!"
    exit 1
  fi
}

#调用部分
networkcfg
nowip=`ip addr | grep inet | grep -v inet6 | grep -v 127 | cut -d ' ' -f6|sed 's/\/.*//'`
state "您目前的IP是:"$nowip""
ostnamecfg
state "主机名`hostname`设置完毕,映射设置完毕!"
safecfg
state "已关闭firewalld、selinux服务!"
yumcfg
state "已成功安装yum源!"
#新增服务可往后加入servicecfg 服务1 服务2
servicecfg vim net-tools wget chrony