功能介绍:此脚本为系统初始化脚本,能交互式设置 IP 和主机名,配置网络与解析,关闭防火墙和 SELinux,并进行阿里云时间同步。
#!/bin/bash
#交互式设置ip地址和主机名
read -p "请输入要初始化成的ip地址:" ip_address
read -p "请输入要初始化的主机名:" name
#设置ip地址
cat > /etc/sysconfig/network-scripts/ifcfg-ens33 << EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="5ad929c2-d3eb-4504-bbf8-58d80652cd75"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=$ip_address
PREFIX=24
GATEWAY=192.168.60.2
DNS1=114.114.114.114
EOF
#设置主机名
hostnamectl set-hostname ${name}.com
#解析主机名
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
$ip_address ${name}.com
EOF
#关闭防火墙
systemctl stop firewalld
systemctl disabled firewalld
#关闭selinux
setenforce 0
cat > /etc/selinux/config << EOF
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
EOF
#时间同步
ntpdate ntp.aliyun.com