shell脚本模板 -- 虚拟机初始化设置

脚本变量设置
  1. 主机名:hostname=vultr
  2. IPV4和网关:ipv4_addresses、ipv4_gateway
  3. 时区:timezone=Asia/Shanghai
  4. 工具软件:softwares=“vim tree bash-completion lrzsz wget setroubleshoot setools”
[root@vultrguest ~]# head -17 setup.sh 
#!/bin/bash

####### COMMON SETTING
# HOSTNAME: leave it blank if u don't want to change
hostname=vultr
# NETWORK : leave it blank if u don't want to change
ipv4_addresses=
ipv4_gateway=
# NETWORK : if ip and gateway were given, change these as needed
interface=ens160
ipv4_dns=114.114.114.114
ipv4_method=manual
connection_autoconnect=yes
# TIMEZONE: leave it blank if u don't want to change
timezone=Asia/Shanghai
# SOFTTOOL: leave it blank if u don't want to change
softwares="vim tree bash-completion lrzsz wget setroubleshoot setools"
[root@vultrguest ~]# 
运行脚本
[root@vultrguest ~]# sh setup.sh 
------- START INITIALIZATION WORK AT 2020-11-19 20:37:53

--- set hostname=vultr

--- set timezone to Asia/Shanghai

--- configure chronyd service
Last metadata expiration check: 0:03:23 ago on Thu 19 Nov 2020 08:34:31 PM CST.
Package chrony-3.5-1.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

--- install softwares
Last metadata expiration check: 0:03:24 ago on Thu 19 Nov 2020 08:34:31 PM CST.
Package vim-enhanced-2:8.0.1763-13.el8.x86_64 is already installed.
Package tree-1.7.0-15.el8.x86_64 is already installed.
Package bash-completion-1:2.7-5.el8.noarch is already installed.
Package lrzsz-0.12.20-43.el8.x86_64 is already installed.
Package wget-1.19.5-8.el8_1.1.x86_64 is already installed.
Package setroubleshoot-3.3.22-2.el8.x86_64 is already installed.
Package setools-4.2.2-2.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

------- FINISH INITIALIZATION WORK AT 2020-11-19 20:37:56
[root@vultrguest ~]# 
脚本
#!/bin/bash

####### COMMON SETTING
# HOSTNAME: leave it blank if u don't want to change
hostname=
# NETWORK : leave it blank if u don't want to change
ipv4_addresses=
ipv4_gateway=
# NETWORK : if ip and gateway were given, change these as needed
interface="ens160"
ipv4_dns=114.114.114.114
ipv4_method=manual
connection_autoconnect=yes
# TIMEZONE: leave it blank if u don't want to change
timezone=
# SOFTTOOL: leave it blank if u don't want to change
softwares=""

####### START SETTING

# HEAD
echo "------- START INITIALIZATION WORK AT $(date +'%F %T')"  

# HOSTNAME
if [ $hostname ]
then
  echo ""
  echo "--- set hostname=$hostname"  
  hostnamectl set-hostname $hostname
fi

# NETWORK
if [ $ipv4_addresses ] && [ $ipv4_gateway ]
then
  echo ""  
  echo "--- set network"  
  echo ""  
  # preparing
  uuid=$(uuidgen $interface)
  echo "Set interface $interface ipv4.addresses          to  $ipv4_addresses"          
  echo "Set interface $interface ipv4.gateway            to  $ipv4_gateway"            
  echo "Set interface $interface ipv4.dns                to  $ipv4_dns"                
  echo "Set interface $interface ipv4.method             to  $ipv4_method"             
  echo "Set interface $interface uuid                    to  $uuid"                    
  echo "Set interface $interface connection.autoconnect  to  $connection_autoconnect"  
  # setting
  nmcli c modify $interface connection.autoconnect $connection_autoconnect ipv4.method $ipv4_method \
    ipv4.addresses $ipv4_addresses ipv4.gateway $ipv4_gateway ipv4.dns $ipv4_dns  
  sed -i "s/^UUID.*/UUID=$uuid/g" /etc/sysconfig/network-scripts/ifcfg-$interface  
  # reloading
  nmcli c reload
  nmcli c up $interface
fi

####### TIMEZONE & TIME SYNC
# Time Zone
if [ $timezone ]
then
  echo ""
  echo "--- set timezone to $timezone"  
  timedatectl set-timezone $timezone
fi
# Time Sync
echo ""
echo "--- configure chronyd service"  
dnf install chrony -y     
systemctl start chronyd   
systemctl enable chronyd  

####### INSTALL SOFT
echo ""  
echo "--- install softwares"  
dnf install -y $softwares  

####### FINISH SETTING
echo ""  
echo "------- FINISH INITIALIZATION WORK AT $(date +'%F %T')"  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值